Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print columns with AWK

Tags:

awk

I tried to print only odd columns but I could not!

awk '{for (i=1; i<=NF; i++) print $2*i-1}' file > test

but it prints everything in one column!

Would you please help me?

Thank you

like image 807
EpiMan Avatar asked Mar 30 '13 09:03

EpiMan


People also ask

How do I print a column in Linux?

The `awk` command is one of many commands that can be used to print a range of columns from tabular data in Linux. The `awk` command is can be used directly from the terminal by executing the `awk` script file.

How do I print the first column in awk?

awk '{print $1}' information. txt prints the first column.

What is awk '{ print $1 }'?

If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.


1 Answers

Just use i+=2:

awk '{ for (i=1;i<=NF;i+=2) print $i }' file > test

For the new requirements, just make null the 'even' columns:

awk '{ for (i=2;i<=NF;i+=2) $i="" }1' file > test
like image 178
Steve Avatar answered Nov 15 '22 11:11

Steve