简体   繁体   中英

Text Manipulation using sed or AWK

I get the following result in my script when I run it against my services. The result differs depending on the service but the text pattern showing below is similar. The result of my script is assigned to var1. I need to extract data from this variable

$var1=HOST1*prod*gem.dot*serviceList : svc1 HOST1*prod*kem.dot*serviceList : svc3, svc4    HOST1*prod*fen.dot*serviceList : svc5, svc6

I need to strip the name of the service list from $var1. So the end result should be printed on separate line as follow:

svc1 
svc2
svc3
svc4
svc5
svc6

Can you please help with this?

Regards

Using gnu grep and gnu sed:

grep -oP ': *\K\w+(, \w+)?' <<< "$var1" | sed 's/, /\n/'
svc1
svc3
svc4
svc5
svc6

Using sed and grep:

sed 's/[^ ]* :\|,\|//g' <<< "$var1" | grep -o '[^ ]*'

sed deletes every non-whitespace before a colon and commas. Grep just outputs the resulting services one per line.

grep is the perfect tool for the job.

From man grep :

-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Sounds perfect!

As far as I'm aware this will work on any grep :

echo "$var1" | grep -o 'svc[0-9]\+'

Matches "svc" followed by one or more digits. You can also enable the "highly experimental" Perl regexp mode with -P , which means you can use the \\d digit character class and don't have to escape the + any more:

grep -Po 'svc\d+' <<<"$var1"

In bash you can use <<< (a Here String ) which supplies "$var1" to grep on the standard input.


By the way, if your data was originally on separate lines, like:

HOST1*prod*gem.dot*serviceList : svc1 
HOST1*prod*kem.dot*serviceList : svc3, svc4    
HOST1*prod*fen.dot*serviceList : svc5, svc6

This would be a good job for awk :

awk -F': ' '{split($2,a,", "); for (i in a) print a[i]}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM