1

I have the following CSV files ( I have linux red-hat 6.2 version )

# more  SIMPLE_FILES.CSV
  FILE1,FILE2,FILE3
  /etc/hosts,/etc/info.txt,/var/log.txt
  /etc/some_file,/var/collect.txt,/etc/INFO.txt
  /sbin/ls,/sbin/awk,/sbin/sed

 # more COMPLEX_FILES.CSV
   FILE1,FILE2,FILE3
   /etc/config/net ip.txt,/var/log/summary snap/LOG OF global/info.txt
   /etc/hosts files hosts.info/etc/hosts,var/log/messages,/sbin/collect file/all.info
   /etc/old/TEXT INFO/info.txt/etc/OLD FILES/info.txt,/root/customor select/info.txt

the following bash script should read the CSV file and print the files PATH for FILE1,FILE2,FILE3

remark - I set the param CSV_LINE=2 only for example ( the second values in CSV )

 #!/bin/bash


 CSV=SIMPLE_FILES.CSV

 CSV_LINE=2

 eval $(awk -v A=$CSV_LINE -F, 'NR==1 { for (i=1; i<=NF; i++) sn[i]=$i }
            NR==A { for (i=1; i<=NF; i++) print sn[i] "=" $i; exit }' $CSV  )


 echo $FILE1
 echo $FILE2
 echo $FILE3

so if I run the bash script ( when I set CSV=SIMPLE_FILES.CSV ) get the following

 /read_path_from_csv.bash


 /etc/hosts
 /etc/info.txt
 /var/log.txt

until now its fine,

but if I run the bash script ( when I set CSV=COMPLEX_FILES.CSV ) get the follwoiojng

  ./read_path_from_csv.bash: line 20: ip.txt: command not found

My conclusion - its seems that the spaces in the PATH cause for this error

please advice how to update my code in order to print the PATH as defined in the COMPLEX_FILES.CSV ,

How to add one " before PATH and one " after the PATH ?

Example what I should to get:

/read_path_from_csv.bash

/etc/config/net ip.txt,/var/log/summary snap/LOG OF global/info.txt
/etc/hosts files hosts.info/etc/hosts,var/log/messages,/sbin/collect file/all.info
/etc/old/TEXT INFO/info.txt/etc/OLD FILES/info.txt,/root/customor select/info.txt
7
  • I don't see the difference between COMPLEX_FILES.csv input and output, apart from the fact 1st line is not printed. Could you indicate so? Commented Jul 16, 2014 at 9:51
  • the first line shuld not printed because they the parameters , we want to print only the values from the second line until end Commented Jul 16, 2014 at 9:58
  • Then why not just awk 'NR>1' file? It is still not clear what you want and definitely using eval looks a bit odd. Commented Jul 16, 2014 at 10:01
  • awk 'NR>1' print only the values , but what I need is to set the values in to the parameters that defined in the first line in the CSV ( parameters shuld be print in side the bash script ) Commented Jul 16, 2014 at 10:03
  • Let me know if you have other idea Commented Jul 16, 2014 at 10:07

1 Answer 1

2

Yes, it the spaces in the PATH that causing this problem, and you can add quotes to avoid it.

eval $(awk -v A=$CSV_LINE -v q='"' -F, 'NR==1 { for (i=1; i<=NF; i++) sn[i]=$i }
            NR==A { for (i=1; i<=NF; i++) print sn[i] "=" q $i q; exit }' $CSV)

Explanation:

If you run eval 'FILE1=/path/file name' in bash, it will give an error:

-bash: name: command not found

And you can quotes the /path/file name part to avoid this error:

> eval 'FILE1="/path/file name"'
> echo $FILE1
/path/file name
Sign up to request clarification or add additional context in comments.

1 Comment

YES GOOD SOLUTION I AGREE ( +1 and UPVOTE )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.