Home > Software design >  Grep two strings in same line
Grep two strings in same line

Time:10-01

Could you please help me? Look this file below:

[Parsed_showinfo_0 @ 0x55d39a569e00] n:48591 pts:24878592 pts_time:2024.62 pos:120292092 fmt:yuv420p sar:1/1 s:640x360 i:P iskey:0 type:B checksum:292B9CE1 plane_checksum:[F3F98FB2 02828690 02828690] mean:[20 128 128 ] stdev:[27.0 0.0 0.0 ]
[Parsed_showinfo_0 @ 0x55d39a569e00] n:48592 pts:24879104 pts_time:2024.67 pos:120291950 fmt:yuv420p sar:1/1 s:640x360 i:P iskey:0 type:B checksum:0FB59CD7 plane_checksum:[6F918FA8 02828690 02828690] mean:[20 128 128 ] stdev:[27.0 0.0 0.0 ]
[Parsed_showinfo_0 @ 0x55d39a569e00] n:48593 pts:24879616 pts_time:2024.71 pos:120292235 fmt:yuv420p sar:1/1 s:640x360 i:P iskey:0 type:B checksum:292B9CE1 plane_checksum:[F3F98FB2 02828690 02828690] mean:[20 128 128 ] stdev:[27.0 0.0 0.0 ]
[Parsed_showinfo_0 @ 0x55d39a569e00] n:48594 pts:24880128 pts_time:2024.75 pos:120291807 fmt:yuv420p sar:1/1 s:640x360 i:P iskey:0 type:P checksum:7CF59CE1 plane_checksum:[47D28FB2 02828690 02828690] mean:[20 128 128 ] stdev:[27.0 0.0 0.0 ]
[Parsed_showinfo_0 @ 0x55d39a569e00] n:48595 pts:24880640 pts_time:2024.79 pos:120292664 fmt:yuv420p sar:1/1 s:640x360 i:P iskey:0 type:B checksum:292B9CE1 plane_checksum:[F3F98FB2 02828690 02828690] mean:[20 128 128 ] stdev:[27.0 0.0 0.0 ]
...

Using this command =>

grep showinfo file.txt | grep pts_time:[0-9.]* -o | grep '[0-9]*\.[0-9]*' -o > timestamps

I get correctly all pts_time values. But I want to capture "n:X" too! Using grep showinfo file.txt | grep pts_time:[0-9.]* -o | grep '[0-9]*\.[0-9]*' -o | grep n:[0-9.]* -o | grep '[0-9]*[0-9]*' do not work! There are no errors. The output should be something like this:

48591/2024.62
48592/2024.67
48593/2024.71
48594/2024.75
...

Also, to avoid creating a new thread, would be possible convert the second argument (2024.62, 2024.67, etc) to human readable? In this format => hours:minute:seconds.miliseconds? Thank you.

CodePudding user response:

grep is not the best solution to this as it is more natural to search for lines matching a pattern with grep but extract data from a line with another tool such as awk

Here would be a solution using awk:

awk -F'[: ]' '{print $5"/"$9}' file.txt

where each line is split on or : and then the 5th column and 9th columns are what you are trying to extract.

awk can also handle converting seconds to HH:MM:SS, see https://unix.stackexchange.com/a/34033

CodePudding user response:

A solution that is more robust to varying ordering of the columns would be:

grep showinfo file.txt | grep -E 'pts_time:[0-9.] |n:[0-9] ' -o | sed '$!N;s/\n/ /' | perl -lape '$_ = qq/@{[sort @F]}/' | awk -F'[: ]' '{print $2"/"$4}'

Which:

  1. Gets the relevant lines
  2. Uses extended grep to or match the two columns
  3. Use sed to merge those columns to merge the pairs of results on to a single line each (https://stackoverflow.com/a/1513918/1581658)
  4. Use perl to sort those lines alphabetically so out of order keys are always ordered. (https://stackoverflow.com/a/47520200/1581658)
  5. Use the above solution awk not with fixed columns to format (https://stackoverflow.com/a/69391284/1581658)

The awk can be replaced with awk -F'[: ]' '{printf "%d/d:d:.2f\n",$2, $4/60/60, $4/60`, $4` }' as in (https://unix.stackexchange.com/a/34033) to format HH:MM:SS.ss

CodePudding user response:

With sed and sort

sed -En '/showinfo/s/^.* n:([[:digit:]]{1,}).* pts_time:([[:digit:].]{1,}).*/\1\/\2/p' file.txt | sort

  •  Tags:  
  • bash
  • Related