0

I have the following lines in a text file:

1 Q0 /home/nikol123/Downloads/Ergasia_1/Ergasia_1/metadata/13/120411.xml 1 
1 Q0 /home/nikol123/Downloads/Ergasia_1/Ergasia_1/metadata/11/105016.xml 2 
1 Q0 /home/nikol123/Downloads/Ergasia_1/Ergasia_1/metadata/15/149972.xml 3 
1 Q0 /home/nikol123/Downloads/Ergasia_1/Ergasia_1/metadata/12/110688.xml 4 

and I want to keep only these data:

1 Q0 120411 1 
1 Q0 105016 2 
1 Q0 149972 3 
1 Q0 110688 4 

namely to keep from each line from the path /home/nikol123/Downloads/Ergasia_1/Ergasia_1/metadata/13/120411.xml for example only the number 120411 and so on...

2

Try awk with /, ., and (space) as a field separators:

awk -F'[/. ]' '{print $1 " " $2 " " $11 " " $13}' file

Output:

1 Q0 120411 1
1 Q0 105016 2
1 Q0 149972 3
1 Q0 110688 4
  • Great!That works! – user88582 Oct 20 '14 at 21:44
1

Try sed:

sed 's/\(1 Q0\).*\/\(.*\)\.xml\( .*\)/\1 \2\3/' file

OR

sed 's/\(.*\)\/home.*\/\(.*\)\.xml\( .*\)/\1\2\3/' file

Output:

1 Q0 120411 1 
1 Q0 105016 2 
1 Q0 149972 3 
1 Q0 110688 4
0

My solution with sed (OSX & BSD):

sed -E 's!([0-9]*) (Q0) [a-zA-Z0-9_/]*/([0-9]*).xml ([0-9])!\1 \2 \3 \4!g' file.txt 

After if you need different pattern (like Q0 is not a constant...) you have to adapt it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.