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.
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 spaces/.*pattern1=([^ ,]+).*/\1\t/ replace the pattern space with pat1\tG append the original lines/\n.*pattern2=([^ ,]+).*$/\1\t/ after the newline in the pattern space, replace everything with pat2\tGives you the patterns in the order you process them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With