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_(.*)$_\1\n\|\}\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.
sed [...] -i testPagewithsed [...] testPage >> newfileand checknewfile?sed -n -r $lineNum's_\|\|_\{\|\n\|_p'then, i.e. suppress output (-n) and print the replaced line (pinsedoperation).newfilehas the correct output then, you have found the source of your problem: The number of lines should not change while iterating over the lines of a file.