Skip to content
Advertisement

Remove eveything before including colon and replace comma to newline

I am looking just to accommodate my query into one-liner.

I have a below file content where i want to remove all before last colon(:) including itself and then replace comma(,) into newline to get the desired results.

these all comma separated values are linux user ID’s

File content:

pa_tools:x:2998:nep08569,nep08623,nep14587,kpc12117,tpa06038,tpa10226,tpa14459,tpa14596,tpa21272,tpa28233,tpa32495,cpf30049,cpf32734,cpf38753,cpf41124,cpf41645,cpf42488,cpf42609,cpf54284,cpf55701,cpf56556,cpf61223,cpf64941,cpf66553,cpf69011,cpf72912,pfc37974,pfc40981,pfc48626,pfc69581,pfc80043,pfc82969,pfc84163,pfc87399,pfc87990,pfc87991,pfc98638,pa_tools_rs

Already tried:

working solution:

1- $  sed -e  's/.*://g' -e  's/,/n/g' test_g

2- $ awk -F: '{print $NF}'  test_g| awk 'BEGIN{RS=","}{$1=$1}1'

3- $ cat test_g| tr "," "n"| tr ":" "n" | sed '1,3d'

result:

$  sed -e  's/.*://g' -e  's/,/n/g' test_g
nep08569
nep08623
nep14587
kpc12117
tpa06038
tpa10226
tpa14459
tpa14596
tpa21272
tpa28233
tpa32495
cpf30049
cpf32734
cpf38753
cpf41124
cpf41645
cpf42488
cpf42609
cpf54284
cpf55701
cpf56556
cpf61223
cpf64941
cpf66553
cpf69011
cpf72912
pfc37974
pfc40981
pfc48626
pfc69581
pfc80043
pfc82969
pfc84163
pfc87399
pfc87990
pfc87991
pfc98638
pa_tools_rs

Is there a better trick, please advise.

Advertisement

Answer

You may use this solution on any version of awk:

awk -F: '{$0 = $NF; gsub(/,/, "n")} 1' file

nep08569
nep08623
nep14587
kpc12117
tpa06038
tpa10226
tpa14459
tpa14596
tpa21272
tpa28233
tpa32495
cpf30049
cpf32734
cpf38753
cpf41124
cpf41645
cpf42488
cpf42609
cpf54284
cpf55701
cpf56556
cpf61223
cpf64941
cpf66553
cpf69011
cpf72912
pfc37974
pfc40981
pfc48626
pfc69581
pfc80043
pfc82969
pfc84163
pfc87399
pfc87990
pfc87991
pfc98638
pa_tools_rs

Using gnu-sed it will something similar to your attempted code:

sed 's/.*://; s/,/n/g' file
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement