Reputation: 315
input:
random abcD .and$ 5487>1.2.3.4:random fghij & 9101112
output:
1.2.3.4
What I have done so far:
sed -e 's/'>'\(.*\)':'/\1/'
I have also tried:
sed -e 's/>\(.*\):/\1/'
note: I can do it with awk:
awk 'BEGIN { FS = ">" } ; { print $2 }' | awk 'BEGIN { FS = ":" } ; { print $1 }'
but it's a little bit heavy, isnt it?
Upvotes: 1
Views: 43
Reputation: 408
if there are other chars ":" and ">" in the line, and you are sure that the substring to match contain only "." and digitis [ 0 to 9 ], it will be better to use :
sed "s/.*>\([0-9|.][0-9|.]*\):.*/\1/g"
or better :
sed "s/.*>\([0-9|.]\+\):.*/\1/g"
or ( to match the first one occurence ) :
sed "s/.*>\([0-9|.]\+\):.*/\1/"
you can also use:
grep -o ">[0-9|.]*:" | grep -o "[0-9|.]*"
test :
echo "random > abcD .and$ 5487>1.2.3.4:random : fghij > & 9101112" | sed "s/.*>\([0-9|.][0-9|.]*\):.*/\1/g"
1.2.3.4
echo "random > abcD .and$ 5487>1.2.3.4:random : fghij > & 9101112" | sed "s/.*>\([0-9|.]\+\):.*/\1/"
1.2.3.4
Upvotes: 0
Reputation: 133680
Solution 1st: This simple sed
may help you on same.
sed 's/.*>//;s/:.*//' Input_file
Solution 2nd: Adding awk
solution too now.
awk '{gsub(/.*>|:.*/,"")} 1' Input_file
Solution 3rd: Using another awk
with field separator logic.
awk -F'[>:]' '{print $2}' Input_file
Solution 4th: Using another sed
with back references logic.
sed 's/\(.*>\)\([^:]*\)\(.*\)/\2/' Input_file
Upvotes: 2