Extract part of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract part of a string
# 1  
Old 01-27-2013
Extract part of a string

I have a file with 100s of lines of text. I want to perform an extraction of this line:

Code:
Info bpzs(pid=2652) using 1000 bits

I have not been able to extract it. Should I try expr match or is there another method? This line has data both in front of and in back of it. I do not have grep -o as an option on my system. Any ideas are greatly appreciated.
# 2  
Old 01-27-2013
Code:
grep 'Info bpzs(pid=2652) using 1000 bits' file

Or, what's the complication here - which part of the string do you want to extract?
# 3  
Old 01-27-2013
Just that one line that I printed. I did try the grep, and it returns words that are both to the left and to the right of that string instead of just that string alone.

---------- Post updated at 12:38 PM ---------- Previous update was at 11:51 AM ----------

I also tried to grep for the single "word" bpzs and what I got was this:

Code:
 echo $a |grep '\<bpzs\>'

it returned bpzs but also everything else on the line. Same with grep -w. I don't have grep -o. I really want it to return just the line:

Info bpzs(pid=2652) using 1000 bits
or even bpzs(pid=2652) would work

I have also tried:

Code:
echo `expr match "$a" '\([b][p][z][s] [0-9]\)'`

But this returns "req id processing Info bpzs("

What is the proper regex to match this string?
# 4  
Old 01-27-2013
This may not be the most elegant way to accomplish want you specified, but at least, it works:
Code:
$ a="Info bpzs(pid=2652) using 1000 bits"
$ sed -n "/$a/ s:.*\($a\).*:\1:p" file
Info bpzs(pid=2652) using 1000 bits

# 5  
Old 01-28-2013
Solved!

Thanks for everyone's help on this. I was able to compose a perl command to get exactly what I needed:

Code:
|perl -ne 'print  /.*(\bInfo.bptm\(pid......).*/' | tr -d "(Info=bzspid"



# 6  
Old 01-28-2013
Code:
sed -n 's/.*\(Info.*bits\).*/\1/p' filename

If the PID could change:
Code:
sed -n 's/.*\(Info.*pid=[0-9]*.*bits\).*/\1/p' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract part of string from all log files.?

Hi All, Let us say we have 5 log files, extract the data from all log files and save the output in a file. home/log/first.log home/log/second.log home/log/third.log home/log/four.log home/log/five.log I want to extract the following text from the log files and save the output in a file.... (7 Replies)
Discussion started by: ROCK_PLSQL
7 Replies

2. UNIX for Advanced & Expert Users

Need help to extract part of the string

Hi, I have a string with name as 20140412-s1-Potopolive_promos_20140412. So I want to extract only Potopolive string. Could you please help me the command. O/p : Potopolive Thx in advance (5 Replies)
Discussion started by: lkeswar
5 Replies

3. Shell Programming and Scripting

Extract number part from the string in ksh 88

I have to extract number part (Date and timestamp part ) from the following 3 strings AB_XYZA_20130930183017.log AB_DY_XYZA_20130930183017.log AB_GZU_20130930183017.log Output should be 20130930183017 Please help me to get the string like above Thanks (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

extract part of string using sed

Hi, I have the following string and I need to extract the date from it: TextHere,2012/07/11,1 I tried using sed: sed -n 's#^.*\({4}/{2}/{2}\).*$#\1#p' But it doesn't return anything. The following line doesn't even return '2012': sed -n 's/^.*\({4}\).*$/\1/p' I used to use grep -o... (6 Replies)
Discussion started by: Subbeh
6 Replies

5. Shell Programming and Scripting

Help with extract particular part of data

Input file <data> <temporary>qe2qrqerq qwewqeqwrqwrq qrerwrewrwer </temporary> </data> <sample>@!@##%#</sample> <info>12345</info> <content>2313214141454</content> <data> <temporary>qe2qrqerq qrerwrewrwer </temporary> <content>123214214523</content> </data>... (5 Replies)
Discussion started by: perl_beginner
5 Replies

6. Shell Programming and Scripting

how to extract a certain part of a line

Hi friends, I want to select and use the certain part of a line. For example I have the following line home/1245/hgdf/acsdf/myhome/afolder/H2O/endfile how can I extract the part " /myhome/afolder/H2O/endfile " thanks (6 Replies)
Discussion started by: rpf
6 Replies

7. Shell Programming and Scripting

How to extract one part from whole output

Hi All, I am trying to write a small shell programming to get db2 database size info. The command I am going to use is- db2 "CALL GET_DBSIZE_INFO(?, ?, ?, -1)" and the output of above command generally is- Value of output parameters -------------------------- Parameter Name :... (4 Replies)
Discussion started by: NARESH1302
4 Replies

8. Shell Programming and Scripting

extract last part of string.

Hi, I have a string like this BUNDLE=/home/bob/flx/user.bun how to extract only the the last part ie, only user.bun (2 Replies)
Discussion started by: vprasads
2 Replies

9. UNIX for Dummies Questions & Answers

Extract a part of file name

Hi, I want to extract a part of filename and pass it as a parameter to one of the scripts. Could someone help. File name:- NLL_NAM_XXXXX.XXXXXXX_1_1.txt. Here i have to extract only XXXXX.XXXXXXX and the position will be constant. that means that i have to extract some n characters from... (6 Replies)
Discussion started by: dnat
6 Replies

10. Shell Programming and Scripting

Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any... (4 Replies)
Discussion started by: stakuri
4 Replies
Login or Register to Ask a Question