user3155939 user3155939 - 6 months ago 76
Bash Question

delete last 3 columns from a file

I have a text file with 6 columns. How can I delete the last three columns from this file?

c1 c2 c3 c4 c5 c6

desired output

c1 c2 c3

ray ray
Answer Source

You can use @paxdiablo's awk to reduce the value of NF

alternatively you can use sed

sed 's/\([ \t]\+[^ \t]*\)\{3\}$//' your_file

If you are on Linux, GNU sed provides -r

sed -r 's/([ \t]+[^ \t]*){3}$//' your_file
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download