Reputation:
I am trying to remove few metacharacter or special chars from mu command line output with sed, awk or tr or any handy though fast utility. Moreover , i have tried few tricks at my level which are working but in multiple addition of commands.. just what i got below..
1) The first One the exact Output which the command yields..
$ getent netgroup login_access_foxone
login_access_foxone (-,cacti,) (-,multi,) (-,hipot,) (-,jack,)
2) below is the sed thing which combined with awk to yield the output` desired.
$ getent netgroup login_access_foxone | sed 's/login_access_onefarm//g' | sed 's/)/\n/g' | awk 'BEGIN {FS=","}{print $2}' | grep -v ^$
cacti
multi
hipot
jack
3) The next below One with tr command but again had to use cut to get the output..
$ getent netgroup login_access_foxone | tr -d '(' | tr ')' '\n' | cut -d, -f2
cacti
multi
hipot
jack
4) This is with awk but again into two parts
$ getent netgroup login_access_foxone | awk -v FS="[()]" '{for (i=2;i<=NF;i+=2) print $i }' | awk -F, '{print $2}'
cacti
multi
hipot
jack
5) Few More test with sed but right now unable to remove "-" & "," and get newline after each name
$ getent netgroup login_access_foxone | sed 's/[(),]//g'
login_access_foxone -cacti -multi -hipot -jack
$ getent netgroup login_access_foxone | sed 's|[()-]||g'
login_access_foxone ,cacti, ,multi, ,hipot, ,jack,
6) tried sed with some ugly combination but there is proble with output which contains whitespaces..
$ getent netgroup login_access_foxone | sed 's/[-,()]/\n/g'
login_access_foxone
cacti
multi
hipot
jack
I got it the idea for blank lines to be deleted with below code but agian need to have sed with another pipe..
$ getent netgroup login_access_foxone | sed 's/[-,()]/\n/g'|sed '/^\s*$/d'
Upvotes: 1
Views: 790
Reputation: 7959
The shortest solution I can think of:
grep -Po ',\K\w*'
Example:
grep -Po ',\K\w*' <<< 'login_access_foxone (-,cacti,) (-,multi,) (-,hipot,) (-,jack,)'
cacti
multi
hipot
jack
Explanation:
\K
: look-behind
\w
: [a-zA-Z_]
*
: Any number of matches
Upvotes: 0
Reputation: 15461
You can try this sed, but it's quite verbose compared to awk :
sed 's/[^(]*(-,\([^,]*\),)/\1\n/g;s/\n$//' file
Upvotes: 1
Reputation: 67527
awk
to the rescue!
$ awk -F, '{for(i=2;i<=NF;i+=2) print $i}' file
cacti
multi
hipot
jack
or
$ awk -v RS=, '!(NR%2)' file
cacti
multi
hipot
jack
in the second alternative, we split each line into mini records separated by comma. Since the required fields are between two commas, we'll print out the even numbered records, meaning record number divisible by two. The order is naturally preserved (no surprises there).
Upvotes: 3