Operating System - HP-UX
1833744 Members
2809 Online
110063 Solutions
New Discussion

Shell Script - Parsing Question

 
SOLVED
Go to solution
Praveen Hari
Advisor

Shell Script - Parsing Question

I have a file of format

[0:2] x.x.a start=3 end=50 items=47 aaa bbb ccc
[3:4] x.x.b start=20 end=70 items=50 aaa bbb ..


I want to parse this and create a new file with the format

x.x.a 50
x.x.b 70

Please note that I want to extract the second string and number in 4th string and ignore rest of the line and write into a new file.
How do I do it in Shell Scripts ?
I am a beginner in Shell Scripts..
Any help is appreciated..
9 REPLIES 9
Hai Nguyen_1
Honored Contributor
Solution

Re: Shell Script - Parsing Question

Try this

# sed 's/end=//' your_file | awk '{print $2 " " $4}' > new_file

output is in new_file

Hai
Praveen Hari
Advisor

Re: Shell Script - Parsing Question

It worked. Thanks
Hai Nguyen_1
Honored Contributor

Re: Shell Script - Parsing Question

Try another way:

# cut -d" " -f2,4 your_file | sed 's/end=//' > new_file

Hai
Praveen Hari
Advisor

Re: Shell Script - Parsing Question

It worked. Thanks
Praveen Hari
Advisor

Re: Shell Script - Parsing Question

It worked. Thanks
James R. Ferguson
Acclaimed Contributor

Re: Shell Script - Parsing Question

Hi:

# awk -F"=" 'awk -F= {split($1,a," ");X=a[2];split($4,a," ");print X,a[1]}' inputfile

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Shell Script - Parsing Question

Hi (again):

Oops. that should be:

# awk -F"=" 'awk -F= {split($1,a," ");X=a[2];split($3,a," ");print X,a[1]}' inputfile

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Shell Script - Parsing Question

Hi (again):

Oops. that should be:

# awk -F"=" 'awk -F= {split($1,a," ");X=a[2];split($3,a," ");print X,a[1]}' inputfile

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Shell Script - Parsing Question

# perl -pe's/^\s*\S+\s+(\S+)\s+\S+\s+.*?(\d+).*/$1 $s/' infile

or if the 'end=' is always 'end=', and there will never be leading whitespace

# perl -pe's/^\S+\s+(\S+).*?end=(\d+).*/$1 $2/' inile

It's not always awk/sed/sh :))

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn