I would harness GNU AWK
for this task following way, let file.txt
content be
test to print
1 aa ee 0.000 0.000 0.000
2 bb ff 0.000 0.000 0.000
3 cc gg 0 0 0
then
awk 'BEGIN{ORS="\t"}{print /test/?$0:$3}' file.txt
gives output
test to print ee ff gg
Explanation: I inform GNU AWK to use tab character as output record separator (ORS
) that is when print
ing tab is used as last character rather than newline. Then for each line I print depending on test
presence in said line, if it is present whole line ($0
) otherwise 3rd column ($3
). If you want to know more about ORS
read 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR.
(tested in gawk 4.2.1)
bash awk pivot file
- markp-fusogrep
on? what happens if there are multiple lines with matches ... process all matches? process just the first match? and what happens if there's an overlap (eg, every other line has agrep
match)? - markp-fuso