[英]awk, sed, grep specific strings from a file in Linux
Here is part of the complete file that I am trying to filter:这是我要过滤的完整文件的一部分:
Hashmode: 13761 - VeraCrypt PBKDF2-HMAC-SHA256 + XTS 512 bit + boot-mode (Iterations: 200000)
Speed.#2.........: 2038 H/s (56.41ms) @ Accel:128 Loops:32 Thr:256 Vec:1
Speed.#3.........: 2149 H/s (53.51ms) @ Accel:128 Loops:32 Thr:256 Vec:1
Speed.#*.........: 4187 H/s
The aim is to print the following: 13761 VeraCrypt PBKDF2-HMAC-SHA256 4187 H/s
目的是打印以下内容:
13761 VeraCrypt PBKDF2-HMAC-SHA256 4187 H/s
Here is what I tried.这是我尝试过的。 The complete file is called complete.txt
完整的文件称为 complete.txt
cat complete.txt | grep Hashmode | awk '{print $2,$4,$5}' > mode.txt
Output: Output:
13761 VeraCrypt PBKDF2-HMAC-SHA256
Then:然后:
cat complete.txt | grep Speed.# | awk '{print $2,$3}' > speed.txt
Output: Output:
4187 H/s
Then:然后:
paste mode.txt speed.txt
The issue is that the lines do not match.问题是线条不匹配。 There are approx 200 types of modes to filter within the file 'complete.txt'
文件“complete.txt”中有大约 200 种模式可供过滤
I also have a feeling that this can be done using a much simpler command with sed
or awk.我也有一种感觉,这可以使用更简单的命令
sed
或 awk 来完成。
I am guessing you are looking for something like the following.我猜你正在寻找类似以下的东西。
awk '/Hashmode:/ { if(label) print label, speed; label = $2 " " $4 " " $5 }
/Speed\.#/ { speed = $2 " " $ 3 }
END { if (label) print label, speed }' complete.txt
We match up the Hashmode
line with the last Speed.#
line which follows, then print when we see a new Hashmode, or reach end of file.我们将
Hashmode
行与最后的Speed.#
行匹配,然后在我们看到新的 Hashmode 或到达文件末尾时打印。 (Failing to print the last one is a common beginner bug.) (未能打印最后一个是常见的初学者错误。)
This might work for you (GNU sed):这可能对您有用(GNU sed):
sed -E '/Hashmode:/{:a;x;s/^[^:]*: (\S+) -( \S+ \S+ ).*\nSpeed.*:\s*(\S+ \S+).*/\1\2\3/p;x;h;d};H;$!d;ba' file
If a line contains Hashmode
, swap to the hold space and using pattern matching, manipulate its contents to the desired format and print, swap back to the pattern space, copy the current line to the hold space and delete the current line.如果一行包含
Hashmode
,则交换到保留空间并使用模式匹配,将其内容操作为所需的格式并打印,交换回模式空间,将当前行复制到保留空间并删除当前行。
Otherwise, append the current line to the hold space and delete the current line, unless the current line is the last line in the file, in which case process the line as if it contained Hashmode
.否则,append 当前行到保持空间并删除当前行,除非当前行是文件中的最后一行,在这种情况下处理该行,就好像它包含
Hashmode
。
NB The first time Hashmode
is encountered, nothing is output. NB 第一次遇到
Hashmode
,什么都没有,output。 Subsequent matches and the end-of-file condition will be the only times printing occurs.随后的匹配和文件结束条件将是唯一发生打印的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.