Operating System - HP-UX
1833790 Members
2606 Online
110063 Solutions
New Discussion

Re: Strings from command output

 
SOLVED
Go to solution
Igor Sovin
Super Advisor

Strings from command output

Hi!
What is the way to get several strings from some hp-ux command and write them to file?
for example
#more filename
ab
bc
cd
ed

I need to read "ab" and "ed" and write it to filename1.

19 REPLIES 19
Sandman!
Honored Contributor
Solution

Re: Strings from command output

Igor,

Try this awk construct...it'll help if you could give a sample of the input that needs processing.

# | awk '{print $1}'

cheers!
john korterman
Honored Contributor

Re: Strings from command output

Hi Igor,

there are several ways, e.g. grep, assuming the file "filename" is in the current directory:

# grep -E "^ab|^ed" ./filename >newfile

which will match lines beginning with ab or ed

If you want to match the two strings anywhere in a line, then:

# grep -E "ab|ed" ./filename >newfile

But you will probably get many other suggestions!

regards,
John K.
it would be nice if you always got a second chance
Igor Sovin
Super Advisor

Re: Strings from command output

Hi!

thank you for advice, I've cheked it, but I need more complex output.
The command is the following
#omnimm -media_info BM2605L1 -detail
MediumID : 0a01010b:412b055d:1f33:0001
Pool name : NW3-Pool
Library : Ultrium
Medium Label : [BM2605L1] NW3-Pool_4
: [Ultrium: 1]
Medium Owner : lev
Used blocks : 470891648
Total blocks : 470891648
Number of writes : 14
Number of overwrites : 15
Number of errors : 0
Creation time : Tue Aug 24 15:07:41 2004
Time of last write : Tue Apr 11 22:02:57 2006
Time of last overwrite : Wed Apr 5 22:37:33 2006
Time of last access : Tue Apr 11 22:02:57 2006
Medium type : FULL
Write-protected : No

I need to get Pool name: Library: Medium Label: Location: into one string, so that every word will be the heading of the separate column. And the value of each parameter acoording to command outpu must be written to column below the heading.
Igor Sovin
Super Advisor

Re: Strings from command output

Hi!

thank you for advice, I've cheked it, but I need more complex output.
The command is the following
#omnimm -media_info BM2605L1 -detail
MediumID : 0a01010b:412b055d:1f33:0001
Pool name : NW3-Pool
Library : Ultrium
Medium Label : [BM2605L1] NW3-Pool_4
: [Ultrium: 1]
Medium Owner : lev
Used blocks : 470891648
Total blocks : 470891648
Number of writes : 14
Number of overwrites : 15
Number of errors : 0
Creation time : Tue Aug 24 15:07:41 2004
Time of last write : Tue Apr 11 22:02:57 2006
Time of last overwrite : Wed Apr 5 22:37:33 2006
Time of last access : Tue Apr 11 22:02:57 2006
Medium type : FULL
Write-protected : No

I need to get pframeters Pool name: Library: Medium Label: Location: into one string, so that every parameter will be the heading of the separate column. And the value of each parameter acoording to command output must be written to column below the heading.
SANTOSH S. MHASKAR
Trusted Contributor

Re: Strings from command output

Hi,

Try this

$ |awk '($1 == "ab" || $1 == "ed"){print $1}' > file_name


-Santosh
Sandman!
Honored Contributor

Re: Strings from command output

Hi Igor,

Based on the sample input you'ave provided...try this awk construct.

# omnimm -media_info BM2605L1 -detail | awk -F: '
> /^Pool name/||/^Library/||/^Medium Label/{head=head" "$1;val=val" "$2}
> END{printf("%s\n%s\n",head,val)}'

I don't see any record that starts with Location so I left it out of the awk pattern, however you can easily extend it.

cheers!
Peter Godron
Honored Contributor

Re: Strings from command output

Igor,
or as a cumbersome shell script:
#!/usr/bin/sh
omnimm -media_info BM2605L1 -detail > a.dat
grep -f a.grep a.dat > a.rep
while read record
do
heading=`echo $record | cut -d':' -f1`
data=`echo $record | cut -d':' -f2`
heado="${headi}\t$heading"
headi=$heado
datao="${datai}\t$data"
datai=$datao
done < a.rep
echo $headi
echo $datai

where a.grep contains:
Pool name :
Library :
Medium Label :
Igor Sovin
Super Advisor

Re: Strings from command output

Thanx Sandman!
It works perfect!
and the last question:

What expression I must use to get only BM2605L1
from the string
Medium Label : [BM2605L1] NW3-Pool_4

without signs '[' and ']' ?
James R. Ferguson
Acclaimed Contributor

Re: Strings from command output

Hi Igor:

This will produce a nicely formatted table:

# omnimm -media_info BM2605L1 -detail | perl -nle 'printf "%-25s %s\n",$1,$2 if /(.+)\s+:\s+(.+)/'

Regards!

...JRF...


Igor Sovin
Super Advisor

Re: Strings from command output

James
I get the same result as the output of command omnimm without any awk expressions. The difference is that using your example I get the same outpur but without ':' sign.
Peter Nikitka
Honored Contributor

Re: Strings from command output

Hi,

based on sandman:
# omnimm -media_info BM2605L1 -detail | awk -F: '
> /^Pool name/||/^Library/||/^Medium Label/{head=head" "$1;if ($2 ~ "]") add=substr($2,2,length($2)-2)
else add=$2
val=val" "add}
> END{printf("%s\n%s\n",head,val)}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
john korterman
Honored Contributor

Re: Strings from command output

Hi again,

you can cut your string like this:

# LABEL="Medium Label : [BM2605L1] NW3-Pool_4"
# REMOVE_START=$(echo ${LABEL##Medium Label : \[})
# echo ${REMOVE_START%%\]*}
BM2605L1

regards,
John K.
it would be nice if you always got a second chance
Igor Sovin
Super Advisor

Re: Strings from command output

john, it works
but how to get this result from omnimm output that I provided above?
Igor Sovin
Super Advisor

Re: Strings from command output

Or how to get only all BM**** from this output

# omnimm -repository_barcode_scan Ultrium
[Normal] From: UMA@lev "Ultrium" Time: 04/13/06 14:26:48
STARTING Media Agent "Ultrium"

[Normal] From: UMA@lev "Ultrium" Time: 04/13/06 14:26:53
COMPLETED Media Agent "Ultrium"

[Normal] From: MSM@lev "Ultrium" Time: 04/13/06 14:26:53

Slot [side] Medium type Medium Label (ID)
===============================================================================
1 Data Protector [BM2605L1] NW3-Pool_4
2 Data Protector [BM2609L1] DMProd-Pool_1
3 Data Protector [BM2584L1] DMProd-Pool_3
4 Data Protector [BM2592L1] PRS-Week_3
5 Data Protector [BM2606L1] NW3-Pool_1
6 Data Protector [BM2572L1] SRD-Week_2
7 Data Protector [BM2573L1] DMTest-Pool_1
8 Data Protector [BM2571L1] DMTest-Pool_2
9 Data Protector [BM2574L1] NW3-Pool_2
10 Data Protector [BM2570L1] NW3-Pool_3
11 Data Protector [BM2595L1] NPU_6
12 Data Protector [BM2616L1] NW2-Pool_4
13 Data Protector [BM2617L1] NPU_1
14 Data Protector [BM2618L1] Ignite_Pool_3
15 Data Protector [BM2647L1] NW3-Pool_5
16 Data Protector [BM2619L1] Ignite_Pool_2
17 Data Protector [BM2625L1] MFS-Pool_7
18 Data Protector [BM2599L1] MFS-Pool_6
19 Data Protector [BM2627L1] NW2-Pool_2
john korterman
Honored Contributor

Re: Strings from command output

Hi again,

assuming the file infile2 holds the BM***s, you can try this:

# cut -d" " -f ,4 ./infile2| tr -d [\[\]]

alternatively, you can read through the file one line at a time:

#!/usr/bin/sh
while read a b c d e
do
REMOVE_START=${d#\[}
echo ${REMOVE_START%\]}
done<$1

run it like this:
# abovescript.sh infile2



regards,
John K.
it would be nice if you always got a second chance
Peter Nikitka
Honored Contributor

Re: Strings from command output

Hi Igor,

you can use this:
awk '$4 ~ "[[]BM" {print substr($4,2,length($4)-4)}' filename

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: Strings from command output

correction: not 'lenght-4' but '-2':

awk '$4 ~ "[[]BM" {print substr($4,2,length($4)-2)}' filename
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Strings from command output

Hi Igor:

My apologies for my first post. I read your requirements too quickly. With regard to "...how to get only all BM**** from this output...":

# perl -nle 'print $1 if /\[(BM.+)\]/' datafile

(or):

# | perl -nle 'print $1 if /\[(BM.+)\]/'

Regards!

...JRF...
Igor Sovin
Super Advisor

Re: Strings from command output

I have found a solution to this question