bash unix使用aw​​k和cut一起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41882138/
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 15:39:56  来源:igfitidea点击:

Unix using awk and cut together

bashunixawkcut

提问by connollyc4

I am trying to get netstat, awk, and cut to work together to parse two parts of netstat output

我试图让 netstat、awk 和 cut 协同工作来解析 netstat 输出的两个部分

I want to pull only the last 4 numbers of the first column:

我只想提取第一列的最后 4 个数字:

 11.111.222.32.3433   
 10.204.101.85.3433
 10.204.101.85.3433

Which I am able to do:

我能够做到:

netstat -an |grep 3433 |awk '{print }' | cut -d "." -f5

But I also want to print the second column but leave off the last 4 numbers

但我也想打印第二列但去掉最后 4 个数字

example:

例子:

 33.44.444.43.5555   
 24.204.101.85.3434
 11.204.101.85.6533

So I want the final output to look like

所以我希望最终的输出看起来像

3433 | 33.44.444.43
3433 | 24.204.101.85
3433 | 11.204.101.85

I tried to combine two awks but I get an error

我试图组合两个 awk,但出现错误

Is this possible?

这可能吗?

Thank you!

谢谢!

回答by Ronak Patel

for my system it was col 4, and 5 so following worked for me:

对于我的系统,它是第 4 列和第 5 列,因此以下对我有用:

$ netstat -an|grep 3433|awk '{print ,"|",}'|sed s/:/./g|cut -d '.' -f5-8

as you have described your data, this should work for you

正如您描述的数据,这应该适合您

$ netstat -an|grep 3433|awk '{print ,"|",}'|cut -d '.' -f5-8

回答by JNevill

You can cut in awk using awk's function split. You can also filter records using a regex condition within awk, making grepand cutsuperfluous.

您可以使用 awk 的函数切入 awk split。您还可以使用 awk、制作grepcut多余的正则表达式条件过滤记录。

Something like the following should get you close:

像下面这样的东西应该让你接近:

 netstat -an  | awk 'BEGIN{OFS=" | "} ~/3433$/{split(,a,"."); split(,b,"."); print a[5], b[1]"."b[2]"."b[3]"."b[4] }'

This breaks down like:

这分解为:

  1. Set the output field separator to be a pipe between two spaces
  2. Check the first field to see if it ends with 3433
  3. Split the first field by a period and store the results in array named a
  4. Split the second field by a period and store it's results in array named b
  5. Print out the 5th element of array a, followed by the OFS, and then the 1st, 2nd, 3rd, and 4th elements of array bwith a period between them.
  1. 将输出字段分隔符设置为两个空格之间的管道
  2. 检查第一个字段以查看它是否以 3433
  3. 按句点拆分第一个字段并将结果存储在名为的数组中 a
  4. 按句点拆分第二个字段并将其结果存储在名为的数组中 b
  5. 打印出 array 的第 5 个元素a,然后是 OFS,然后是 array的第 1、2、3 和 4 个元素,b它们之间有一个句点。

You can also have awk determine a distinct field by using more than one delimiter, which may fit here too, depending on your needs.

您还可以让 awk 使用多个定界符来确定一个不同的字段,这也可能适合此处,具体取决于您的需要。

This would look something like:

这看起来像:

 netstat -an  | awk -F"[ .]" 'BEGIN{OFS="."} =="3433" { print " | " ,,,}'

回答by karakfa

direct replacement of your grep in awk

直接替换你的grep awk

$ ... | awk -v OFS=' | ' '/3433/{sub(/.*\./,"",); sub(/\.[^.]+$/,"",)}1'

3433 | 33.44.444.43
3433 | 24.204.101.85
3433 | 11.204.101.85

however, you should be checking $1~/3433$/instead.

但是,您应该$1~/3433$/改为检查。

回答by Walter A

You can also use sed:

您还可以使用sed

echo " 33.44.444.43.5555
 24.204.101.85.3434
 11.204.101.85.6533" | sed 's/\(.*\)\.\([^.]*\)$/ | /'