How to Extract text between two strings?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Extract text between two strings?
# 8  
Old 07-06-2011
@Franklin52

Thanks for you reply

But still it is not working, it is not giving any output.

please explain me this command also, how it will work?

and I want to extract between two strings(string1 and string2 )

Thanks
# 9  
Old 07-06-2011
Quote:
Originally Posted by emresearch
@Franklin52

Thanks for you reply

But still it is not working, it is not giving any output.

please explain me this command also, how it will work?

and I want to extract between two strings(string1 and string2 )

Thanks
Works fine for me:
Code:
$ cat file
line1
line2
aaa -bbb -ccc -string1 c,d,e -string2
line4
$ sed -n 's/.*string1 \([^ ]*\) .*/\1/p' file
c,d,e

Post a better sample of your input file and please use code tags.
# 10  
Old 07-06-2011
Code:
sed -n '/\-string1.*\-string2$/p' inputfile | awk '{print $(NF-1)}'


Last edited by Franklin52; 07-06-2011 at 07:21 AM.. Reason: Please use code tags for code and data samples, thank you
# 11  
Old 07-06-2011
My file content is----
Code:
#interpreter
cd directory
spotlight -verilog file.v -$regression -batch -policy=xyz -rules ABC,abc,pqr -wdir ../other_files
cd ...

In this case i want to extract strings after -rules till the next space.-ABC,abc,pqr

I am using first awk command to find the line--
Code:
awk '/-rules/'{print} > output

because awk print complete line ----

so now output file will have only line which is having -rule, now i have to extract string after -rule till the next space.

As i am new to shell scripting so please explain your answer also.

Thanks
Manish

Last edited by Franklin52; 07-06-2011 at 07:21 AM.. Reason: Please use code tags for code and data samples, thank you
# 12  
Old 07-06-2011
I wonder why you didn't try the sed solution posted by Franklin52 or me! Can you make necessary changes to sed and try again.
Code:
sed -n '/rules/s/.*rules \([^ ]*\) .*/\1/p' inputfile


Last edited by michaelrozar17; 07-06-2011 at 07:21 AM.. Reason: posted sed command..
# 13  
Old 07-06-2011
@Franklin52

@michaelrozar17

I am using the same command again and again but its is not working.

Please go through my input files once then reply me for the same.

or if you have any alternative solution for this task.

My input file is ----
Code:
Script---

#interpreter
cd directory
spotlight -verilog file.v -$regression -batch -policy=xyz -rules ABC,abc,pqr -wdir ../other_files
cd ...

First i am using awk command----
Code:
$ awk '/-rules/{print}' script > output

after this i am using the sed command provided by you like---
Code:
$ sed -n '/rules/s/.*rules \([^ ]*\) .*/\1/p' output > out1

now out1 file is empty.
it means sed command is not working properly in this case.

Thanks
Manish

Last edited by Franklin52; 07-06-2011 at 07:48 AM.. Reason: Please use code tags for code and data samples, thank you
# 14  
Old 07-06-2011
Quote:
Originally Posted by emresearch
My input file is ----
Code:
Script---

#interpreter
cd directory
spotlight -verilog file.v -$regression -batch -policy=xyz -rules ABC,abc,pqr -wdir ../other_files
cd ...

after this i am using the sed command provided by you like---
Code:
$ sed -n '/rules/s/.*rules \([^ ]*\) .*/\1/p' output > out1

now out1 file is empty.
it means sed command is not working properly in this case.
Ok.Can you post the content of the file output as highlighted above. If its same as the highlighted in bule please post few more lines of the inputfile to sed command. And post the sed version you have.Command: sed --version (in GNU Linux)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need grep regex to extract multiline text between two strings

I have a file conatining the below: --- 10.9.16.116: /tmp/5835113081224811756.jar: hash: e6df90d38fa86f0e289f73d79cd2cfd2a29954eb /tmp/4603745991442278706.jar: hash: e6df90d38fa86f0e289f73d79cd2cfd2a29954eb 10.9.14.126: /tmp/conf/extra/httpd-ssl.conf: hash:... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

3. Shell Programming and Scripting

Extract text between two strings

Hi, I have a text like these: ECHO "BEGGINING THE SHELL....." MV FILE1 > FILE2 UNIQ_ID=${1} PARTITION_1=`${PL}/Q${CON}.KSH "SELECT ....." PARTITION_2=`${PL}/Q${CON}.KSH "SELECT ........" ${PL}/Q${CON}.KSH "CREATE ...." IF .... ....... I would like to extract only text that only... (4 Replies)
Discussion started by: mierdatuti
4 Replies

4. Shell Programming and Scripting

Extract text between two strings

Hi I have something like this: EXAMPLE 1 CREATE UNIQUE INDEX "STRING_1"."STRING_2" ON "BOSNI_CAB_EVENTO" ("CD_EVENTO" , "CD_EJECUCION" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 5242880 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "DB1000_INDICES_512K"... (4 Replies)
Discussion started by: chrispaz81
4 Replies

5. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

6. Shell Programming and Scripting

AWK: How to extract text lines between two strings

Hi. I have a text test1.txt file like:Receipt Line1 Line2 Line3 End Receipt Line4 Line5 Line6 Canceled Receipt Line7 Line8 Line9 End (9 Replies)
Discussion started by: TQ3
9 Replies

7. UNIX for Dummies Questions & Answers

Using awk/sed to extract text between Strings

Dear Unix Gurus, I've got a data file with a few hundred lines (see truncated sample)... BEGIN_SCAN1 TASK_NAME=LA48 PDD Profiles PROGRAM=ArrayScan 1.00 21.220E+00 2.00 21.280E+00 END_DATA END_SCAN1 BEGIN_SCAN2 TASK_NAME=LA48 PDD Profiles 194.00 2.1870E+00 ... (5 Replies)
Discussion started by: tintin72
5 Replies

8. UNIX for Dummies Questions & Answers

extract text between two strings exclusive

Hi, I'm trying to retrieve text between two strings excluding the two strings. So for example I have the following input: type=friend username=phone1 secret=password host=dynamic dtmfmode=rfc2833 mailbox=9664 context=sip insecure=very canreinvite=no nat=yes realm=192.168.1.122... (3 Replies)
Discussion started by: the1armedcoder
3 Replies

9. Shell Programming and Scripting

using awk to extract text between two constant strings

Hi, I have a file from which i need to extract data between two constant strings. The data looks like this : Line 1 SUN> read db @cmpd unit 60 Line 2 Parameter: CMPD -> "C00071" Line 3 Line 4 SUN> generate Line 5 tabint>ERROR: (Variable data) The data i need to extract is... (11 Replies)
Discussion started by: mjoshi
11 Replies

10. Shell Programming and Scripting

using AWK how to extract text between two same strings

I have a file like: myfile.txt it is easy to learn awk and begin awk scripting and awk has got many features awk is a powerful text processing tool Now i want to get the text between first awk and immediate awk not the third awk . How to get it ? its urgent pls help me and file is unevenly... (2 Replies)
Discussion started by: santosh1234
2 Replies
Login or Register to Ask a Question