0

I have a csv file that I loop through and pick only some lines. Like that:

while read p; do
    if [[ "$p" =~ '0'$ ]]; then
        echo "$p"
    fi
done <$1

I would like the picked records to be appended to the original file, like this

  • old_records
  • "Picked records:"
  • new_appended_record

However, when I add >> $1 at the end of my script I seem to enter into an infinite loop.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
OskarF
  • 1
  • 1
  • Reading from a file as you modify it (in this case, appending to it) can cause a lot of trouble if you don't understand how the writing & reading interact. It's much safer to save the selected lines somewhere else temporarily, then append them only after you finish scanning the file's original contents. – Gordon Davisson Mar 11 '22 at 10:51
  • @GordonDavisson Do you mean something like this: -read loop ->write to temp file ->append temp_file to the input_file ->remove temp_file ? And, will the loop at the top of the script not read the values appended from the temp file? Or maybe I need to do something to make it break before the joining of the temp file? Thank you – OskarF Mar 11 '22 at 10:58
  • 1
    Yes, something like that. As long as you put the "append" step *after the end of the read loop*, it won't read the new lines you append (because it'll have finished reading already). – Gordon Davisson Mar 11 '22 at 11:02
  • 1
    BTW, you could also replace the `while read` loop with something like `grep '0$' "$1" >"$tmpfile"` – Gordon Davisson Mar 11 '22 at 17:16
  • Also see [Bash Pitfalls #13 (cat file | sed s/foo/bar/ > file)](https://mywiki.wooledge.org/BashPitfalls#cat_file_.7C_sed_s.2Ffoo.2Fbar.2F_.3E_file). – pjh Mar 11 '22 at 17:40

0 Answers0