Print text between two delimiters


 
Thread Tools Search this Thread
Operating Systems AIX Print text between two delimiters
# 1  
Old 05-15-2009
Print text between two delimiters

Hi,

Can somebody help me with the below situation,

Input File,
========
2007_08_07_IA-0100-014_(MONTHLY).PDF
2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF
2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CSV

Required output,
============
MONTHLY
QUARTERLY
MONTH

I have a file with a million records like the above sample input. I will be grateful if somebody could help me to print the text between the first open and close braces. A little urgent please.

NOTE: There may also be other text like ANNUAL etc.,. in the braces, so please help me to print text between first "(" and ")".

Thanks in advance
# 2  
Old 05-15-2009
Code:
 # awk -F"(" '{gsub(/\).*/,"",$2);print $2}' file
MONTHLY
QUARTERLY
MONTH

# 3  
Old 05-15-2009
Thank you so much it is working fine... Smilie
# 4  
Old 05-15-2009
Code:
awk -F '[()]' '{print $2}' file

# 5  
Old 05-15-2009
Thank you funksen... The output is very fast..

Thank you all guys...
# 6  
Old 05-15-2009
Quote:
Originally Posted by sravicha
Thank you funksen... The output is very fast..

Thank you all guys...
on a large file, using -F'[()]' may not be that fast, eg experimentation on file with million lines
Code:
# wc -l < file1
1000000
# head -10 file1
2007_08_07_IA-0100-014_(MONTHLY).PDF
2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF
2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CSV
2007_08_07_IA-0100-014_(MONTHLY).PDF
2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF
2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CSV
2007_08_07_IA-0100-014_(MONTHLY).PDF
2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF
2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CSV
2007_08_07_IA-0100-014_(MONTHLY).PDF

# time awk -F '[()]' '{print $2}' file1  > /dev/null

real    0m30.326s
user    0m30.194s
sys     0m0.116s

# time awk -F"(" '{gsub(/\).*/,"",$2);print $2}' file1 > /dev/null

real    0m11.002s
user    0m10.929s
sys     0m0.064s

# time awk -F '[()]' '{print $2}' file1  > /dev/null

real    0m30.035s
user    0m29.966s
sys     0m0.044s

# time awk -F"(" '{gsub(/\).*/,"",$2);print $2}' file1 > /dev/null

real    0m11.195s
user    0m11.033s
sys     0m0.136s

# 7  
Old 05-15-2009
You can influence speed by choosing different tools. I used your code snippet to create a similar file that has about 1.2 million lines:
Code:
> head cutlist
2007_08_07_IA-0100-014_(MONTHLY).PDF
2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF
2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CS
2007_08_07_IA-0100-014_(MONTHLY).PDF
2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF
2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CS
2007_08_07_IA-0100-014_(MONTHLY).PDF
2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF
2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CS
2007_08_07_IA-0100-014_(MONTHLY).PDF
>
> wc -l cutlist
 1232454 cutlist
>

Reading the file from disk takes a fraction of a second only. The closer we get to this speed the better.
Code:
 > time cat cutlist > /dev/null

real    0m0.41s
user    0m0.04s
sys     0m0.37s

Then I used first SED to truncate the field
Code:
time sed -e 's/^.*(//' -e 's/).*//' cutlist >/dev/null

real    0m13.34s
user    0m12.36s
sys     0m0.91s

Afterwards I used a less demanding program
Code:
time cut -f2 -d"(" cutlist | cut -f1 -d")" > /dev/null

real    0m5.06s
user    0m6.02s
sys     0m0.57s

Enjoy!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

find & Replace text using two non-unique delimiters.

I can find and replace text when the delimiters are unique. What I cannot do is replace text using two NON-unique delimiters: Ex., "This html code <text blah >contains <garbage blah blah >. All tags must go,<text > but some must be replaced with <garbage blah blah > without erasing other... (5 Replies)
Discussion started by: bedtime
5 Replies

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

3. Shell Programming and Scripting

How to put delimiters in text files after fix characters?

Hi , i have a text file in which i want to put delimiters after certain characters ( fix),. like put a delimiter (any like ,) after 1-3 character than 4 than 5 than 6-17 ..... files looks like this (original)... (8 Replies)
Discussion started by: anamdev
8 Replies

4. Shell Programming and Scripting

awk: Print fields between two delimiters on separate lines and send to variables

I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc... I know the sed to get rid of... (11 Replies)
Discussion started by: tay9000
11 Replies

5. Shell Programming and Scripting

Print text between delimiters IF it contains a certain term...

So I'm racking my brain on appropriate ways to solve a problem that once fixed, will solve every problem in my life. Its very easy (for you guys and gals) I'm sure, but I can't seem to wrap my mind around the right approach. I really want to use bash to do this, but I can't grasp how I'm going to... (14 Replies)
Discussion started by: eh3civic
14 Replies

6. Shell Programming and Scripting

Order text by delimiters

I try order the content from file by delimiters. This is the text: interface Loopback0 description !!!RID RR_SLT ip address 172.31.128.19 255.255.255.255 interface GigabitEthernet0 description !!!P_SLT GI0/0/9 ip address 172.31.130.246 255.255.255.252 and the result that I need... (11 Replies)
Discussion started by: bobbasystem
11 Replies

7. Shell Programming and Scripting

Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi , could anyone help me out with this problem. sample.txt has this content : u001- this is used for project1 || u002- this is used for p2|| not to be printed u003- this is used for project3 || u004- this is used for p4 || u005- this is used for project5 || u006- this is used for p6... (9 Replies)
Discussion started by: Balaji PK
9 Replies

8. Shell Programming and Scripting

Fetch the rows with match string on a fixed lenth text file - NO delimiters

Hi I am trying to fetch the rows with match string "0000001234" Input file looks like below: 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1... (6 Replies)
Discussion started by: nareshk
6 Replies

9. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

10. UNIX for Advanced & Expert Users

extract text b/w two delimiters

I have an input file which looks like " @$SCRIPT/atp_asrmt_adj.sql $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1005w.pls $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1006w.pls start $SCRIPT/cim1020d.sql;^M spool $DATA/cim1021m.sql @$DATA/cim1021m.sql ! rm $DATA/cim1021m.sql spool $DATA/cim1021m.sql... (6 Replies)
Discussion started by: dowsed4u8
6 Replies
Login or Register to Ask a Question