Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of sed in while read loop doesn’t complete commands without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
I have a test file called testPage
with the following text:
|| '''Title1''' || '''Title2''' || '''Title3''' || || Cell11 || Cell12 || Cell13 || || Cell21 || Cell22 || || Cell31 || Cell32 || || Cell41 || Cell42 || || CEll51 || Cell52 || || Cell61 || Cell62 || || Cell71 || Cell72 ||
That I want to look like:
{| |'''Title1''' || '''Title2''' || '''Title3''' || |- | Cell11 || Cell12 || Cell13 || |- | Cell21 || Cell22 || |- | Cell31 || Cell32 || |- | Cell41 || Cell42 || |- | CEll51 || Cell52 || |- | Cell61 || Cell62 || |- | Cell71 || Cell72 |}
I have a script:
#!/bin/bash isTable=0 beginTable="^||" lineNum=1 while IFS= read -r line do echo "lineNum: $lineNum, line: $line, isTable: $isTable" if [[ $line =~ $beginTable ]] then if [ "$isTable" -eq "0" ] then isTable=1 sed -r $lineNum's_||_{|n|_' -i testPage #Begin table echo "begin table" else sed -r $lineNum's_^||_|-n|_' -i testPage #Define row ##DOESN'T WORK## echo "start of row" fi else if [ "$isTable" -eq "1" ] then isTable=0 sed -r $lineNum's_(.*)$_1n|}n_' -i testPage #End table ##WEIRD RESULT## echo "end table" fi fi ((lineNum++)) done < testPage
That gives the result:
{| | '''Title1''' || '''Title2''' || '''Title3''' || |- | Cell11 || Cell12 || Cell13 || |- | Cell21 || Cell22 || |- | Cell31 || Cell32 || || Cell41 || Cell42 || |} || CEll51 || Cell52 || || Cell61 || Cell62 || || Cell71 || Cell72 ||
I can’t figure out why it stops substituting after three iterations even though the loop reports the appropriate line and line number as well as matching the appropriate logic.
Any help is appreciated.
For clarity, testPage is a portion of a larger file, so sed
beginning and ending flags (i.e., doing different things on lines 1
and $
)
won’t do for me.
Answer
I used a combination answers to achieve my desired result.
#!/bin/bash isTable=0 beginTable="^||" lineNum=1 tableCount=0 while IFS= read -r line #Print changes to temp file do if [[ $line =~ $beginTable ]] then if [ "$isTable" -eq "0" ] then isTable=1 ((tableCount++)) > $1_$tableCount sed -n -r $lineNum's_^||_{|n|_p' $1 >> $1_$tableCount #Begin table sed -r $lineNum's_^||_@'$tableCount'@_' -i $1 else sed -n -r $lineNum's_^||_|-n|_p' $1 >> $1_$tableCount #Define row fi else if [ "$isTable" -eq "1" ] then isTable=0 sed -n -r $lineNum's_(.*)$_1|}n_p' $1 >> $1_$tableCount #End table fi fi ((lineNum++)) done < $1 for ((n=1;n<=$tableCount;n++)) do sed -e '/@'$n'@/r '$1'_'$n'' -e '/^@'$n'@/d;/^||/d' -i $1 rm "$1"_"$n" done
I print each table to a different numbered file and then sed
read each of those into the original file.
We are here to answer your question about sed in while read loop doesn’t complete commands - If you find the proper solution, please don't forgot to share this with your team members.