0

I want to transform a CSV file using bash by adding new lines into it depending on some conditions described below :

CSV file structure:

name,id_name,url Amy,N1,http://google.com Rob,N2,http://google.com http://other-url.com http://anotherurl.com http://other-again.com Johh,N3,http://google.com http://anotherurl.com Jack,N4,http://google.com http://other-url.com ... 

I want to transform the CSV file like this :

name,id_name,url Amy,N1,http://google.com Rob,N2,http://google.com Rob,N2,http://other-url.com Rob,N2,http://anotherurl.com Johh,N3,http://google.com Johh,N3,http://anotherurl.com Jack,N4,http://google.com Jack,N4,http://other-url.com ... 

Thanks

4 Answers 4

1

It is just a matter of splitting the last field and then printing the 1st and 2nd field followed by the set of these slices:

awk 'BEGIN{FS=OFS=","}{n=split($NF,a," "); for (i=1;i<=n;i++) print $1,$2,a[i]}' file 

Which returns:

Amy,N1,http://google.com Rob,N2,http://google.com Rob,N2,http://other-url.com Rob,N2,http://anotherurl.com Rob,N2,http://other-again.com Johh,N3,http://google.com Johh,N3,http://anotherurl.com Jack,N4,http://google.com Jack,N4,http://other-url.com 
Sign up to request clarification or add additional context in comments.

Comments

1

This awk should work:

awk -F '[, ]' -v OFS=, '{for (i=3; i<=NF; i++) print $1, $2, $i}' file name,id_name,url Amy,N1,http://google.com Rob,N2,http://google.com Rob,N2,http://other-url.com Rob,N2,http://anotherurl.com Rob,N2,http://other-again.com Johh,N3,http://google.com Johh,N3,http://anotherurl.com Jack,N4,http://google.com Jack,N4,http://other-url.com 
  • -F '[, ]' sets field separator as comma or space.
  • Then just start iterating from field #3 and print it along with first 2 fields.

Comments

0

with bash

while IFS=, read name id url; do set -f for u in $url; do echo "$name,$id,$u" done set +f done < file 
name,id_name,url Amy,N1,http://google.com Rob,N2,http://google.com Rob,N2,http://other-url.com Rob,N2,http://anotherurl.com Rob,N2,http://other-again.com Johh,N3,http://google.com Johh,N3,http://anotherurl.com Jack,N4,http://google.com Jack,N4,http://other-url.com 

This will not pring any records for which the url field is empty.

I'm taking advantage of shell word-splitting with the unquoted variable in the for-loop. For safety, I'm turning off filename expansion while I do that.

Comments

0

perl -F'[, ]' -lane 'for ($i=2; $i<=$#F; $i++) {print "$F[0],$F[1],$F[$i]"}' file

-a autosplits each line into the @F array
-F'[, ]' autosplit field separator is either a comma or a space
$#F is the index of the last element of the @F array
perl arrays start with index 0, while awk starts with 1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.