How to print the text between two strings in unix.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to print the text between two strings in unix.
# 1  
Old 03-13-2012
How to print the text between two strings in unix.

Hi Team,

Would you please help me for the below scenario.

I want to print the text between "PREF:" AND "AVAIL:" in the below example.

For example:-


TEST_TAF PREF: RAC1 RAC2 RAC3 ...... AVAIL: RAC4

Output will be :-RAC1,RAC2,RAC3.............

Thanks in Advance

Shoan


Moderator's Comments:
Mod Comment Please use next time code tags for your code and data
Smilie

Last edited by vbe; 03-13-2012 at 10:05 AM..
# 2  
Old 03-13-2012
Do you only have exactly 1 occurrence of each within one line ?

if yes, then

Code:
sed 's/.*PREF//;s/AVAIL.*//' yourfile

# 3  
Old 03-13-2012
Hi Team,

Thanks!! But It will helpful if the output comes with ","(comma) in between.

Output:-RAC1,RAC2,RAC3

Thanks
Shoan
# 4  
Old 03-13-2012
Code:
$ cat input
TEST_TAF PREF: RAC1 RAC2 RAC3 AVAIL: RAC4
TEST_TAF PREF: RAC1 RAC2 RAC3 AVAIL: RAC4
$
$ perl -lne '(/PREF: (.+?) AVAIL/) && print join(",", (split /\s+/,$1))' input
RAC1,RAC2,RAC3
RAC1,RAC2,RAC3
$

# 5  
Old 03-13-2012
Code:
sed 's/.*PREF: //;s/ AVAIL.*//;s/  */,/g' yourfile

# 6  
Old 03-14-2012
Hi ,

Great!! Thanks a lot .It is working fine.


Thanks
Shoan
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print text between 2 strings for the entire file

hey guys, for the following output: starting open open close close starting close starting open close close starting open open close open (2 Replies)
Discussion started by: boaz733
2 Replies

2. Shell Programming and Scripting

Print text between 2 identical strings

hey, i m having a hard time trying to print only the first occurrence between 2 idenicale strings. for the following output: please help me im a noob please im a noob help me noob please help me im a noob please im a noob help me noob (3 Replies)
Discussion started by: boaz733
3 Replies

3. UNIX for Dummies Questions & Answers

How to create a print filter that print text & image?

Currently, I have a print filter that takes a text file, that convert it into PCL which then gets to a HP printer. This works. Now I need to embedded a image file within the text file. I'm able to convert the image file into PCL and I can cat both files together to into a single document... (1 Reply)
Discussion started by: chedlee88-1
1 Replies

4. 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

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. 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

7. 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

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 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

10. UNIX for Dummies Questions & Answers

help to print text file in unix system

I am using SCO Unix to print text file. But the word size is too large, it is always over page (A4) when I print. Does Any one know how to use command change word size and make it fit on the page to print text file. I use command: lp -d (printer name) textFileName (16 Replies)
Discussion started by: wendyz
16 Replies
Login or Register to Ask a Question