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

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 fine, but how do I print everything in column $ 3 and then all columns after?

+6
source share
5 answers

In this case, you can use cut instead of awk:

  cut -f1,3- -d ' ' 
+11
source
 awk '{ $2 = ""; print }' # remove col 2 
+8
source

If you do not mind a small gap:

 awk '{ $2="" }1' 

But UUOC and grep :

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

If you want to trim spaces:

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


For fun, here is another way: GNU sed :

 < dbfake sed -r '/100%/s/^(\S+)\s+\S+(.*)/\1\2/' | ... 
+1
source

Others answered in different ways, but I want to point out that using xargs output for multiplexing is a pretty bad idea.

Instead, why would you:

 awk '$2=="100%" { sub("100%[[:space:]]*",""); print; print >>"fake.log"}' dbfake 

It's all. You do not need grep, you do not need multiple channels, and definitely you do not need to use a fork shell for every line that you output.

You can do awk ...; print}' | tee fake.log awk ...; print}' | tee fake.log awk ...; print}' | tee fake.log , but forking tee doesn't make much sense if awk can handle it too.

0
source

All you need is:

 awk 'sub(/.*100% /,"")' dbfake | tee "fake.log" 
0
source

All Articles