The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 13 06-18-2009 06:01 AM
to copy and repeat falcondown01 Shell Programming and Scripting 4 09-07-2007 08:15 PM
Get line1 and line4 in a repeat pattern file bobo UNIX for Dummies Questions & Answers 3 10-25-2006 11:11 AM
FTP repeat sending files Euler04 Shell Programming and Scripting 1 11-24-2005 02:46 PM
Repeat Commands dereckbc UNIX for Dummies Questions & Answers 6 01-04-2005 11:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-14-2008
ajp7701 ajp7701 is offline VIP Member  
Supporter
  
 

Join Date: Dec 2007
Posts: 63
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.
  #2 (permalink)  
Old 03-14-2008
yunccll yunccll is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 23
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
  #3 (permalink)  
Old 03-15-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
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
  #4 (permalink)  
Old 03-15-2008
ajp7701 ajp7701 is offline VIP Member  
Supporter
  
 

Join Date: Dec 2007
Posts: 63
Aaron and Jadu

Hey thanks for the replies! that looks great! Yall are awesome! hey my name is Aaron too. thx again!!!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0