Home > Back-end >  Create files from one column of a csv. And write data from the other column to thos files
Create files from one column of a csv. And write data from the other column to thos files

Time:12-05

I have a csv file (xyz.csv) contains hostnames and IPs. e.g.:

HOSTNAME;IP;GW
device1;ip1;gw1
device2;ip2;gw2

I want to create files named with data from the column1 and those files contain data from column2. So, file "device1" contain ip1 and gw1(the order dose matter) and so on.

Expected output:

cat device1

ip1
gw1

cat device2

ip2
gw2

CodePudding user response:

You could use the great Miller to run

# create output folder
mkdir -p ./output
# extract in it one file for each HOSTNAME
mlr --csv --fs ";" --from input.csv put -q 'tee > "./output/".$HOSTNAME.".csv", $*'
# keep only the IP column
mlr -I --headerless-csv-output  --csv --ifs ";"  cut -x -f HOSTNAME then reshape -r "." -o i,v then cut -f v ./output/*.csv

and get

cat ./output/device1.csv

ip1
gw1

cat ./output/device2.csv

ip2
gw2
  • Related