Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed : extract multiple fields in random order

I have a paragraph as below

Hello, this is pattern1=somedata and pattern2=someotherdata and something else after this along with pattern3=data_again and pattern4=wellstilldata and nothing more.

And I am able to extract those patterns values with,

sed -r 's/.*pattern1=([^ ,]+).*pattern2=([^ ,]+).*pattern3=([^ ,]+).*pattern4=([^ ,]+).*/\1\t\2\t\3\t\4/' file.txt

but there are some cases where pattern will be in random positions , I mean there is no condition as such always pattern1 should come first and pattern4 should come at last and pattern string may not be pattern always but it will be one of the given pattern in above sed logic. their positions may vary but my above logic of extracting multiple fields from paragraph will only work if they are in order or else I have to re arrange them in order everytime.

But is there any we can modify above logic to work like irrespective of position of patterns and string is not "pattern" always but they will be in my sed logic.

Thank you.

like image 360
rɑːdʒɑ Avatar asked Jan 27 '26 05:01

rɑːdʒɑ


1 Answers

Assuming you want the patterns output in sequential order separated by tabs, you can stuff the matching line into the hold space, and then process each pattern sequentially:

sed -r -e 'h;s/.*pattern1=([^ ,]+).*/\1\t/;G;s/\n.*pattern2=([^ ,]+).*$/\1\t/;G;s/\n.*pattern3=([^ ,]+).*$/\1\t/;G;s/\n.*pattern4=([^ ,]+).*$/\1\t/;'

$ echo blahpattern2=pat2,blahpattern1=pat1 blahpattern4=pat4,pattern3=pat3 blah | sed -r -e 'h;s/.*pattern1=([^ ,]+).*/\1\t/;G;s/\n.*pattern2=([^ ,]+).*$/\1\t/;G;s/\n.*pattern3=([^ ,]+).*$/\1\t/;G;s/\n.*pattern4=([^ ,]+).*$/\1\t/;'
pat1    pat2    pat3    pat4    
  • h hold the line in the hold space
  • s/.*pattern1=([^ ,]+).*/\1\t/ replace the pattern space with pat1\t
  • G append the original line
  • s/\n.*pattern2=([^ ,]+).*$/\1\t/ after the newline in the pattern space, replace everything with pat2\t
  • etc

Gives you the patterns in the order you process them.

like image 128
stevesliva Avatar answered Jan 29 '26 19:01

stevesliva