Need a command to delete certain entries in a text file (Solved)

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

User avatar
amethyst
Posts: 2355
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 55 times
Been thanked: 473 times

Re: Need a command to delete certain entries in a text file

Post by amethyst »

@Burunduk
That last script of yours does not work for me. I'll go with the following which gives the desired results. Topic marked as solved:

Code: Select all

D=/tmp/.packages
tmpfile=`mktemp`

for file in "$D"/*; do
  while read -r line; do
    [ -f "$line" ] && printf "%s\n" "$line"
  done <"$file" >"$tmpfile"
  mv "$tmpfile" "$file"
done

find "$D" -mindepth 1 -empty -delete
Last edited by amethyst on Tue Jan 03, 2023 5:44 am, edited 1 time in total.
Burunduk
Posts: 244
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 122 times

Re: Need a command to delete certain entries in a text file

Post by Burunduk »

amethyst wrote: Tue Jan 03, 2023 5:18 am

That last script of yours does not work for me....

This is not a very informative bug report.

Well, probably you are using the #!/bin/sh shebang. In this mode bash doesn't support process substitution. If the directory is flat, your code will work as it is.

Code: Select all

root# bash -c 'head <(echo foo)'
foo
root# sh -c 'head <(echo foo)'
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `head <(echo foo)'
User avatar
amethyst
Posts: 2355
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 55 times
Been thanked: 473 times

Re: Need a command to delete certain entries in a text file (Solved)

Post by amethyst »

Asked ChatGPT to explain the code just to see the response. It's quite good, didn't expect to get such a complete and well-explained answer. https://chat.openai.com/share/2cbb6711- ... 37f8ffcadf

Post Reply

Return to “Programming”