bash awk 第一列和第三列之后的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14803308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:31:18  来源:igfitidea点击:

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

bashawkxargs

提问by ooXei1sh

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 , }' |

# 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 列中的所有内容,然后打印所有列?

回答by mjuarez

You can use cut instead of awk in this case:

在这种情况下,您可以使用 cut 而不是 awk :

  cut -f1,3- -d ' '

回答by DigitalRoss

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

回答by Steve

If you don't mind a little whitespace:

如果你不介意一点空白:

awk '{ ="" }1'

But UUOCand grep:

但是UUOCgrep

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

If you'd like to trim that whitespace:

如果你想修剪那个空格:

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




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

为了好玩,这是另一种使用方式GNU sed

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

回答by Steve

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 '=="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, but there is not much point in forking tee, if awk can handle it as well.

你可以这样做awk ...; print}' | tee fake.log,但是如果 awk 也可以处理它,那么分叉 tee 没有多大意义。

回答by Ed Morton

All you need is:

所有你需要的是:

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