repeat pattern without using excel


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting repeat pattern without using excel
# 1  
Old 03-14-2008
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  
Old 03-14-2008
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  
Old 03-15-2008
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  
Old 03-15-2008
Aaron and Jadu

Hey thanks for the replies! that looks great! Yall are awesome! hey my name is Aaron too. thx again!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to fill the entire row of Excel file with color based on pattern match

Hi All , I have to write one Perl script in which I need to read one pre-existing xls and based on pattern match for one word in some cells of the XLS , I need to fill the entire row with one color of that matched cell and write the content to another excel Please find the below stated... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

3. Shell Programming and Scripting

Regular Expression repeat pattern

Hi, I'm struggling with very very simple task but dont know where I'm going wrong. Have the following file numbers.txt 1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567890 9876543210 987654321 98765432 9876543 987654 98765 (1 Reply)
Discussion started by: bobbygsk
1 Replies

4. Shell Programming and Scripting

Repeat for different variable

Hey, I've created following script: var1=test1 setA=testA if ... touch $setA/$var1 ... fi I would like now the repeat the command touch (in this example) for different variables. So below, the varX should run 3 times (var1, var2, var4). Var3 is skipped in this example... (4 Replies)
Discussion started by: brononius
4 Replies

5. Shell Programming and Scripting

Perl script to Merge contents of 2 different excel files in a single excel file

All, I have an excel sheet Excel1.xls that has some entries. I have one more excel sheet Excel2.xls that has entries only in those cells which are blank in Excel1.xls These may be in different workbooks. They are totally independent made by 2 different users. I have placed them in a... (1 Reply)
Discussion started by: Anamika08
1 Replies

6. Shell Programming and Scripting

Writing excel file using perl : Excel file formatting changed

I am trying to create a program where user can input data in certain excel cells using user interface on internet....the programming is on perl and server is unix But when i parse data into excel the formatting of sheets is turned to default and all macro coding removed. What to do...Please... (7 Replies)
Discussion started by: mud_born
7 Replies

7. Shell Programming and Scripting

Script on pattern matching and print lines and export to excel

Hi Friends, I am working on a script.. Looking forward for your expert help..... My requirement is: I have a text file where, need to search equip * RTF or end of line with RTF ,once this pattern is found then print 2nd line, 6th line, 7th line to a different file. For Ex: equip 1... (34 Replies)
Discussion started by: shaliniyadav
34 Replies

8. Shell Programming and Scripting

PERL: Split Excel Workbook to Indiv Excel files

Hi, I am trying to find a way to read an excel work book with multiple worksheets. And write each worksheet into a new excel file using perl. My environment is Unix. For example: I have an excel workbook TEST.xls and it has Sheet1, Sheet2, Sheet3 worksheets. I would like to create... (2 Replies)
Discussion started by: sandeep78
2 Replies

9. UNIX for Dummies Questions & Answers

Get line1 and line4 in a repeat pattern file

I have a datafile contain hundreds of lines: line1 1 2 line4 ================== line1 4 2 line4 ================== line1 3 1 line4 =================== (3 Replies)
Discussion started by: bobo
3 Replies

10. UNIX for Dummies Questions & Answers

Repeat Commands

On my system I use Escape "k" to go back in commands. I read on tutorials that it is ctrl p, but that does not work on my system. Anyone know what the command to go foward is? (6 Replies)
Discussion started by: dereckbc
6 Replies
Login or Register to Ask a Question