negate search help

Hi, I've tried a lot of negate codes in this forum, but they do not perform what I intended. Please help.

inputfile:

Paragraph1 contents: die1, die2, die3, pr_name1, pr_name2
pr_name3, pr_name4

Paragraph2 more contents: die1, die2, die3, pr_name1, pr_name2
pr_name3, pr_name4

outputfile:

pr_name1
pr_name2
pr_name3
pr_name4

pr_name1
pr_name2
pr_name3
pr_name4

The 2 codes that I've found so far are:
sed -ne '/Paragraph1/s/Paragraph1 contents: die1, die2, die3, pr/pr/p' inputfile > outputfile
This only give me pr_name1, pr_name2. I want to modify this code sed -ne '/Paragraph./s/Paragraph.(anything in between)pr/pr/p' inputfile > outputfile

awk '/Paragraph1/{print $NF}' inputfile > outputfile
This only give me pr_name2.

thanks.

$ cat infile 
Paragraph1 contents: die1, die2, die3, pr_name1, pr_name2
pr_name3, pr_name4

Paragraph2 contents: die1, die2, die3, pr_name1, pr_name2
pr_name3, pr_name4

$ perl -00>outfile  -lne's/, /\n/g;print/((?:.*?\n*){4})$/' infile 
$ cat outfile 
pr_name1
pr_name2
pr_name3
pr_name4

pr_name1
pr_name2
pr_name3
pr_name4

try somethig like this

sed -e 's/, /\
/g' -e '/^pr/!d' infile

Thanks, Radoulov for the perl script. I'm using .bashrc and perl is much more complicated. I think your code is looking at the 4th field after the last comma. My report varies so it's not always the 4th field, but I forgot to mention that :slight_smile:

Vidyadhar85, your codes works perfectly. Can you please explain how it works? I tried to combine the 2 lines together (sed -e 's/, /\/g' -e '/^pr/!d' infile) and of course it didn't work. hehe. I only thing that I understand is the first line look for the last comma using the -e option, but I dont understand the /\ part. Then the next line negate delete all words that don't start with ^pr. Please explain more in details so I can leverage your code in the future. Thanks.

basically it enteres a new line after each ", "
so i am searching for ", " and then inserting newline by hitting an enter after search thats why it went to next line so it won't work in single line
regards,
vidyadhar

Vidyadhar85, Thanks for the explaination. Leverage from your codes, I have modified it for anyone who might find this useful. I used:

sed -e 's/, /\n/g' -e '/pr/!d' -e 's/contents: /\n/g' input > temp
sed -e '/pr/!d' temp > output