1827286 Members
2984 Online
109717 Solutions
New Discussion

Re: Help for script...

 
SOLVED
Go to solution
PSS SYS ADMIN
Super Advisor

Help for script...

Hi everyone,
I need to get a tape id from an output file.
In the output file I have a line like this:
Medium : [9d1c8029:3d888e10:0f0b:0001]

How can I "grep" only the code between the squares?

Regards...
PSS
6 REPLIES 6
Larry Reinhart
Advisor
Solution

Re: Help for script...

Try this:

grep "^Medium" output_file | awk -F\[ '{print $2}' | awk -F\] '{print $1}'

Hope this helps.
Have a fun day!
Larry
harry d brown jr
Honored Contributor

Re: Help for script...

You don't. Grep doesn't "extract" strings, it matches on strings!

You need to use something like awk, perl, sed, ...

Try this:

sed "s/^\(Medium : \[\)\(.*\)\(\]$\)/\2/"

live free or die
harry
Live Free or Die
John Palmer
Honored Contributor

Re: Help for script...

You could use sed:-

ID=$( | sed -e 's/.*\['// -e 's/].*//')

This removes all text up to the first [ and after the final ].

You could also use the shell itself:-

| read DATA
DATA=${DATA#*\[} # remove text up to the first [
ID=${DATA%]} #remove the final ] and all after it

Regards,
John
Geoff Wild
Honored Contributor

Re: Help for script...

Here's 1 way:

grep Medium "output file" | awk -F[ '{print $2}' |awk -F] '{print $1}'


Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
harry d brown jr
Honored Contributor

Re: Help for script...

So basicly you use grep to extract the LINE out of the file, then sed to EXTRACT the STRING:

cat thatoutputfilename |
grep "^Medium " |
sed "s/^\(Medium : \[\)\(.*\)\(\]$\)/\2/"

returns:

9d1c8029:3d888e10:0f0b:0001

live free or die
harry
Live Free or Die
Rodney Hills
Honored Contributor

Re: Help for script...

If you have perl (which all admins should have)-

perl -ne '/^Medium/ && print((split("[\\[\\]]",$a))[1]);'

or

perl -ne '/^Medium\s*:\s*\[(.*)\]/ && print $1'

HTH

-- Rod Hills

There be dragons...