追加以逗号分隔的行,同时保留现有的新行

发布于 2025-01-13 22:43:18 字数 888 浏览 5 评论 0原文

使用的 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 技术交流群。

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

发布评论

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

评论(5

悲歌长辞 2025-01-20 22:43:18

请您尝试以下操作:

#!/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

Would you please try the following:

#!/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

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
峩卟喜欢 2025-01-20 22:43:18

使用 pastesedrev(尽管不是 POSIX 实用程序)实用程序和 bash 进程替换的单行代码可以是:

paste -d, exlist_sample <(rev exlist_sample | sed 's/^/this is /') > znamelist

A one-liner using paste, sed, and rev (though not a POSIX utility) utilities and bash process substitution could be:

paste -d, exlist_sample <(rev exlist_sample | sed 's/^/this is /') > znamelist
墨小沫ゞ 2025-01-20 22:43:18

在每个 Unix 机器上的任何 shell 中非常有效地使用任何 awk 并且几乎不使用内存:

$ awk -v OFS=',' -v ORS='\n\n' '{r=""; for (i=1;i<=length();i++) r=substr($0,i,1) r; print $0, "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]=$0; next} {print $0, "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=$0} (getline < "file") > 0{print $0, "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如果你要使用最后一个。就我个人而言,我不会,除非内存和效率都是问题。

Using any awk in any shell on every Unix box pretty efficiently and using almost no memory:

$ awk -v OFS=',' -v ORS='\n\n' '{r=""; for (i=1;i<=length();i++) r=substr($0,i,1) r; print $0, "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

or using more memory but more efficiently:

$ rev file | awk -v OFS=',' -v ORS='\n\n' 'NR==FNR{r[NR]=$0; next} {print $0, "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

or [probably] most efficiently using almost no memory:

$ rev file | awk -v OFS=',' -v ORS='\n\n' '{r=$0} (getline < "file") > 0{print $0, "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

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.

禾厶谷欠 2025-01-20 22:43:18

这是我的脚本版本:

#!/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"
  • 使用 whileread 这种方式可以确保即使行中有空格,脚本也能正常工作。对于您的具体要求来说,这可能有点过分了,但最好学习“安全”的方法。

  • 不需要使用文件来存储反转的行,您可以将其存储在每次 while 迭代中的变量中。

    $ cat znamelist 
    dz-eggg-123,这是 321-ggge-zd
    
    dz-fggg-123,这是 321-gggf-zd
    
    lk-opipo- 123,这是 321 -opipo-kl
    
    poipo-123-oiu,这是 uio-321-opiop
    

Here is my version of your script:

#!/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"
  • using while with read 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.

    $ 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
    
情魔剑神 2025-01-20 22:43:18

解决方案

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

Solution

rev input.txt | sed 's/^/this is /' | paste -d, input.txt - | sed G

Input

λ cat input.txt 
dz-eggg-123
dz-fggg-123
lk-opipo-123
poipo-123-oiu

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

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