追加以逗号分隔的行,同时保留现有的新行
使用的 Bash 脚本:
#!/bin/bash
set -xv
IS=$'\n'
list=$(cat exlist_sample | xargs -n1)
for i in $list; do
echo "$i" | rev > slist
echo "$i" >> znamelist
for x in $(cat slist);do
echo "this is $x" >> znamelist
echo $IS >> znamelist
done
done
使用的输入文件 (exlist_sample)
dz-eggg-123
dz-fggg-123
lk-opipo-123
poipo-123-oiu
当前输出 (final_list)
dz-eggg-123
this is 321-ggge-zd
dz-fggg-123
this is 321-gggf-zd
lk-opipo-123
this is 321-opipo-kl
poipo-123-oiu
this is uio-321-opiop
预期输出:
dz-eggg-123,this is 321-ggge-zd
dz-fggg-123,this is 321-gggf-zd
lk-opipo-123,this is 321-opipo-kl
poipo-123-oiu,this is uio-321-opiop
如何实现预期输出以将其转换为 csv在 sciprt 中格式化,同时保留新行。
Bash script used:
#!/bin/bash
set -xv
IS=
Input file used (exlist_sample)
dz-eggg-123
dz-fggg-123
lk-opipo-123
poipo-123-oiu
Current output (final_list)
dz-eggg-123
this is 321-ggge-zd
dz-fggg-123
this is 321-gggf-zd
lk-opipo-123
this is 321-opipo-kl
poipo-123-oiu
this is uio-321-opiop
Expected output:
dz-eggg-123,this is 321-ggge-zd
dz-fggg-123,this is 321-gggf-zd
lk-opipo-123,this is 321-opipo-kl
poipo-123-oiu,this is uio-321-opiop
How to achieve the expected output to make it in csv format in the sciprt while preserving the new line.
\n'
list=$(cat exlist_sample | xargs -n1)
for i in $list; do
echo "$i" | rev > slist
echo "$i" >> znamelist
for x in $(cat slist);do
echo "this is $x" >> znamelist
echo $IS >> znamelist
done
done
Input file used (exlist_sample)
Current output (final_list)
Expected output:
How to achieve the expected output to make it in csv format in the sciprt while preserving the new line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请您尝试以下操作:
输出:
Would you please try the following:
Output:
使用
paste
、sed
和rev
(尽管不是 POSIX 实用程序)实用程序和 bash 进程替换的单行代码可以是:A one-liner using
paste
,sed
, andrev
(though not a POSIX utility) utilities and bash process substitution could be:在每个 Unix 机器上的任何 shell 中非常有效地使用任何 awk 并且几乎不使用内存:
或者使用更多内存但更有效:
或者[可能]最有效地使用几乎不使用内存:
请确保阅读并理解 awk.freeshell.org/AllAboutGetline如果你要使用最后一个。就我个人而言,我不会,除非内存和效率都是问题。
Using any awk in any shell on every Unix box pretty efficiently and using almost no memory:
or using more memory but more efficiently:
or [probably] most efficiently using almost no memory:
Make sure to read and understand awk.freeshell.org/AllAboutGetline if you're going to use that last one. Personally I wouldn't unless memory and efficiency are both issues.
这是我的脚本版本:
使用
while
和read
这种方式可以确保即使行中有空格,脚本也能正常工作。对于您的具体要求来说,这可能有点过分了,但最好学习“安全”的方法。不需要使用文件来存储反转的行,您可以将其存储在每次
while
迭代中的变量中。Here is my version of your script:
using
while
withread
this way ensures the script will work ok even if there are spaces in lines. It might be overkill a bit for your specific requirement here, but better learn the "safe" way to do it.no need to use files to store the reversed line, you can store it in a variable in each
while
iteration.解决方案
输入
输出
Solution
Input
Output