0

My base.csv file has several entries a sample of which is below.

NAME,IP_ADDRESS,PRI_IP,APP_CODE,APP_NAME,OWNER
test2,10.9.7.12/10.222.101.11,10.9.9.12,545,myapp1,owner1
mytest,10.9.7.12,10.9.9.12,545,myapp1,owner9
checkthis,10.9.7.12/10.222.101.11/10.3.2.111,10.9.9.12,545,myapp1,owner7

Note: the second column is always a single or multiple IP address seperated by delimiter /

I wish to create newbase.csv with second colum always having only a single ip.

Thus the desired newbase.csv would be like

test2,10.9.7.12,10.9.9.12,545,myapp1,owner1
test2,10.222.101.11,10.9.9.12,545,myapp1,owner1
mytest,10.9.7.12,10.9.9.12,545,myapp1,owner9
checkthis,10.9.7.12,10.9.9.12,545,myapp1,owner7
checkthis,10.222.101.11,10.9.9.12,545,myapp1,owner7
checkthis,10.3.2.111,10.9.9.12,545,myapp1,owner7

Below give me the desired columns that i need but does not split rowns containing multiple ips in the second column into multiple rows.

cat -- base.csv | cut -d, -f2-5 > newbase.csv

Can you please suggest ?

1 Answer 1

2

You could use the Miller nest function to explode values across records:

$ mlr --csv nest --explode --values --across-records --nested-fs '/' -f IP_ADDRESS base.csv
NAME,IP_ADDRESS,PRI_IP,APP_CODE,APP_NAME,OWNER
test2,10.9.7.12,10.9.9.12,545,myapp1,owner1
test2,10.222.101.11,10.9.9.12,545,myapp1,owner1
mytest,10.9.7.12,10.9.9.12,545,myapp1,owner9
checkthis,10.9.7.12,10.9.9.12,545,myapp1,owner7
checkthis,10.222.101.11,10.9.9.12,545,myapp1,owner7
checkthis,10.3.2.111,10.9.9.12,545,myapp1,owner7

With more recent versions, --explode --values --across-records --nested-fs '/' may be abbreviated to --evar '/' so

mlr --csv nest --evar '/' -f IP_ADDRESS base.csv >newbase.csv

Alternatively, with awk:

awk -F, '
  BEGIN{OFS=FS}
  {
    n = split($2,a,"/")
    for(i=1;i<=n;i++) {
      $2=a[i]; print
    }
  }
' base.csv >newbase.csv
3
  • mlr command not found on my redhat Linux 7.1 host Is the mlr rpm present along with the dependencies for download ? Kindly share @steeldriver.
    – Ashar
    Oct 18, 2020 at 21:14
  • @Ashar I am not familiar with the RedHat packaging system - you will need to figure that part out yourself Oct 18, 2020 at 21:21
  • amazing answer & thanks @steeldriver
    – Ashar
    Oct 20, 2020 at 21:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .