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:

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:

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

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.

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

1 Like

A variation:

awk -v FS="[ =]" '{ print $3 }' inputfile | xargs -L1 basename

(should handle variable depths of path)

1 Like

You can also try the following:

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

Output:

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:

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

It will give the desired output.

1 Like

Hi All,

Thanks for your reply. Appreciate it.

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

Thanks.

  • Peter
$ 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 ----------

 
$ 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

1 Like
sed 's|.*/||;s| .*||' infile
awk -v FS="[ =]" '/df0/ { print $3 }' inputfile | xargs -L1 basename

or if you want to pass the value in from a script:

srch="df0";
awk -v FS="[ =]" -vval=$srch '$0 ~ val { print $3 }' inputfile | xargs -L1 basename
1 Like
awk -F"[ /]" '{print $(NF-2)}' inputfile
1 Like
awk '{$1=$1}NF=NF>2' RS=/ infile

Some awks:

awk 'NF=NF>2' RS=/ infile
2 Likes

Nice tricky last one ! Thx !

Hi All,

Great.

Thanks for your responses and help.

Let me test it tomorrow and let you know.

  • Peter

---------- Post updated 04-06-12 at 05:00 AM ---------- Previous update was 04-05-12 at 06:59 AM ----------

Hi All,

Would like to add that I'm using Solaris system as shown below.

-> uname -a
-> SunOS 5.10 Generic_144488-08 sun4u sparc SUNW,SPARC-Enterprise

I've tried most the given awk commands, but seems to be having some issues, see below.

root # awk -v FS="[ =]" '/df0/ { print $3 }' test1.txt  | xargs -L1 basename
awk: syntax error near line 1
awk: bailing out near line 1

---
root # srch="df0";
root # awk -v FS="[ =]" -vval=$srch '$0 ~ val { print $3 }' test1.txt | xargs -L1 basename
awk: syntax error near line 1
awk: bailing out near line 1

---
root # awk -F"[ /]" '{print $(NF-2)}' test1.txt
awk: trying to access field -1
 record number 1

---

Did I miss out anything?

Appreciate for your advise.

Thanks.

  • Peter

Don't use /usr/bin/awk on Solaris. Use nawk or /usr/xpg4/bin/awk instead.

1 Like

Hi All,

Thanks for your responses.

It's working as expected. Great.

Appreciate for your help.

  • Peter