How to extract portion of a string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract portion of a string?
# 1  
Old 04-04-2012
Question How to extract portion of a string?

Hi Gurus,

Would like to seek some help on how to extract a portion of string from log's output as shown below.

Sample of raw data:
Code:
piece handle=/test123/disk_dump/test123/df0_cntrl_PCPFCI20120404_68498 tag=TAG20120404T180035 comment=NONE
piece handle=/test123/disk_dump/test123/df0_arc_PCPFCI20120404_68499 tag=TAG20120404T180045 comment=NONE
piece handle=/test123/disk_dump/test123/df0_arc_PCPFCI20120404_68500 tag=TAG20120404T180045 comment=NONE
piece handle=/test123/disk_dump/test123/df0_arc_PCPFCI20120404_68501 tag=TAG20120404T180045 comment=NONE
piece handle=/test123/disk_dump/test123/df0_spfile_PCPFCI20120404_68518 tag=TAG20120404T192714 comment=NONE

Expected output:
Code:
df0_cntrl_PCPFCI20120404_68498
df0_arc_PCPFCI20120404_68499
df0_arc_PCPFCI20120404_68500 
df0_arc_PCPFCI20120404_68501 
df0_spfile_PCPFCI20120404_68518

Is there a magic 1 line script or command that could perform this task?

Appreciate for any of your help / advise.

Thanks a lot.


- Peter

Last edited by Scrutinizer; 04-04-2012 at 03:08 PM.. Reason: Code tags
# 2  
Old 04-04-2012
awk is a handy shell language which splits its inputs on columns -- by default on whitespace. But, usefully, you can tell it to use anything you want as its field separator(FS) -- including spaces, equals, and forward slashes. Then you can just print the appropriate column.

Code:
$ echo "piece handle=/test123/disk_dump/test123/df0_cntrl_PCPFCI20120404_68498 tag=TAG20120404T180035 comment=NONE" |
        awk -v FS="[ =/]" '{ print $7 }'

df0_cntrl_PCPFCI20120404_68498

$ awk -v FS="[ =/]" '{ print $7 }' inputfile

df0_cntrl_PCPFCI20120404_68498
df0_arc_PCPFCI20120404_68499
df0_arc_PCPFCI20120404_68500
df0_arc_PCPFCI20120404_68501
df0_spfile_PCPFCI20120404_68518

$

Use nawk on Solaris.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-04-2012
A variation:
Code:
awk -v FS="[ =]" '{ print $3 }' inputfile | xargs -L1 basename

(should handle variable depths of path)
This User Gave Thanks to CarloM For This Post:
# 4  
Old 04-04-2012
You can also try the following:

Code:
echo "piece handle=/test123/disk_dump/test123/df0_cntrl_PCPFCI20120404_68498 tag=TAG20120404T180035 comment=NONE" | cut -d " " -f 2 |cut -d "/" -f 5

Output:

Code:
df0_cntrl_PCPFCI20120404_68498

So, if you assume that the above data is contained in file named data.txt, then you can also use the following:

Code:
cut -d " " -f 2 data.txt | cut -d "/" -f 5

It will give the desired output.
This User Gave Thanks to Epsilon For This Post:
# 5  
Old 04-05-2012
Hi All,

Thanks for your reply. Appreciate it.

Is it possible to extract the string based on a keyword such as: df0 ?

Thanks.


- Peter
# 6  
Old 04-05-2012
Code:
$ awk -F"[ /]" '/df0/ {print $6}' input.txt
df0_cntrl_PCPFCI20120404_68498
df0_arc_PCPFCI20120404_68499
df0_arc_PCPFCI20120404_68500
df0_arc_PCPFCI20120404_68501
df0_spfile_PCPFCI20120404_68518

---------- Post updated at 01:00 PM ---------- Previous update was at 12:58 PM ----------

Code:
 
$ awk -F"[ /]" '{for(i=1;i<=NF;i++)if($i~/df0/){print $i}}' input.txt
df0_cntrl_PCPFCI20120404_68498
df0_arc_PCPFCI20120404_68499
df0_arc_PCPFCI20120404_68500
df0_arc_PCPFCI20120404_68501
df0_spfile_PCPFCI20120404_68518

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 04-05-2012
Code:
sed 's|.*/||;s| .*||' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract a portion of string from each line in Linux

Hi I have to extract the destination path information from each record the file is of variable length so I will not be able to use the print command.The search should start on variable "destinationPath" and it should end at immediate "," also the first field has to be printed Input File:... (7 Replies)
Discussion started by: rkakitapalli
7 Replies

2. Shell Programming and Scripting

Extract portion of data

Hi Gurus, I need some help in extracting some of these information and massage it into the desired output as shown below. I need to extract the last row with the header in below sample which is usually the most recent date, for example: 2012-06-01 142356 mb 519 -219406 mb 1 ... (9 Replies)
Discussion started by: superHonda123
9 Replies

3. Shell Programming and Scripting

Extract portion of log info based on specific word

Hi Gurus, I'm using HP-UX B.11.23 operating system. I've been trying to extract a specific wording for example: "A tool used by tp produced warnings" from my below log data, but could not find a way to solve it. My intention is, if the log contain the word: "A tool used by tp produced... (9 Replies)
Discussion started by: superHonda123
9 Replies

4. Shell Programming and Scripting

How to extract a text portion from a file

Can some one help me with shell script to extract a text block between two known strings. The given input file is as below: Name: abs Some tesxt.... Some tesxt.... Some tesxt.... end of text Name: xyz Some tesxt.... Some tesxt.... Some tesxt.... end of text Name: efg Some... (5 Replies)
Discussion started by: ejazs0
5 Replies

5. Shell Programming and Scripting

extract string portion from filename using sed

Hi All, I posted something similar before but I now have a another problem. I have filenames as below TOP_TABIN240_20090323.200903231830 TOP_TABIN235_1_20090323.200903231830 i need to extract the dates as in bold. Using bash v 3.xx Im trying to using the print sed command but... (11 Replies)
Discussion started by: santam
11 Replies

6. Shell Programming and Scripting

extract string portion using sed

Hi All I have 3 files as listed below and highlighted in bold the portions of the filenames I need to extract: TOS_TABIN218_20090323.200903231830 TOS_TABIN219_1_20090323.200903231830 TOS_TABIN219_2_20090323.200903231830 I tried source_tabin_name=`echo $fname | sed 's/_.*//'` but I... (6 Replies)
Discussion started by: santam
6 Replies

7. UNIX for Dummies Questions & Answers

How to extract a portion of text from a log file

I am using Unix on Mac OS X 10.5.6. I am trying to extract the last entry of a log (text) file. As seen below, each log entry looks like the following (date and time change with each log entry): I want the script to extract everything quoted above, including the "===" dividers. ... (2 Replies)
Discussion started by: atilano
2 Replies

8. Shell Programming and Scripting

extract date portion from file

Hi, I have a file where there is a date field (single line variable length file) how to extract just the date portion from it the position of date field may vary anywhere in the line but will always have the format mm-dd-yyyy for eg . xxxxxxxxxxxxxxx09-10-2006xxxxxxxxxxxxxxxxxxxx (5 Replies)
Discussion started by: misenkiser
5 Replies

9. Shell Programming and Scripting

extract a portion of log file

hello, I want to grep the log file according to time and get the portion of log from one particular time to other. I can grep for individual lines by time but how should I print lines continuously from given start time till end till given end time. Appreciate your ideas, Thanks chandra (8 Replies)
Discussion started by: chandra004
8 Replies

10. UNIX for Dummies Questions & Answers

How to extract a portion of a string from the whole string

How to extract a portion of a string from a full string using unix. For example: Say source string is = "req92374923.log" I want only the numeric portion of the string say "92374923" how to do that in Unix. (2 Replies)
Discussion started by: ds_sastry
2 Replies
Login or Register to Ask a Question