Grep two strings in same line

Ruan Matt

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.

SamBob

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/%02d:%02d:%05.2f\n",$2, $4/60/60, $4/60%60, $4%60 }' as in (https://unix.stackexchange.com/a/34033) to format HH:MM:SS.ss

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Match two strings in one line with grep

Print multi-line strings on same line

How to grep for two words existing on the same line?

How to pad two strings on the same line in a checkedListBox C#?

Why two strings in Mysql are the same?

Two div on the same line

show no of occurences of two strings in same line on access logs

How to grep two numbers from the same line at different places using bash?

Two divs in the same line

How to grep same strings by comparing two files

grep two strings from a file and write into a txt

Grep/sed between two strings

Grep searching two words in a line

How to grep for 2 strings (AND condition) within a block which are not in the same line and then find something else within that same block

How to grep two strings in line by specific order AND calculating values line by line according to my grep

Use grep to find strings with consecutive same letters

Using grep to return two instances from a list of options in the same line

Grep two strings from a curl response, output them to a file on the same line

Find all lines in a file that have two strings on the same line

getting a string between two strings not in the same line, regex

Grepping all strings on the same line

Using grep to match where two tokens occur on the same line

Search a file using two strings on the same line from another file with grep or another tool

Two paragraphs in the same line

How to grep for multiple strings in the same line inside a file with similar pattern?

Trying to search two strings in a document in the same line

Placing two strings in center of page for Python on the same line

Grep all strings that start and end with the same substring

Print next word after pattern match for two strings in same line for one file in se