bash awk first 1st column and 3rd column with everything after

17,642

Solution 1

You can use cut instead of awk in this case:

  cut -f1,3- -d ' '

Solution 2

awk '{ $2 = ""; print }' # remove col 2

Solution 3

If you don't mind a little whitespace:

awk '{ $2="" }1'

But UUOC and grep:

< dbfake awk '/100%/ { $2="" }1' | ...

If you'd like to trim that whitespace:

< dbfake awk '/100%/ { $2=""; sub(FS "+", FS) }1' | ...


For fun, here's another way using GNU sed:

< dbfake sed -r '/100%/s/^(\S+)\s+\S+(.*)/\1\2/' | ...
Share:
17,642
ooXei1sh
Author by

ooXei1sh

I enjoy python, javascript, c#, php, sql, html, css and likely other stuff? “Let the future tell the truth, and evaluate each one according to his work and accomplishments. The present is theirs; the future, for which I have really worked, is mine.” - Nikola Tesla

Updated on June 11, 2022

Comments

  • ooXei1sh
    ooXei1sh almost 2 years

    I am working on the following bash script:

    # contents of dbfake file
    1 100% file 1
    2 99%  file name 2
    3 100% file name 3
    
    #!/bin/bash
    
    # cat out data
    cat dbfake |
    
    # select lines containing 100%
    grep 100% |
    
    # print the first and third columns
    awk '{print $1, $3}' |
    
    # echo out id and file name and log
    xargs -rI % sh -c '{ echo %; echo "%" >> "fake.log"; }'
    
    exit 0
    

    This script works ok, but how do I print everything in column $3 and then all columns after?