![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| repeat character with printf | ripat | Shell Programming and Scripting | 10 | 12-13-2007 05:52 PM |
| to copy and repeat | falcondown01 | Shell Programming and Scripting | 4 | 09-07-2007 05:15 PM |
| Get line1 and line4 in a repeat pattern file | bobo | UNIX for Dummies Questions & Answers | 3 | 10-25-2006 08:11 AM |
| FTP repeat sending files | Euler04 | Shell Programming and Scripting | 1 | 11-24-2005 10:46 AM |
| Repeat Commands | dereckbc | UNIX for Dummies Questions & Answers | 6 | 01-04-2005 07:15 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
repeat pattern without using excel
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. |
| Forum Sponsor | ||
|
|
|
|||
|
try this code(bash):
Code:
#!/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
.Aaron |