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

前端 未结 10 2528
佛祖请我去吃肉
佛祖请我去吃肉 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.
    0 讨论(0)
  • 2020-12-04 13:39

    This might work for you (GNU sed):

    sed -r 's/^([^ ]+ ){2}//' file
    

    or for columns separated by one or more white spaces:

    sed -r 's/^(\S+\s+){2}//' file
    
    0 讨论(0)
  • 2020-12-04 13:41

    Its pretty straight forward to do it with only shell

    while read A B C; do
    echo "$C"
    done < oldfile >newfile
    
    0 讨论(0)
  • 2020-12-04 13:42

    Using awk, and based in some of the options below, using a for loop makes a bit more flexible; sometimes I may want to delete the first 9 columns ( if I do an "ls -lrt" for example), so I change the 2 for a 9 and that's it:

    awk '{ for(i=0;i++<2;){$i=""}; print $0 }' your_file.txt

    0 讨论(0)
提交回复
热议问题