如何在 Bash 中连接文件中的所有行?

发布于 2024-12-13 11:55:59 字数 460 浏览 1 评论 0原文

我有一个文件 csv

data1,data2,data2
data3,data4,data5
data6,data7,data8

我想将其转换为(包含在变量中):

variable=data1,data2,data2%0D%0Adata3,data4, data5%0D%0Adata6,data7,data8

我的尝试:

data=''
cat csv | while read line
do
data="${data}%0D%0A${line}"
done
echo $data  # Fails, since data remains empty (loop emulates a sub-shell and looses data)

请帮助..

I have a file csv :

data1,data2,data2
data3,data4,data5
data6,data7,data8

I want to convert it to (Contained in a variable):

variable=data1,data2,data2%0D%0Adata3,data4,data5%0D%0Adata6,data7,data8

My attempt :

data=''
cat csv | while read line
do
data="${data}%0D%0A${line}"
done
echo $data  # Fails, since data remains empty (loop emulates a sub-shell and looses data)

Please help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

遗心遗梦遗幸福 2024-12-20 11:55:59

从文件中删除换行符更简单:

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

Simpler to just strip newlines from the file:

tr '\n' '' < yourfile.txt > concatfile.txt
海未深 2024-12-20 11:55:59

在 bash 中,

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

在非 bash shell 中,您可以使用 `...` 代替 $(...)。另外,echo -n 可以抑制换行符,不幸的是它并不完全可移植,但同样可以在 bash 中使用。

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.

摇划花蜜的午后 2024-12-20 11:55:59

其中一些答案非常复杂。这个怎么样。

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

或者

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

对你来说太“外部实用”?

IFS=

现在你有了一个数组!随心所欲地输出它,也许

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

仍然太“外部效用?”好吧,

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

是的,printf 可以是内置函数。如果不是,但您的 echo 是并且它支持 -n,请改用 echo -n

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

好的,现在我承认我是变得有点傻了。安德鲁的回答是完全正确的。

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

现在你有了一个数组!随心所欲地输出它,也许

仍然太“外部效用?”好吧,

是的,printf 可以是内置函数。如果不是,但您的 echo 是并且它支持 -n,请改用 echo -n

好的,现在我承认我是变得有点傻了。安德鲁的回答是完全正确的。

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=

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.

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

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

Still too "external utility?" Well fine,

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

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

一笔一画续写前缘 2024-12-20 11:55:59

我更喜欢循环:

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

注意:此解决方案要求输入文件在文件末尾有一个新行,否则它将删除最后一行。

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.

ζ澈沫 2024-12-20 11:55:59

另一个简短的 bash 解决方案

variable=$(
  RS=""
  while read line; do
    printf "%s%s" "$RS" "$line"
    RS='%0D%0A'
  done < filename
)

Another short bash solution

variable=$(
  RS=""
  while read line; do
    printf "%s%s" "$RS" "$line"
    RS='%0D%0A'
  done < filename
)
静待花开 2024-12-20 11:55:59
awk 'END { print r }
{ r = r ? r OFS $0 : $0 }
  ' OFS='%0D%0A' infile  

使用 shell

data=

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

printf '%s\n' "$data"   

最近的 bash 版本:

data=

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

printf '%s\n' "$data"
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"
神经大条 2024-12-20 11:55:59

一个非常简单的单行解决方案,不需要额外的文件,因为它很容易理解(我认为,只需将文件放在一起并执行 sed-replace ):

output=$(echo $(cat ./myFile.txt) | sed 's/ /%0D%0A/g')

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')
寂寞清仓 2024-12-20 11:55:59

没用的猫,受罚!您想要将 CSV 送入循环

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

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

while read line; do 
 # ...
done < csv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文