Bash 带有嵌套for循环的While循环

Bash 带有嵌套for循环的While循环,bash,Bash,我需要bash脚本的帮助。 所以我把数据输入为 CellIden="461" CellIden="465" CellIden="468" CellIden="462" CellIden="466" CellIden="469" CellIden="463" CellIden="467" CellIden="460" 我已经创建了while循环 cat FolderCell2 | while read line ; do echo $line | sed -e 's/cellIden/Cell/

我需要bash脚本的帮助。 所以我把数据输入为

CellIden="461"
CellIden="465"
CellIden="468"
CellIden="462"
CellIden="466"
CellIden="469"
CellIden="463"
CellIden="467"
CellIden="460"
我已经创建了while循环

cat FolderCell2 | while read line ;
do
echo $line | sed -e 's/cellIden/Cell/g'
done > FolderCell3
这将读取每一行并替换字符串,但我需要在这个while脚本中再执行两个循环:

for (( i = 1; i <=$SEC_NUM ; i++ ))
do
for (( j = 1 ; j <=$Cell_Num ; j++ ))
do
sed -e 's/Cell/Cell$i$j/g'
done
done > File

变量$SEC_NUM和$Cell_NUM有时是不同的。因此,我不知道如何将这两个代码,
while loop
two for loop
组合在一起,以获得如上所述的输出。

类似的方法应该可以:

i=1
while read -r line;
do
    j=1
    sed "s/CellIden=/Cell$i$j=/" <<< "$line"
    for ((j=2; j<=$Cell_Num; j++)); do
        read -r line
        sed "s/CellIden=/Cell$i$j=/" <<< "$line"
    done
    ((i++))
done < FolderCell2 > file.out
i=1
而read-r行;
做
j=1

sed“s/CellIden=/Cell$i$j=/”类似的东西应该可以工作:

i=1
while read -r line;
do
    j=1
    sed "s/CellIden=/Cell$i$j=/" <<< "$line"
    for ((j=2; j<=$Cell_Num; j++)); do
        read -r line
        sed "s/CellIden=/Cell$i$j=/" <<< "$line"
    done
    ((i++))
done < FolderCell2 > file.out
i=1
而read-r行;
做
j=1
sed“s/CellIden=/Cell$i$j=/”A解决方案:

SEC_NUM=3
CellNum=3

i=0
j=0

while read line ; do
  echo  "${line/CellIden/Cell$((i+1))$((j+1))}"
  ((j=(j+1)%CellNum))
  [ $j -eq 0 ] && ((i=(i+1)%SEC_NUM))
done < "$infile" > "$outfile"
输出是

Cell11="461"
Cell12="465"
Cell13="468"
Cell14="462"
Cell21="466"
Cell22="469"
Cell23="463"
Cell24="467"
Cell31="460"
解决方案:

SEC_NUM=3
CellNum=3

i=0
j=0

while read line ; do
  echo  "${line/CellIden/Cell$((i+1))$((j+1))}"
  ((j=(j+1)%CellNum))
  [ $j -eq 0 ] && ((i=(i+1)%SEC_NUM))
done < "$infile" > "$outfile"
输出是

Cell11="461"
Cell12="465"
Cell13="468"
Cell14="462"
Cell21="466"
Cell22="469"
Cell23="463"
Cell24="467"
Cell31="460"

当逐行解析时,我发现使用AWK脚本更方便

考虑以下代码:

#!/bin/awk

BEGIN{
        i=1;
        j=1;
}

{
        split($0,arr,"\"");
        print "Cell" i j "=\"" arr[2] "\""
        j++;
        if (j > CELL_NUM){
                j=1;
                i++;
        }
        if (i > SEC_NUM){
                i=1;
        }
}

END{

}
然后假设脚本为test.awk,输入文件为1.txt,按以下方式运行:

awk -vCELL_NUM=3 -vSEC_NUM=3 -f test.awk 1.txt
获取您想要的输出:

Cell11="461"
Cell12="465"
Cell13="468"
Cell21="462"
Cell22="466"
Cell23="469"
Cell31="463"
Cell32="467"
Cell33="460"

当逐行解析时,我发现使用AWK脚本更方便

考虑以下代码:

#!/bin/awk

BEGIN{
        i=1;
        j=1;
}

{
        split($0,arr,"\"");
        print "Cell" i j "=\"" arr[2] "\""
        j++;
        if (j > CELL_NUM){
                j=1;
                i++;
        }
        if (i > SEC_NUM){
                i=1;
        }
}

END{

}
然后假设脚本为test.awk,输入文件为1.txt,按以下方式运行:

awk -vCELL_NUM=3 -vSEC_NUM=3 -f test.awk 1.txt
获取您想要的输出:

Cell11="461"
Cell12="465"
Cell13="468"
Cell21="462"
Cell22="466"
Cell23="469"
Cell31="463"
Cell32="467"
Cell33="460"
另一种方法:

i=0
while read -r line;
do
    j=$(bc <<< "obase=$Cell_Num; $i" | xargs printf "%02d" | tr '[0-8]' '[1-9]')
    sed "s/CellIden=/Cell$j=/" <<< "$line"
    ((i++))
done < FolderCell2 > file.out
i=0
而read-r行;
做
j=$(bc另一种方法:

i=0
while read -r line;
do
    j=$(bc <<< "obase=$Cell_Num; $i" | xargs printf "%02d" | tr '[0-8]' '[1-9]')
    sed "s/CellIden=/Cell$j=/" <<< "$line"
    ((i++))
done < FolderCell2 > file.out
i=0
而read-r行;
做

j=$(bc这是一种将一维数组数据转换为二维数组的方法。事实上,只需要其中一个(SEC_NUM或Cell_NUM)

awk -v SEC_NUM=3 '{i=(NR-1)%SEC_NUM+1;j=int((NR-1)/SEC_NUM)+1;sub(/CellIden/,"Cell" j i)}1' file

Cell11="461"
Cell12="465"
Cell13="468"
Cell21="462"
Cell22="466"
Cell23="469"
Cell31="463"
Cell32="467"
Cell33="460"

awk -v SEC_NUM=4 '{i=(NR-1)%SEC_NUM+1;j=int((NR-1)/SEC_NUM)+1;sub(/CellIden/,"Cell" j i)}1' file
Cell11="461"
Cell12="465"
Cell13="468"
Cell14="462"
Cell21="466"
Cell22="469"
Cell23="463"
Cell24="467"
Cell31="460"

这是一种将一维数组数据转换为二维数组的方法。实际上只需要其中一种(secu NUM或Cell_NUM)

awk -v SEC_NUM=3 '{i=(NR-1)%SEC_NUM+1;j=int((NR-1)/SEC_NUM)+1;sub(/CellIden/,"Cell" j i)}1' file

Cell11="461"
Cell12="465"
Cell13="468"
Cell21="462"
Cell22="466"
Cell23="469"
Cell31="463"
Cell32="467"
Cell33="460"

awk -v SEC_NUM=4 '{i=(NR-1)%SEC_NUM+1;j=int((NR-1)/SEC_NUM)+1;sub(/CellIden/,"Cell" j i)}1' file
Cell11="461"
Cell12="465"
Cell13="468"
Cell14="462"
Cell21="466"
Cell22="469"
Cell23="463"
Cell24="467"
Cell31="460"

将shell变量输入到sed,您需要使用双配额
将shell变量输入到sed,您需要使用双配额
不是真的,请查看输出文件中的编号。变量“i”和“j”是不同的,并且与您的代码始终相同。请检查我的输出数据..Cell11,Cell12,Cell13,Cell21。。。etc@anishsane:非常感谢您编辑我对套件OP需求的回答。非常感谢…不太感谢,请查看输出文件中的编号。变量“i”和“j”是不同的,并且与您的代码始终相同。请检查我的输出数据..Cell11,Cell12,Cell13,Cell21。。。etc@anishsane:非常感谢您编辑我对套件OP需求的回答。非常感谢。。。