[英]bash awk first 1st column and 3rd column with everything after
I am working on the following bash script: 我正在处理以下bash脚本:
# 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? 这个脚本工作正常,但是如何打印$ 3列中的所有内容,然后打印所有列?
在这种情况下,您可以使用cut而不是awk:
cut -f1,3- -d ' '
awk '{ $2 = ""; print }' # remove col 2
If you don't mind a little whitespace: 如果你不介意一点空白:
awk '{ $2="" }1'
But UUOC and grep
: 但是UUOC和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
: 为了好玩,这是使用GNU sed
的另一种方式:
< dbfake sed -r '/100%/s/^(\S+)\s+\S+(.*)/\1\2/' | ...
Others responded in various ways, but I want to point that using xargs to multiplex output is rather bad idea. 其他人以各种方式做出回应,但我想指出使用xargs来复用输出是个坏主意。
Instead, why don't you: 相反,你为什么不:
awk '$2=="100%" { sub("100%[[:space:]]*",""); print; print >>"fake.log"}' dbfake
That's all. 就这样。 You don't need grep, you don't need multiple pipes, and definitely you don't need to fork shell for every line you're outputting. 你不需要grep,你不需要多个管道,而且你不需要为你输出的每一行分叉shell。
You could do awk ...; print}' | tee fake.log
你可以做awk ...; print}' | tee fake.log
awk ...; print}' | tee fake.log
awk ...; print}' | tee fake.log
, but there is not much point in forking tee, if awk can handle it as well. awk ...; print}' | tee fake.log
,但是如果awk也可以处理它,那么分叉三通没有多大意义。
所有你需要的是:
awk 'sub(/.*100% /,"")' dbfake | tee "fake.log"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.