4

Possible Duplicate:
How to loop over the lines of a file?

Let's say I cat a file and I want to run a bash command on each line and write each line as output.

How can I run a bash command on a single line the most succinct way?

cat something | lineBylineInplpace grep -E "spo" > &1

(I forget the standard descriptor symbol = / )

Should output the output of each line to the new file in place with the line it used for input.

I realize that this is very similar to awk. Is there a bash builtin kind of way to make this faster?

Tegra Detra
  • 4,908
  • 11
  • 39
  • 53
  • i found http://www.linuxquestions.org/questions/programming-9/bash-shell-script-read-file-line-by-line-136784/page2.html – Tegra Detra Dec 09 '12 at 22:18
  • this seems to be the best so far cat file | ( while read line do echo process $line done ) – Tegra Detra Dec 09 '12 at 22:19
  • 1
    You like useless uses of cats, don't you? – gniourf_gniourf Dec 09 '12 at 22:28
  • And see also [Understanding IFS](http://unix.stackexchange.com/q/26784) – Gilles 'SO- stop being evil' Dec 09 '12 at 23:06
  • i learned IFS the last time you recommended it =) – Tegra Detra Dec 10 '12 at 06:16
  • also its not a duplicate i wanted to edit line by line in the file this seems a bit pushy and negative to close this topic. there are a million ways to do things like this and i am sure there are ways that you dont kno this "bash" thing has been around for a while if u didnt know. the problem i think is people really forgot how they used it and i want to kno not get pushed down on the stack – Tegra Detra Dec 10 '12 at 06:18

1 Answers1

11

The proper way to process a file line by lines is :

   while read -r line; do
       echo "$line"
    done < /path/to/file.txt

See http://mywiki.wooledge.org/BashFAQ/001

NOTE

  • using unix pipes have a cost, better avoid it for speed
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82