Added new line before a specific pattern problem asking

Input 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

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.

Hai!!! you can try the following sed command

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

Try this,

sed -r "s/(^.+ +.+ +pattern .+$)/\n\1/g" file

Sed:

sed  '/pattern/{N;s/^/\n/}' filename

cheers,
Devaraj Takhellambam

Thanks a lot, devtakh.
Your sed code worked perfectly for my case.
Thanks again for sharing :slight_smile:

you can try this

sed '/pattern/{x;p;x}' file.txt

:slight_smile:

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.

Sample1 pattern 842 3150
Sample1 range 842 3150
Sample1 pattern 842 1127
Sample1 option 842 1127
Sample1 length 1483 1603
Sample1 pattern 1483 1603
Sample1 length 1698 1747
Sample1 option 1698 1747
Sample1 length 1868 1935
Sample1 pattern 1868 1935
Sample1 length 2164 2262
           Here the pattern is in the second column.

Awk solution...

awk '$3=="pattern"{print "\n"$0;next}1' infile

Try:

awk '/pattern/&&NR>1 { print RS; }NR' file

If specific to a column, ignoring extra line if matches in the first line itself.

sed  '2,$s/\([^ ]*\)\( .*\)\( pattern\)\(.*\)/\n\1\2\3\4/' file

cheers,
Devaraj Takhellambam

Try this script,

#!/bin/sh
while read line
do
        val=` echo $line | cut -d " " -f 3 `
        if [[ $val == "pattern" ]]
        then
                echo -e "\n$line"
        else
                echo $line
        fi
done < inputfile

Hi malcomex999,
Your awk solution worked perfectly to my case too :slight_smile:
Thanks a lot.

If you are looking for appatern in third col, try:

awk '$3 ~ /pattern/&&NR>1 { print RS"\b" ;} NR ' file