Home > Enterprise >  Remove eveything before including colon and replace comma to newline
Remove eveything before including colon and replace comma to newline

Time:12-08

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.

CodePudding user response:

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

CodePudding user response:

Using sed

$ sed 's/.*:\([^,]*\)\|,/\n\1/g' input_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

CodePudding user response:

With sed:

sed 's/.*://; s/,/\n/g' file
# needs to be a sed the supports \n
# GNU and recent BSD do support \n but that is a superset
# of POSIX

If your sed does not support \n in replacement, then use a literal escaped new line:

sed 's/.*://; s/,/\
/g' file

Or use tr to replace , with \n:

sed 's/.*://' file | tr ',' '\n'

All three print:

nep08569
nep08623
nep14587
...
pfc87991
pfc98638
pa_tools_rs

CodePudding user response:

With

data=$(< test_g)                    # read the file
id_csv=${data##*:}                  # remove the prefix
IFS=, read -ra ids <<< "$id_csv"    # split into an array

The last 2 steps can be merged

IFS=, read -ra ids <<< "${data##*:}"

Then, output with one of:

printf '%s\n' "${ids[@]}"
(IFS=$'\n'; echo "${ids[*]}")
join() { local IFS=$1; shift; echo "$*"; }
join $'\n' "${ids[@]}"
  • Related