bash: append new line/string/text if input string has 2 lines -


i got following output , want test whether line count (e.g. wc -l) equal 2. if so, want append something. must use chain pipes.

start input:

echo "this new line test" 

goal output:

"this  new line test  chars" 

but if start input line count equals 2.

i tried like:

echo "this new line test" | while read line ; lines=$(echo "$lines\n$line") ; echo $all ... ; done 

but none of ideas got solution. using sed/awk, etc ok, should chained pipe.

thanks!

awk '1; end {if (nr <= 2) print "another line"}' file 

here's way fun: bash version 4

mapfile lines <file; (ifs=; echo "${lines[*]}"); ((${#lines[@]} <= 2)) && echo line 

better bash: tee process substitution

$ seq 3 | tee >( (( $(wc -l) <= 2 )) && echo line ) 1 2 3  $ seq 2 | tee >( (( $(wc -l) <= 2 )) && echo line ) 1 2 line  $ seq 1 | tee >( (( $(wc -l) <= 2 )) && echo line ) 1 line 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -