Using awk to print all columns from the nth to the last
-----------------------
Print all except first column:
$ awk '{$1=""; print $0}' myfile
Print all except two first columns:
$ awk '{$1=$2=""; print $0}' myfile
$ awk '{$1=$2=""; print}'
or
$ awk '{$1=$2=""}1'
Print except 3rd field
$ awk '!($3="")' myfile
Using GNU sed
$ sed -i -r 's/\S+//3' myfile
Delete the white space before the 3rd field
$ sed -i -r 's/(\s+)?\S+//3' file
$ awk '{print $1 " " $2}' myfile
Remove the last field of each line.
$ awk -i inplace NF--
$ awk '$3="";1' myfile > new_file && mv new_file myfile
or
$ awk '{$3="";print}' myfile > new_file && mv new_file myfile
If your data is in a file input.txt you can do one of the following:
$ cat myfile | tr -s ' ' | cut -d ' ' -f-2
Or to remove the 3rd column
$ cat myfile | tr -s ' ' | cut -d ' ' --complement -f3
You want specific columns 1 and 2 with...
$ cat myfile | tr -s ' ' | cut -d ' ' -f1,2
Using Perl solution...
$ perl -ane 'print "$F[0] $F[1]\n"' file
command-line options are used:
-n loop around every line of the input file, do not automatically print every line
-a autosplit mode – split input lines into the @F array. Defaults to splitting on whitespace
-e execute the following perl code
Using awk
$ awk -F- '$0=$1' myfile
Using cut
$ cut -d- -f1 myfile
Using sed
$ sed 's/-.*//' myfile
Using perl
$ perl -pe 's/-.*//' myfile
No comments:
Post a Comment