简体   繁体   English

bash awk第一列和第三列包含所有内容

[英]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 : 但是UUOCgrep

< 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.

相关问题 按第3列和第1列进行Bash排序 - Bash sorting by 3rd column and 1st column awk比较3个值,第一个文件值之间的第二个文件值和两个文件之间的多列打印输出到第3个 - Awk comparing 3 values, 2nd file value between 1st file values with multiple column printout between both files to a 3rd 在bash / awk中是否有任何方法可以打印出第1列的输入文件(第1列是IPv4和IPv6),并将其转换为主机名? - Is there any way in bash/awk, to prints out 1st column of inputed file(1st columns are IPv4s and IPv6s), and convert that to their hostnames? 使用awk计算文件第一列中的记录数? - Count the number of records in 1st column of a file using awk? 比较 CSV 文件中每一行的第 2 和第 3 字段,如果匹配,则在 BASH 中添加第 1 字段 - Compare 2nd and 3rd field of CSV file for each row and if matches, do addition of 1st field in BASH awk或shell命令根据第4列中的值来计数第1列中的值的出现 - awk or shell command to count occurence of value in 1st column based on values in 4th column 模式 - 保留第一列的第一个实例并替换其他实例 - Pattern - Leave 1st instance of 1st column and replace others 在bash中为第一列中的每个不同值查找第n列中的最大值 - Find the maximum values in nth column for each distinct values in 1st column in bash grep,剪切,sed,awk文件的第3列,一次n行,然后粘贴到n行的重复列中? - grep, cut, sed, awk a file for 3rd column, n lines at a time, then paste into repeated columns of n rows? 如果第一个第二和第三个字段相同,我如何使用排序或其他 bash cmd 从所有行中获取 1 行 - How Can I Use Sort or another bash cmd To Get 1 line from all the lines if 1st 2nd and 3rd Field are The same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM