I have a file that I need to reiterate all the lines. This is a text file with pipe delimeters, four fields and 133 lines.
file1.txt
-----
0 | 0 | 1 | random TEXT1 |
0 | 0 | 2 | random TEXT2 |
0 | 0 | 3 | random TEXT3 |
...
0 | 0 | 133 | random TEXT133 |
-----
Now, I need all 133 lines to re-iterate themselves and change only the 2nd field to a 1, and then a 2. my resulting file would then look like this:
file2.txt
-----
0 | 0 | 1 | random TEXT1 |
0 | 0 | 2 | random TEXT2 |
0 | 0 | 3 | random TEXT3 |
...
0 | 0 | 133 | random TEXT133 |
0 | 1 | 1 | random TEXT1 |
0 | 1 | 2 | random TEXT2 |
0 | 1 | 3 | random TEXT3 |
...
0 | 1 | 133 | random TEXT133 |
0 | 2 | 1 | random TEXT1 |
0 | 2 | 2 | random TEXT2 |
0 | 2 | 3 | random TEXT3 |
.
.
.
0 | 2 | 133 | random TEXT133 |
-----
Notice the 2nd column is incrementing by +1 for each reiteration. everything else stays the same per line. I see that this would be pretty simple to do in excel or something, but I'd rather use a command or a script.
#!/bin/bash
#constants
CNT=10
SOURCE="file1.txt"
RESULT="file2.txt"
#copy first field to file2.txt from file1.txt
cat $SOURCE > $RESULT
printf "\n" > $RESULT
#iterate $CNT times, and append the modified content into the file1.txt in each time
for((i = 1; i < $CNT; i++ ))
do
awk -F'|' '{$2=val; print $1"|"$2"|"$3"|"$4"|"}' val=$i $SOURCE >> $RESULT
printf "\n" >> $RESULT
done
#exit normally
exit 0