I have a file with many lines in each line there are many columns(fields) separated by blank " " the numbers of columns in each line are different I want to remove the first two columns how to?
Without GNU awk you need a match() + substr() combo or multiple sub() s + vars to remove a middle field. See also Print all but the first three columns. Note: on Ubuntu Trusty GNU Awk 4.0. 1 doesn't have the awk inplace extension enabled by default.
Use the colrm command to remove specified columns from a file. Input is taken from standard input. Output is sent to standard output. If the command is called with one parameter, the columns of each line from the specified column to the last column are removed.
You can do it with cut
:
cut -d " " -f 3- input_filename > output_filename
Explanation:
cut
: invoke the cut command-d " "
: use a single space as the delimiter (cut
uses TAB by default)-f
: specify fields to keep3-
: all the fields starting with field 3input_filename
: use this file as the input> output_filename
: write the output to this file.Alternatively, you can do it with awk
:
awk '{$1=""; $2=""; sub(" ", " "); print}' input_filename > output_filename
Explanation:
awk
: invoke the awk command$1=""; $2="";
: set field 1 and 2 to the empty stringsub(...);
: clean up the output fields because fields 1 & 2 will still be delimited by " "print
: print the modified lineinput_filename > output_filename
: same as above.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With