附加以逗号分隔的行,同时保留现有的新行
Append lines separated by comma while preserving the existing new line
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
如何在保留新行的同时在 sciprt 中以 csv 格式实现预期的输出。
请您尝试以下操作:
#!/bin/bash
while IFS= read -r i; do # read the input file line by line
j=$(rev <<< "$i") # reverse the string
printf "%s,this is %s\n" "$i" "$j" # print the original string and the reversed one
done < exlist_sample > znamelist
输出:
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
这是我的脚本版本:
#!/bin/bash
inputfile="exlist_sample"
if [[ ! -f "$inputfile" ]]
then
echo "ERROR: input file $inputfile not found."
exit 1
fi
outputfile="znamelist"
while IFS= read -r line
do
reverseline=$(echo "$line"| rev)
echo -e "$line,this is $reverseline\n"
done < "$inputfile" >"$outputfile"
使用 while
和 read
这种方式确保即使行中有空格,脚本也能正常工作。对于您的特定要求,这可能有点矫枉过正,但最好学习“安全”的方法。
无需使用文件存储反转线,您可以在每次while
迭代中将其存储在变量中。
$ cat znamelist
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
A one-liner 使用 paste
、sed
和 rev
(虽然不是 POSIX 实用程序)实用程序和 bash 进程替换可能是:
paste -d, exlist_sample <(rev exlist_sample | sed 's/^/this is /') > znamelist
在每个 Unix 机器上非常有效地使用任何 shell 中的任何 awk 并且几乎不使用内存:
$ awk -v OFS=',' -v ORS='\n\n' '{r=""; for (i=1;i<=length();i++) r=substr([=10=],i,1) r; print [=10=], "this is " r}' file
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
或使用更多内存但效率更高:
$ rev file | awk -v OFS=',' -v ORS='\n\n' 'NR==FNR{r[NR]=[=11=]; next} {print [=11=], "this is " r[FNR]}' - file
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
或[可能]几乎不使用内存最有效:
$ rev file | awk -v OFS=',' -v ORS='\n\n' '{r=[=12=]} (getline < "file") > 0{print [=12=], "this is " r}'
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
如果您要使用最后一个,请务必阅读并理解 awk.freeshell.org/AllAboutGetline。我个人不会,除非内存和效率都是问题。
解决方案
rev input.txt | sed 's/^/this is /' | paste -d, input.txt - | sed G
输入
λ cat input.txt
dz-eggg-123
dz-fggg-123
lk-opipo-123
poipo-123-oiu
输出
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
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
如何在保留新行的同时在 sciprt 中以 csv 格式实现预期的输出。
请您尝试以下操作:
#!/bin/bash
while IFS= read -r i; do # read the input file line by line
j=$(rev <<< "$i") # reverse the string
printf "%s,this is %s\n" "$i" "$j" # print the original string and the reversed one
done < exlist_sample > znamelist
输出:
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
这是我的脚本版本:
#!/bin/bash
inputfile="exlist_sample"
if [[ ! -f "$inputfile" ]]
then
echo "ERROR: input file $inputfile not found."
exit 1
fi
outputfile="znamelist"
while IFS= read -r line
do
reverseline=$(echo "$line"| rev)
echo -e "$line,this is $reverseline\n"
done < "$inputfile" >"$outputfile"
使用
while
和read
这种方式确保即使行中有空格,脚本也能正常工作。对于您的特定要求,这可能有点矫枉过正,但最好学习“安全”的方法。无需使用文件存储反转线,您可以在每次
while
迭代中将其存储在变量中。$ cat znamelist 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
A one-liner 使用 paste
、sed
和 rev
(虽然不是 POSIX 实用程序)实用程序和 bash 进程替换可能是:
paste -d, exlist_sample <(rev exlist_sample | sed 's/^/this is /') > znamelist
在每个 Unix 机器上非常有效地使用任何 shell 中的任何 awk 并且几乎不使用内存:
$ awk -v OFS=',' -v ORS='\n\n' '{r=""; for (i=1;i<=length();i++) r=substr([=10=],i,1) r; print [=10=], "this is " r}' file
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
或使用更多内存但效率更高:
$ rev file | awk -v OFS=',' -v ORS='\n\n' 'NR==FNR{r[NR]=[=11=]; next} {print [=11=], "this is " r[FNR]}' - file
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
或[可能]几乎不使用内存最有效:
$ rev file | awk -v OFS=',' -v ORS='\n\n' '{r=[=12=]} (getline < "file") > 0{print [=12=], "this is " r}'
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
如果您要使用最后一个,请务必阅读并理解 awk.freeshell.org/AllAboutGetline。我个人不会,除非内存和效率都是问题。
解决方案
rev input.txt | sed 's/^/this is /' | paste -d, input.txt - | sed G
输入
λ cat input.txt
dz-eggg-123
dz-fggg-123
lk-opipo-123
poipo-123-oiu
输出
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