How to concatenate all lines from a file in Bash? [duplicate]

狂风中的少年 提交于 2019-12-06 08:57:32

In bash,

data=$(
while read line
do
  echo -n "%0D%0A${line}"
done < csv)

In non-bash shells, you can use `...` instead of $(...). Also, echo -n, which suppresses the newline, is unfortunately not completely portable, but again this will work in bash.

Simpler to just strip newlines from the file:

tr '\n' '' < yourfile.txt > concatfile.txt

Some of these answers are incredibly complicated. How about this.

 data="$(xargs printf ',%s' < csv | cut -b 2-)"

or

 data="$(tr '\n' ',' < csv | cut -b 2-)"

Too "external utility" for you?

IFS=$'\n', read -d'\0' -a data < csv

Now you have an array! Output it however you like, perhaps with

data="$(tr ' ' , <<<"${data[@]}")"

Still too "external utility?" Well fine,

data="$(printf "${data[0]}" ; printf ',%s' "${data[@]:1:${#data}}")"

Yes, printf can be a builtin. If it isn't but your echo is and it supports -n, use echo -n instead:

data="$(echo -n "${data[0]}" ; for d in "${data[@]:1:${#data[@]}}" ; do echo -n ,"$d" ; done)"

Okay, now I admit that I am getting a bit silly. Andrew's answer is perfectly correct.

Alexandre Mélard

I would much prefer a loop:

for line in $(cat file.txt); do echo -n $line; done

Note: This solution requires the input file to have a new line at the end of the file or it will drop the last line.

Another short bash solution

variable=$(
  RS=""
  while read line; do
    printf "%s%s" "$RS" "$line"
    RS='%0D%0A'
  done < filename
)
awk 'END { print r }
{ r = r ? r OFS $0 : $0 }
  ' OFS='%0D%0A' infile  

With shell:

data=

while IFS= read -r; do
  [ -n "$data" ] &&
     data=$data%0D%0A$REPLY ||
    data=$REPLY
done < infile

printf '%s\n' "$data"   

Recent bash versions:

data=

while IFS= read -r; do
  [[ -n $data ]] &&
     data+=%0D%0A$REPLY ||
    data=$REPLY
done < infile

printf '%s\n' "$data"

Useless use of cat, punished! You want to feed the CSV into the loop

while read line; do 
 # ...
done < csv

A very simple single-line solution which requires no extra files as its quite easy to understand (I think, just cat the file together and perform sed-replace):

output=$(echo $(cat ./myFile.txt) | sed 's/ /%0D%0A/g')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!