Sample1 Type pattern 842 3150
Sample1 Type range 842 3150
Sample1 Type pattern 842 1127
Sample1 Type option 842 1127
Sample1 Type length 1483 1603
Sample1 Type pattern 1483 1603
Sample1 Type length 1698 1747
Sample1 Type option 1698 1747
Sample1 Type length 1868 1935
Sample1 Type pattern 1868 1935
Sample1 Type length 2164 2262
Output file:
Sample1 Type pattern 842 3150
Sample1 Type range 842 3150
Sample1 Type pattern 842 1127
Sample1 Type option 842 1127
Sample1 Type length 1483 1603
Sample1 Type pattern 1483 1603
Sample1 Type length 1698 1747
Sample1 Type option 1698 1747
Sample1 Type length 1868 1935
Sample1 Type pattern 1868 1935
Sample1 Type length 2164 2262
I'm trying to added a new line before it matched that column 3 is "pattern". Thanks a lot for any advice.
sed -e 's/\(\(.* \)\{2\}\(pattern\)\)/\n\1/g' file
This will print as follows
Sample1 Type pattern 842 3150
Sample1 Type range 842 3150
Sample1 Type pattern 842 1127
Sample1 Type option 842 1127
Sample1 Type length 1483 1603
Sample1 Type pattern 1483 1603
Sample1 Type length 1698 1747
Sample1 Type option 1698 1747
Sample1 Type length 1868 1935
Sample1 Type pattern 1868 1935
Sample1 Type length 2164 2262
Hai actually the poster want to add a new line if the pattern is there in the 3rd column. But your sed script will simply match a pattern line and will add a new line before to that. kindly change it
Hi patrick87,
What is your requirement whether that pattern will in the third column or anywhere in the line.
devtakh's solution seems to correct,but its not exactly correct,because he is matching the pattern in line.He has not match the pattern with the third column.
His solution will work for the following input also.