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

前端 未结 10 2546
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 12:58

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 fir

10条回答
  •  臣服心动
    2020-12-04 13:38

    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.

提交回复
热议问题