1
#!/bin/bash

Dir="" 
while read line
    do echo "Record is :$line"
    Dir+="Dir,$line" 
done < dir.csv 

echo $Dir

where dir.csv is an input file here which includes following data:

/example/people/
/book/
/book/english/

I would like to append the values of the rows into one variable like /example/people/,/book/,/book/english/

Is there any easy way to achieve this through shell script? above script is showing only the last value ex:/book/english/

3
  • 1
    Please, kindly edit the post enclosing and formatting your script in order to understand it better and give you a useful answer. Commented May 26, 2021 at 14:43
  • 1
    How about tr '\n' ',' < dir.csv Commented May 26, 2021 at 14:48
  • Keeping your current code structure, you have Dir+="Dir,$line" but you should be using Dir="$Dir,$line" or Dir="$Dir${Dir:+,}$line", where the more complex version avoids a leading comma. This works in any shell derived from the Bourne shell (sh, bash, zsh, dash, ksh, …). In Bash (and probably some other modern shells), you could use Dir+="${Dir:+,}$line", where the ${Dir:+,} part adds a comma only if Dir is set and non-empty — avoiding the leading comma again. See Shell parameter expansion. Commented May 26, 2021 at 15:06

2 Answers 2

4

I don't see anything in your code that would cause your script to only show the last value.

This may be an illusion: is your dir.csv file CRLF-delimited? (DOS/Windows format) If so, remove the CR that ends each line with a utility like dos2unix or a command like tr -d '\r'.

Some notes though:

  • In Dir+="Dir,$line", the string Dir should probably be removed (Dir+=",$line").
  • You probably want to get rid off the initial comma: Dir=${Dir#,}.
  • All this can be simplified with the single command below:
Dir=$(paste -s -d, dir.csv)

... or, with CRLF line-endings:

Dir=$(tr -d '\r' < dir.csv | paste -s -d,)
Sign up to request clarification or add additional context in comments.

Comments

1

This is easier:

xargs < dir.csv|sed 's/ /,/g'

or if you have CRLF line endings, you can clean those up with:

xargs < dir.csv|tr -d '\r'|sed 's/ /,/g'

The above proposed tr '\n' ',' < dir.csv can add an additional , at the end if the CSV ends with newline

2 Comments

I would like to save all the values in the variable( ex /example/people/,/book/,/book/english/) but this also shows last row only ( ,/book/english/)
please check your line ending, or see my updated answer.

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.