问题
I have a file with many lines
http://example.com/part-1 this number 1 one
http://example.com/part--2 this is number 21 two
http://example.com/part10 this is an number 12 ten
http://example.com/part-num-11 this is an axample number 212 eleven
How can I remove all character after "number x" + between first columd and "number x"...I wanna my output like this
http://example.com/part-1 1
http://example.com/part--2 21
http://example.com/part10 12
http://example.com/part-num-11 212
Another case : Input:
http://server1.example.com/00/part-1 this number 1 one
http://server2.example.com/1a/part--2 this is section 21 two two
http://server3.example.com/2014/5/part10 this is an Part 12 ten ten ten
http://server5.example.com/2014/7/part-num-11 this is an PARt number 212 eleven
I wanna the same output....And the number is always in last numeric field
回答1:
sed -r 's/^([^0-9]*[0-9]+)[^0-9]*([0-9]+).*/\1 \2/' file
Output:
http://example.com/part-1 1 http://example.com/part--2 21 http://example.com/part10 12 http://example.com/part-num-11 212
回答2:
Here is one way:
awk -F"number" '{split($1,a," ");split($2,b," ");print a[1],b[1]}' file
http://example.com/part-1 1
http://example.com/part--2 21
http://example.com/part10 12
http://example.com/part-num-11 212
If the number you like to have is always on the second last field, this should do too:
awk '{print $1,$(NF-1)}' file
http://example.com/part-1 1
http://example.com/part--2 21
http://example.com/part10 12
http://example.com/part-num-11 212
回答3:
Try this:
sed 's/ .*number \([0-9]+\).*/ \1/' myfile.txt
回答4:
Thank everyone...From your comments, I have my own solution :
sed -re 's/([0-9]*[0-9]+)/#\1#/g' | sed -re 's/(^.*#).*/\1/g' | sed 's/#//g' | awk '{print $1" "$NF}'
My idea : Replace all numeric group with #[numbers]# , then select all character from start of line to "#" (sed will select last # ) and remove all rest character. Next is awk
Thank everyone (y)
来源:https://stackoverflow.com/questions/27211825/remove-all-character-after-matched-character