How to print few strings in a line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print few strings in a line?
# 8  
Old 12-10-2012
try sth like this

Code:
$ 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


Last edited by pamu; 12-10-2012 at 06:13 AM.. Reason: corrected..
# 9  
Old 12-10-2012
Code:
 
sed 's/^Successfully added //;s/ to the Content Manager//' input.txt

This User Gave Thanks to itkamaraj For This Post:
# 10  
Old 12-10-2012
Thanks Itkamaraj .It works fine. Could you please explain me how its working.I didn't get this part

Code:
//;s/

# 11  
Old 12-10-2012
two substitute operations are performed on the file.

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

This also can be splitted like...

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

This User Gave Thanks to itkamaraj For This Post:
# 12  
Old 12-10-2012
awk alternative
Code:
awk -F'Successfully added | to the Content' 'NF>2{print $2}'

This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 12-10-2012
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.
# 14  
Old 12-13-2012
Even ksh can do this:
Code:
cat file | while read x
do
 x2="${x#Successfully added }"
 x3="${x2% to the Content Manager}"
 if [ "$3" != "$x" ]
 then
  echo "$x3"
 fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print strings from a particular position in each line

I am using bash in Fedora 30 From the below lines (ls -l output), how can I print whatever is between the strings 'status_' and '.log' $ ls -l | grep -i status -rw-rw-r--. 1 sysadmin sysadmin 378530 Nov 11 21:58 status_vsbm1.log -rw-rw-r--. 1 sysadmin sysadmin 428776 Nov 11 21:58... (8 Replies)
Discussion started by: kraljic
8 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Programming

Print only some strings from an output

Hi, Here is an example: I have a grep line: grep -i -r -H "$WORD" "$DIRECTORY"with an output like this: /media/dir/dir2//dir4/file.txt:/media/dir/dir2/dir3/file_16072008/es6.txt: "content of the file found from grep"/media/dir/dir2/dir3/dir4/file3.txt:/media/dir/dir2/dir3//file.txt:"other... (3 Replies)
Discussion started by: Hornys
3 Replies

4. Shell Programming and Scripting

[Help me!] Print text between two strings

Deal All, I have problem for this: input file : "data.txt" R 240 585694.59946146.8 8.0 239 585694.09946134.3 8.0 238 585693.59946121.8 8.01R 237 585693.09946109.3 8.0 236 585692.59946096.9 8.0 235 585692.19946084.4 8.01R 234 585691.59946071.9 8.0 233 585691.09946059.5 8.0 232... (2 Replies)
Discussion started by: aksin
2 Replies

5. Shell Programming and Scripting

[Help me!] print text between two strings

Deal All, I have problem for this: input file : "data.txt" R 240 585694.59946146.8 8.0 239 585694.09946134.3 8.0 238 585693.59946121.8 8.01R 237 585693.09946109.3 8.0 236 585692.59946096.9 8.0 235 585692.19946084.4 8.01R 234 585691.59946071.9 8.0 233 585691.09946059.5 8.0 232... (2 Replies)
Discussion started by: aksin
2 Replies

6. UNIX for Dummies Questions & Answers

Get strings on a file and print

hi all! i have a file like this lea 25 female dave 18 male jake 27 male and i want to have an output file like this my name is lea. i am 25. female my name is dave. i am 18. male my name is jake. i am 27. male thanks! (2 Replies)
Discussion started by: engr.jay
2 Replies

7. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

8. Shell Programming and Scripting

print specific strings only

Hello, I have a file like this.. 2 168611167 STK39 STK39 --- 27347 "serine threonine kinase 39 (STE20/SPS1 homolog, yeast)" YES SNP_A-2086192 rs16854601 0.001558882 6 13670256 SIRT5 /// RPS4X SIRT5 --- 23408 /// 6191 "sirtuin (silent mating type... (5 Replies)
Discussion started by: genehunter
5 Replies

9. Shell Programming and Scripting

Print all the lines between 2 specified strings

Hi All, I have a file in which i want to print all the lines between 2 defined strings. Ex- I have file with data as follows STEP1:- ----- has some 20 -30 lines of data STEP2:- ----- has some 20 -30 lines of data So i want to print those lines between STEP1 & STEP2. (line including STEP1)... (7 Replies)
Discussion started by: digitalrg
7 Replies

10. Shell Programming and Scripting

Print all between 2 strings

Hi All, I'm working on a large file and need to extract all data between 2 strings. I have seen many good solutions to threads almost like my problem but none that quite fit. This is all very new to me so any ideas would be really appreciated! (attempted to read sed and awk tutorials but got a... (9 Replies)
Discussion started by: soots
9 Replies
Login or Register to Ask a Question