Bash 从文件中提取字符串时,在文件中替换字符串

Bash 从文件中提取字符串时,在文件中替换字符串,bash,shell,Bash,Shell,我需要用bash编写以下要求,如下所示 我的文件包括文件名、stra、strb、colname filename:文件名 斯特拉:要找出的字符串 strb:要替换为的字符串 colname:文件中列的名称 stringlist.csv: filename、stra、strb、colname x、 txt、aaa、xxx、col1 y、 txt、bbb、yyy、col1 x、 txt: col1、col2、col3 aaa,10,20 bbb,20,30 aaa,21,34 ccc,43,98

我需要用bash编写以下要求,如下所示

我的文件包括文件名、stra、strb、colname filename:文件名 斯特拉:要找出的字符串 strb:要替换为的字符串 colname:文件中列的名称

stringlist.csv:

filename、stra、strb、colname
x、 txt、aaa、xxx、col1
y、 txt、bbb、yyy、col1

x、 txt:

col1、col2、col3
aaa,10,20
bbb,20,30
aaa,21,34
ccc,43,98

y、 txt:

aaa,10,20
bbb,20,30
aaa,21,34
ccc,43,98


从作为输入文件的stringlist.csv中,x.txt列中的col1 aaa应替换为xxx,y.txt中包含bbb的col1应替换为yyy,文件中的其他内容应保持不变

我自己做的。我期待的是一些提示,但不是代码

echo -n "Enter 1 for directory
         Enter 2 for list of files
         Enter 3 for one file as input: "
read mode
if [ $mode -eq 1 ]; then
        echo -n "Enter direcotry path: "
        read dir
        DIRS=`ls -l $dir  | awk '{print $9}'`
        echo -n "Do you want to override the file or want to create a new file(y/n)? "
        read choice
        if [ $choice == "n" ]; then
                echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
                        read prefix
        elif [ $choice == "y" ]; then
                :
        else
                echo  $'You have chosen invalid option.... Bye!!\n'
                exit
        fi
elif [ $mode -eq 2 ]; then
        echo -n "Enter List of files separated by space: "
        read DIRS
        echo -n "Do you want to override the file or want to create a new file(y/n)? "
        read choice
        if [ $choice == "n" ]; then
                echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
                        read prefix
        elif [ $choice == "y" ]; then
                :
        else
                echo  $'You have chosen invalid option.... Bye!!\n'
                exit
        fi
elif [ $mode -eq 3 ]; then
        echo -n "Enter file name: "
        read DIRS
        echo -n "Do you want to override the file or want to create a new file(y/n)? "
        read choice
        if [ $choice == "n" ]; then
                echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
                        read prefix
        elif [ $choice == "y" ]; then
                :
        else
                echo  $'You have chosen invalid option.... Bye!!\n'
                exit
        fi
else
        echo  $'You have chosen invalid option.... Bye!!\n'
        exit
fi
echo -n "Enter file delimiter: "
read del
echo -n "enter string replacement file: "
read strrepfile
echo -n "Enter Delimiter for string replacement input file: "
read indel
if [ $indel == "tab" ]; then
        indel='\t'
        sed "s/$indel/|/g" $strrepfile > $strrepfile.1
else
         sed "s/$indel/|/g" $strrepfile > $strrepfile.1
fi
#filenames=`awk 'BEGIN{FS=OFS='$indel1'}{print $1}' $strrepfile`
if [ $del == "tab" ]; then
        del1='"'"\t"'"'
else
        del1='"'$del'"'
fi
for var in $DIRS; do
        if [ $mode -eq 1 ]; then
                colheadfile=$dir/$var
        else
                colheadfile=$var
        fi
        break
done
awvar="-v col"
while read -r line
do
        echo "$line" > temp
        stra=`awk -F '|' '{print $1}' temp`
        strb=`awk -F '|' '{print $2}' temp`
        col=`awk -F '|' '{print $3}' temp`
        if [ $del == "tab" ]; then
                del='\t'
        fi
        filedel="'"$del"'"
        colpos=`head -1 $colheadfile | tr -s $filedel '\n' | nl -nln |  grep $col | cut -f1`
        colpos=$(echo $colpos | tr -d ' ')
        awkvar1=$awkvar1$awvar$colpos'='$colpos' '
        gsubvar=$gsubvar'gsub("'$stra'","'$strb'",$'$colpos');'
done < "$strrepfile.1"
echo $awkvar1
echo $gsubvar
for dir1 in $DIRS; do
        echo $dir1
        if [ $mode -eq 1 ] && [ $choice == "y" ]; then
                awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir/$dir1 > $dir1
        elif [ $mode -eq 1 ] && [ $choice == "n" ]; then
                awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir/$dir1 > $prefix'_'$dir1
        elif [ $mode -eq 2 ] && [ $choice == "y" ]; then
                awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $dir1
        elif [ $mode -eq 2 ] && [ $choice == "n" ]; then
                awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $prefix'_'$dir1
        elif [ $mode -eq 3 ] && [ $choice == "y" ]; then
                awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $dir1
        elif [ $mode -eq 3 ] && [ $choice == "n" ]; then
                awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $prefix'_'$dir1
        fi
done
rm $strrepfile.1
rm temp
echo-n“为目录输入1
输入2作为文件列表
为一个文件输入3作为输入:”
读取模式
如果[$模式-等式1];然后
echo-n“输入目录路径:”
读目录
DIRS=`ls-l$dir | awk'{print$9}'`
echo-n“是否要覆盖该文件或创建新文件(y/n)?”
阅读选择
如果[$choice==“n”];然后
echo-n“输入输出文件的前缀。输出文件将是前缀,后跟文件名:”
读前缀
elif[$choice==“y”];然后
:
其他的
echo$'您选择的选项无效。。。。再见\不
出口
fi
elif[$模式-等式2];然后
echo-n“输入以空格分隔的文件列表:”
读目录
echo-n“是否要覆盖该文件或创建新文件(y/n)?”
阅读选择
如果[$choice==“n”];然后
echo-n“输入输出文件的前缀。输出文件将是前缀,后跟文件名:”
读前缀
elif[$choice==“y”];然后
:
其他的
echo$'您选择的选项无效。。。。再见\不
出口
fi
elif[$模式-等式3];然后
echo-n“输入文件名:”
读目录
echo-n“是否要覆盖该文件或创建新文件(y/n)?”
阅读选择
如果[$choice==“n”];然后
echo-n“输入输出文件的前缀。输出文件将是前缀,后跟文件名:”
读前缀
elif[$choice==“y”];然后
:
其他的
echo$'您选择的选项无效。。。。再见\不
出口
fi
其他的
echo$'您选择的选项无效。。。。再见\不
出口
fi
echo-n“输入文件分隔符:”
读德尔
echo-n“输入字符串替换文件:”
读取strepfile
echo-n“为字符串替换输入文件输入分隔符:”
阅读索引
如果[$indel==“tab”];然后
indel='\t'
sed“s/$indel/|/g”$strepfile>$strepfile.1
其他的
sed“s/$indel/|/g”$strepfile>$strepfile.1
fi
#文件名=`awk'开始{FS=OFS='$indel1'}{print$1}'$strepfile`
如果[$del==“tab”];然后
del1='''\t''
其他的
del1=““$del”
fi
对于风险值,单位为美元迪尔;做
如果[$模式-等式1];然后
colheadfile=$dir/$var
其他的
colheadfile=$var
fi
打破
完成
awvar=“-v col”
而read-r行
做
回声“$line”>温度
stra=`awk-F'|'{print$1}'临时`
strb=`awk-F'|'{print$2}'临时`
col=`awk-F'|'{print$3}'临时`
如果[$del==“tab”];然后
del='\t'
fi
filedel=“”“$del””
colpos=`head-1$colheadfile | tr-s$filedel'\n'| nl-nln | grep$col | cut-f1`
colpos=$(echo$colpos | tr-d'')
awkvar1=$awkvar1$awvar$colpos'='$colpos''
gsubvar=$gsubvar'gsub(“$stra'”、“$strb'”、$“$colpos”);'
完成<“$strepfile.1”
echo$awkvar1
echo$gsubvar
对于dir1,单位为$DIRS;做
echo$dir1
如果[$mode-eq 1]&&[$choice==“y”];然后
awk$awkvar1'开始{FS=OFS='$del1'}{'$gsubvar'print;}'$dir/$dir1>$dir1
elif[$mode-eq 1]&&[$choice==“n”];然后
awk$awkvar1'开始{FS=OFS='$del1'}{'$gsubvar'print;}'$dir/$dir1>$prefix''.'$dir1'
elif[$mode-eq 2]&&[$choice==“y”];然后
awk$awkvar1'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}'$dir1>$dir1
elif[$mode-eq 2]&&[$choice==“n”];然后
awk$awkvar1'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}'$dir1>$prefix''.'$dir1'
elif[$mode-eq 3]&&[$choice==“y”];然后
awk$awkvar1'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}'$dir1>$dir1
elif[$mode-eq 3]&&[$choice==“n”];然后
awk$awkvar1'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}'$dir1>$prefix''.'$dir1'
fi
完成
rm$STREPFILE.1
室温

谢谢

看来您希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是包括您迄今为止编写的代码、示例输入(如果有)、预期输出和实际获得的输出(控制台输出、回溯等)。你提供的细节越多,你可能得到的答案就越多。检查和。事实上,我的要求略有改变,我完成了。Stringlist.csv文件没有包含文件名的第一列。它只有三列,即stra、strb和columnname。我们可以将目录(假设所有文件的格式相同)或多个文件(假设所有文件的格式相同)或单个文件传递给此代码。