Operating System - HP-UX
1829159 Members
2094 Online
109986 Solutions
New Discussion

Shell script to get a piece of data from a file.

 
Sushant Gupta
Occasional Contributor

Shell script to get a piece of data from a file.

I want to retrieve the data which is in 3 diffrent xml tags from an xml file using shell script.
7 REPLIES 7
Muthukumar_5
Honored Contributor

Re: Shell script to get a piece of data from a file.

Can you give the data which you want to extract through with an example.

Use grep or perl or sed or awk to get it done. Post to help you out.

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Shell script to get a piece of data from a file.

Hello,

You can simply use "grep" to do that.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Sushant Gupta
Occasional Contributor

Re: Shell script to get a piece of data from a file.

I want to get "0400004250" from the given string "0400004250" here the data within the tags keeps varying
Peter Godron
Honored Contributor

Re: Shell script to get a piece of data from a file.

Hi,
how about:
#!/usr/bin/sh
x="0400004250"
y=`echo $x | cut -d'>' -f2`
echo `echo $y | cut -d'<' -f1`

First cut eliminates everything before the ">"
Second cut elimintates everything following the "<"
Steven E. Protter
Exalted Contributor

Re: Shell script to get a piece of data from a file.

Shalom Sushant,

#!/usr/bin/ksh
# user the shell of choice
> outputfile
string1="hello"
string2="goodbye"
string3="whatever"

grep $string1 inputfile >> outputfile
grep $string2 inputfile >> outputfile
grep $string3 inputfile >> outputfile

You could use Korn shell array processing to make it prettier.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Cesare Salvioni
Trusted Contributor

Re: Shell script to get a piece of data from a file.

hi

let'say that xmlfile is the file containing the string you want to extract. You can use something like:

awk '
/^.*<\/ABCDE>$/ {
gsub(/<\/*ABCDE>/, "")
print
}' < xmlfile

the above command will:
1. extract ALL lines with the given tag ()
2. the tag part will be deleted and only the string inside the tag will be printed
3. the lines must contain ONLY the tag to be extracted, meaning that a line like
first tagthis tag
will NOT be extracted. This is for the use of character ^ and $ in the regexp pattern used to address the awk command
If you need to extract the given part of tag from all lines you need something more complex like:
awk '
/.*<\/ABCDE>/ {
gsub(/^.*/, "")
gsub(/<\/ABCDE>.*$/, "")
print
}' < xmlfile

In general see awk man to manage text files inside a script: awk is really a powerfull and efficient tool to work on text file.
You can do almost everything with it. :-)

hope it helps
Sushant Gupta
Occasional Contributor

Re: Shell script to get a piece of data from a file.

Used java instead of shell script.
Thanks to all for help.