Reputation: 2071
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
Upvotes: 0
Views: 975
Reputation: 67221
>gawk -F"/" "{ split($5,a,\".\"); print a[1]}" 1.t
PRX01
PRX02
PRX03
PRX04
Upvotes: 0
Reputation: 342363
the accepted answer can be simplified without the useless cat and sed,
awk '{gsub(/\..*/,"",$5) ;print $5}' file
Upvotes: 0
Reputation: 41222
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.
Upvotes: 2
Reputation: 46965
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'
Upvotes: 1