The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-14-2008
Supporter
 

Join Date: Dec 2007
Posts: 42
Stumble this Post!
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-14-2008
Registered User
 

Join Date: Mar 2008
Posts: 23
Stumble this Post!
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
you can modify the CNT=?, and run it!

.Aaron
Reply With Quote
  #3 (permalink)  
Old 03-14-2008
jaduks's Avatar
Registered User
 

Join Date: Aug 2007
Location: Assam,India
Posts: 145
Stumble this Post!
Code:
$ cat aj.txt
0|0|1|random TEXT1|
0|0|2|random TEXT2|
0|0|3|random TEXT3|
0|0|4|random TEXT4|

$ for i in `seq 0 4`
> do
> awk 'BEGIN{OFS=FS="|"}{$2+="'"$i"'"}{print}' aj.txt >> aj.mod
> done

$ cat aj.mod
0|0|1|random TEXT1|
0|0|2|random TEXT2|
0|0|3|random TEXT3|
0|0|4|random TEXT4|
0|1|1|random TEXT1|
0|1|2|random TEXT2|
0|1|3|random TEXT3|
0|1|4|random TEXT4|
0|2|1|random TEXT1|
0|2|2|random TEXT2|
0|2|3|random TEXT3|
0|2|4|random TEXT4|
0|3|1|random TEXT1|
0|3|2|random TEXT2|
0|3|3|random TEXT3|
0|3|4|random TEXT4|
0|4|1|random TEXT1|
0|4|2|random TEXT2|
0|4|3|random TEXT3|
0|4|4|random TEXT4|
//Jadu
Reply With Quote
  #4 (permalink)  
Old 03-14-2008
Supporter
 

Join Date: Dec 2007
Posts: 42
Stumble this Post!
Aaron and Jadu

Hey thanks for the replies! that looks great! Yall are awesome! hey my name is Aaron too. thx again!!!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0