How to print few strings in a line?

I have one script which gave final output as the below statment.

Successfully added Nomination Petition Manifest to the Content Manager.

Now i want a sed statement which will search for this statement in any given file and print only Nomination Petition Manifest. I don't want to hardcore the value like using awk

awk '{ print $3,$4,$5 }'

.

Any help will be greatly appreciable.

Thanks

$ sed -n 's/^Successfully added \(.*\) to the Content Manager\.$/\1/p' <your_files>
1 Like

Iam not getting any output after using the below code.

cat vikram.txt
Successfully added Nomination Petition Manifest to the Content Manager
 sed -n 's/^Successfully added \(.*\) to the Content Manager\.$/\1/p' vikram.txt

Try this:

awk '/^Successfully added/{ print $3,$4,$5 }' vikram.txt

Thanks for the reply Frankiln but i already tried this .I know its working but the thing is that iam not sure that this line will be having a 3 word report or 4 thats why iam searching for something which is in between the entire line.It should print in between added and to of this line.It may be a 3 words or 4 words etc.

Regarding the difference with the sed command: Your original sample contains a trailing dot, which DGPickett's suggestion takes into account. Replace \.$ with .* and it should work.

1 Like

Thanks Scrutinizer.It worked fine.

However i didn't use .*

sed -n 's/^Successfully added \(.*\) to the Content Manager/\1/p' vikram.txt
Nomination Petition Manifest

try sth like this

$ cat file
Successfully added Nomination Petition Manifest to the Content Manager.
Successfully added Nominati on Petition Manifest to the Content Manager.
Successfully added Nomination Peti tion Mani fest to the Content Manager.

$ awk '{for(i=1;i<=NF;i++){if($i == "added" || $i == "to"){a++};if(a && a < 2){s=s?s FS $i:" "}}print s;s=a=""}' file
  Nomination Petition Manifest
  Nominati on Petition Manifest
  Nomination Peti tion Mani fest
 
sed 's/^Successfully added //;s/ to the Content Manager//' input.txt
1 Like

Thanks Itkamaraj .It works fine. Could you please explain me how its working.I didn't get this part

//;s/

two substitute operations are performed on the file.

 
s/^Successfully added //
s/ to the Content Manager//

This also can be splitted like...

sed -e 's/^Successfully added //' -e 's/ to the Content Manager//' input.txt
1 Like

awk alternative

awk -F'Successfully added | to the Content' 'NF>2{print $2}'
1 Like

Thanks Itkamaraj. I got it.

Thanks Scrutinizer . I always prefer sed over awk but its good to know that awk could be use very much efficiently.

Even ksh can do this:

cat file | while read x
do
 x2="${x#Successfully added }"
 x3="${x2% to the Content Manager}"
 if [ "$3" != "$x" ]
 then
  echo "$x3"
 fi
done