Print out a selected word.

Hi can anyone assist me on my problem.
I try to grep 1 word in 1 line data. Example like below.

  • Data below located in a.txt, i just wanna grep just processing-time = "12"

total-octets = "20080718214210Z" total-pages = "" octets-completed = "20080721064351Z" pages-completed = "2" processing-time = "12"

I searched the threat and found 1 solution like below, unfortunately it is just work if the word located in different row not in 1 line row.

sed -ne '/^[ ]*processing-time/s#^\([^=][^=]*\)=\([^/][^/]*\).*#\12#p'

Please assits :frowning: thanks.

sed 's/.*\(processing-time = \"[^"]*\"\).*/\1/g'

Thanks it is work for me.
1 more, how about if i need to grep 2 words octets-completed = "20080721064351Z" and processing-time = "12" it is located in same 1 file in 1 line data, as below.

sed 's/.*\(octets-completed = \"[^"]*\"\).*\(processing-time = \"[^"]*\"\).*/\1 \2/g'

You may get something useful from piping Your search through sed this way:

echo 'total-octets = "20080718214210Z" total-pages = "" octets-completed = "20080721064351Z" pages-completed = "2" processing-time = "12"'|sed -e 's/ = /=/g;s/ /\n/g'

resulting in

total-octets="20080718214210Z"
total-pages=""
octets-completed="20080721064351Z"
pages-completed="2"
processing-time="12"

which then can be used in a script or something...

/Lakris

Thanks Guys, for your help, it is very helpful for me now.
:slight_smile:

Hi Lakris,

I tried to used your method using the pipe after the echo but it wouldn't work. Sorry if it is my mistake i am new in this shell field. Below is what i did

My Code :

Result I get :

If your code are working means i can just sort it more cleanly using row, as the usefull output data. :slight_smile:

Works for me, but perhaps your sed is braindead. Try replacing \n with \x0A.

i tried using \x0A it is same, it replacing from creating new row with char 'x0A' :smiley: my OS running on HPUX. is there are any different for the OS argument option in SED?

Inserting newlines in most versions of sed require you to do something like the following

sed -e 's/ = /=/g;s/ /\
/g' 

Here is a short example:

#!/usr/bin/ksh

TMP=file.$$

cat <<EOT >$TMP
total-octets = "20080718214210Z" total-pages = "" octets-completed = "20080721064351Z" pages-completed = "2" processing-time = "12"
EOT

sed -e 's/ = /=/g;s/ /\
/g' $TMP

rm $TMP

exit 0

Yes I think it may be sed/shell version issue, I only use bash and sed on Linux.

/Lakris