What's a good way to extract foo.开发者_运维百科com from the string using awk?
ImaString (foo1.com, ,bar.com) (foo2.com, , bar.com)
Output i want: foo1.com foo2.com
If you have more than one field in the comma separated lists:
awk -F, -v RS="[)(]" 'NF>1{print $1}' inputfile
Apologies for the snarky comment, but the question is not clearly worded. You might try setting FS to the regex "(|," and let awk extract the field for you automatically:
$ awk -F ',|\\(' ...
Question is still a bit unclear, are you asking for this?
$ echo "ImaString (foo.com, ,bar.com) (foo.com, , bar.com)" |
awk -F '(' '{sub(/,.*/,"", $2);{print $2}}'
foo.com
Are you sure you want to use awk for this? Sed and grep would seem to be a better choice for this.
$ echo "ImaString (foo1.com, ,bar.com) (foo2.com, , bar.com)" |
grep -E -o '\(([a-zA-Z0-9]*\....),' | sed -e 's/^.//' -e 's/,$//'
foo1.com
foo2.com
Dump the results into a bash variable, count them with wc, or stick them in a bash array.
精彩评论