Aaron Q Aaron Q - 1 year ago 74
Bash Question

how do I delete lines and columns together using awk

I am super new here, I am trying to remove a few rows (header) and some columns (not wanted fields) from a data file, I found this:
if I use:

awk 'NR>2' text.txt >newtext.txt

I can basically get rid of the first two lines.
if I use:

awk '{for(z=2;z<=13;z++)$z="";$0=$0;$1=$1}1' text.txt > newtext.txt

I get the desired columns.
However, I cannot do them together. How do I combine two commands?
How could I do this in an efficient way?

Answer Source

You can try something like this

 awk '{if (NR>2) { print $0; print $1  }} ' text.txt >newtext.txt

The if condition will make sure that first 2 lines are not output. And using print command inside if will output only your desired column

Note:I suggest you to use print command inside awk to output text for redirecting to file

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download