使用的 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 格式。
uj5u.com熱心網友回復:
請您嘗試以下方法:
#!/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
uj5u.com熱心網友回復:
這是我的腳本版本:
#!/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
uj5u.com熱心網友回復:
paste使用,sed和rev(盡管不是 POSIX 實用程式)實用程式和 bash 行程替換的單行程式可能是:
paste -d, exlist_sample <(rev exlist_sample | sed 's/^/this is /') > znamelist
uj5u.com熱心網友回復:
與Perl:
perl -ne 'chomp; print $_, ",this is ", scalar reverse, "\n";' exlist_sample
輸出:
dz-eggg-123,這是 321-ggge-zd dz-fggg-123,這是 321-gggf-zd lk-opipo-123,這是 321-opipo-kl poipo-123-oiu,這是 uio-321-opiop
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441506.html
