Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

awk

xargs

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?

like image 873
ooXei1sh Avatar asked Feb 10 '13 22:02

ooXei1sh


3 Answers

You can use cut instead of awk in this case:

  cut -f1,3- -d ' '
like image 93
mjuarez Avatar answered Nov 20 '22 11:11

mjuarez


awk '{ $2 = ""; print }' # remove col 2
like image 43
DigitalRoss Avatar answered Nov 20 '22 11:11

DigitalRoss


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/' | ...
like image 1
Steve Avatar answered Nov 20 '22 09:11

Steve