I'd like to pass lines through cut line-by-line (much like egrep --line-buffered
). How can I do this? [awk
, sed
, perl
solutions welcome, just please keep them short. :)]
If you just want to emulate the function of cut in a loop in UNIX (which can be quite slow), you can use awk's substr function.
e.g., say you have a text file (lines.txt) that's arranged like this:
line1: the first line
line2: the second line
line3: the third line
line4: the fourth line
line5: the fifth line
With this one-liner, where 8 is the index of the character you want to start printing each the line from:
awk '{ print substr($0,8) }' lines.txt
This is the result:
the first line
the second line
the third line
the fourth line
the fifth line
If you have a specific word or regexp to remove, you can feed it into awk as a field separator and then print the portion of the line that comes after the portion you would like removed:
For example, this one-liner:
awk 'BEGIN { FS=": " } { print $2 }' lines.txt
Would also print out:
the first line
the second line
the third line
the fourth line
the fifth line
Because you can exploit the fact that each line contains a semicolon followed by a space (": ") to divide the line into two parts. Then you simply tell awk to print the second part (i.e. field) of each line (print $2).