tags:

views:

34

answers:

4

I have the following XML output when I grep for "Server":

<Server id="1" src="/other/Server/PRX01/PRX01.xml"/>
<Server id="2" src="/other/Server/PRX01/PRX02.xml"/>
<Server id="3" src="/other/Server/PRX01/PRX03.xml"/>
<Server id="4" src="/other/Server/PRX01/PRX04.xml"/>

I need to be able to take this output and sed/awk or some other tool, and just get the filename, without the path or extension. So my output would need to be (for this example):

PRX01
PRX02
PRX03
PRX04
+1  A: 

simple to do with awk and sed, assuming the data is in the file "test.data":

cat test.data | awk 'BEGIN{FS="/"}{print $5}'  | sed 's/\..*//g'
ennuikiller
Exactly what I needed - thanks! :)
drewrockshard
+2  A: 

For the example input data, the following sed script will work:

sed -e 's/.*\/\(.*\)\.xml.*/\1/g' t.tmp

The .*\/ matches up to a forward slash (greedy). Then \(.*\)\.xml matches the last of the line and grabs the base file name in a group. The \1 tells it to substitute all of that for what was in the group.

Mark Wilkins
A: 

the accepted answer can be simplified without the useless cat and sed,

awk '{gsub(/\..*/,"",$5) ;print $5}' file
ghostdog74
A: 
>gawk -F"/" "{ split($5,a,\".\"); print a[1]}" 1.t
PRX01
PRX02
PRX03
PRX04
Vijay Sarathi