A co-worker came to me with a problem I thought would be trivial, but turned out to be challenging. The challenge is this: given an input string, what Unix commands can be used to print out any matching patterns within the string?
Let’s say we have the following input string.
12345 4444 abc 789012 xyz 1234567 def 987654321 qrz 60606
The goal is to print out any 5-digit or 6-digit numbers within the string, but not any 4-digit or 7-digit numbers.
We first thought of using sed like so:
echo "12345 4444 abc 789012 xyz 1234567 def 987654321 qrz 60606" |
sed 's/.*[^0-9]\{1\}\([0-9]\{5,6\}\).*/\1/g'
This command, however, only prints out the last occurrence of any matching patterns.
We finally came up with using a combination of sed and grep -Eo.
echo "12345 4444 abc 789012 xyz 1234567 def 987654321 qrz 60606" |
sed 's/^/ /' | sed 's/$/ /' | grep -Eo '[[:space:]]+[0-9]{5,6}[[:space:]]+' |
sed 's/ $//' | sed 's/^ //'
It works but seem a bit kludgy. Is there a better way?
How about
trandgrep?Or as dmckee suggests, you can use the
-oflag to grep to skip thetrstage (if your version ofgrephas this flag):