Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove the first two columns in a file using shell (awk, sed, whatever)

Tags:

shell

sed

awk

perl

cut

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?

like image 644
wenzi Avatar asked Nov 19 '12 00:11

wenzi


People also ask

How do you delete a column in awk?

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.

How do you delete a column from a file in Linux?

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.


1 Answers

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 keep
  • 3-: all the fields starting with field 3
  • input_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 string
  • sub(...);: clean up the output fields because fields 1 & 2 will still be delimited by " "
  • print: print the modified line
  • input_filename > output_filename: same as above.
like image 186
sampson-chen Avatar answered Sep 19 '22 18:09

sampson-chen