Extract specific columns from delimited file using Awk

I don’t know if it’s possible to do ranges in awk. You could do a for loop, but you would have to add handling to filter out the columns you don’t want. It’s probably easier to do this: awk -F, ‘{OFS=”,”;print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$20,$21,$22,$23,$24,$25,$30,$33}’ infile.csv > outfile.csv something else to consider – and this faster and more … Read more

How to escape a single quote inside awk

This maybe what you’re looking for: awk ‘BEGIN {FS=” “;} {printf “‘\”%s’\” “, $1}’ That is, with ‘\” you close the opening ‘, then print a literal ‘ by escaping it and finally open the ‘ again.

Sed to extract text between two strings

sed -n ‘/^START=A$/,/^END$/p’ data The -n option means don’t print by default; then the script says ‘do print between the line containing START=A and the next END. You can also do it with awk: A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines … Read more

Why does “1” in awk print the current line?

In awk, Since 1 always evaluates to true, it performs default operation {print $0}, hence prints the current line stored in $0 So, awk ‘$2==”no”{$3=”N/A”}1’ file is equivalent to and shorthand of awk ‘$2==”no”{$3=”N/A”} {print $0}’ file Again $0 is default argument to print, so you could also write awk ‘$2==”no”{$3=”N/A”} {print}’ file In-fact you … Read more