Tom
Tom

Reputation: 3

Create files from one column of a csv. And write data from the other column to thos files

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

Upvotes: 0

Views: 108

Answers (1)

aborruso
aborruso

Reputation: 5688

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", $*'

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

Upvotes: 1

Related Questions