简体   繁体   中英

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

Here is what I tried. The complete file is called complete.txt

cat complete.txt | grep Hashmode | awk '{print $2,$4,$5}' > mode.txt

Output:

 13761 VeraCrypt PBKDF2-HMAC-SHA256 

Then:

cat complete.txt | grep Speed.# | awk '{print $2,$3}' > speed.txt

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'

I also have a feeling that this can be done using a much simpler command with sed or 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. (Failing to print the last one is a common beginner bug.)

This might work for you (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.

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 .

NB The first time Hashmode is encountered, nothing is output. Subsequent matches and the end-of-file condition will be the only times printing occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM