Find word in file then get following characters

Hello,

I have several xml files from which I want to find and return a particular string
I want to locate the InId="00000008". Now that is inlcuded within a tag and ofcourse the number is different every time

this is what I came up with given that after greping the line that contains the InId it is placed always fourth with spaces seperating the information.

 
ls $1 | grep 'ANI*' > list
while read line
do
 grep 'inID="' $line | cut -d ' ' -f4 > tmp
 InvID=`grep 'inID="' tmp`
 
 echo $line " ---- " $InvID
 
done < list

this worked fine outputing the filename and the inID until I found out several of the files did not inlcude the inID at the fourth place but somwhere else.

I need to find a way to grep that "inID=" and the following 8 character ID no matter their position in the file

I tried grep -A 9 which should return "inID="00000008" but it seems AIX does not support -A for grep.

Any ideas how to do this? either in awk or sed or other way

many thanks in advance

Post a sample data from your input file.

 
<?xml version="1.0" encoding="UTF-8"?>
<Header>
    <From>
        <Credential
            domain="ID">
    <Identity>680</Identity>
 
    </Credential>
 
    </From>
    <To>
        <Credential
            domain="ID">
    <Identity>908</Identity>
 
    </Credential>
 
    </To>
    <Sender>
        <Credential domain="ID">
    <Identity>001</Identity>
 
    Credential>
        <UserAgent>Test</UserAgent>
    </Sender>
 
</Header>
<Request Mode="test"><InDetailRequest><InDetailRequestHeader Date="2011-04-06T00:00:00-03:00" inID="00000008" inOrigin="test" >
</InDetailRequestHeader>
</InDetailRequest>
    </Request>
</cXML>

not tested...

nawk ' /inID/ {for(i=1;i<=NF;i++){if($i~/inID/) {print $i}}' xmlfile
1 Like

Hi,
just try this,

grep 'inID' $line | nawk '{split($0,a,"inID=\"");b=substr(a,0,8);print b;}'

cheers,
Ranga:-)

1 Like

Had a missing } at the end but worked like a charm! Thank you very much

for the record I also tried

 
sed -n '/inID="/,/" /p' $line

but with no luck

nevertheless many thanks again

Try this...

sed -n '/inID/s/.*\(inID="[0-9]*"\) .*/\1/p' $line