Repeat using for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Repeat using for loop
# 1  
Old 10-11-2011
Repeat using for loop

I have a file like this
Code:
2011-10-10 10:46:00,1-1-13-1-1,151510,ALCLA0A84D2C
2011-10-10 10:46:00,1-1-13-1-1,151520,65537
2011-10-10 10:46:00,1-1-13-1-1,151515,46932
2011-10-10 10:46:00,1-1-13-1-1,151521,32769
2011-10-10 10:46:00,1-1-13-1-1,151522,32769
2011-10-10 10:46:00,1-1-13-1-1,151518,167
2011-10-10 10:46:00,1-1-13-1-1,151523,-183
2011-10-10 10:46:00,1-1-13-1-1,151517,2
2011-10-10 10:46:00,1-1-13-1-1,151512,3FE51469AAAA01
2011-10-10 10:46:00,1-1-13-1-1,151513,BVM5400CRAO00240VA
2011-10-10 10:46:00,1-1-13-1-1,151519,11984
2011-10-10 10:46:00,1-1-13-1-1,11124,0
2011-10-10 10:46:00,1-1-13-1-1,151516,0
2011-10-10 10:46:00,1-1-13-1-1,151514,0
2011-10-10 10:46:00,1-1-13-1-1,121255,4673
2011-10-10 10:46:00,1-1-13-1-1,121256,333
2011-10-10 10:46:00,1-1-13-1-1,121254,50
2011-10-10 10:46:00,1-1-13-1-1,121257,16
2011-10-10 10:46:00,1-1-13-1-1,151511,WILDCARD

All i need is 1-1-13-1-1 to be generated n number of times with the same set of data
eg 1-1-13-1-2
1-1-13-1-3... till n

Please help
Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by vbe; 10-11-2011 at 08:32 AM..
# 2  
Old 10-11-2011
Code:
# awk -F, 'NR==1{print}NR>1{split($2,a,"-");x=a[NF];i=1;while(i<=NF){for(j=1;j<NR-1;j++);
{printf "%s,%s-%s-%s-%s-%d,%s,%s\n",$1,a[i++],a[i++],a[i++],a[i++],x+j,$3,$4}}}' infile
2011-10-10 10:46:00,1-1-13-1-1,151510,ALCLA0A84D2C
2011-10-10 10:46:00,1-1-13-1-2,151520,65537
2011-10-10 10:46:00,1-1-13-1-3,151515,46932
2011-10-10 10:46:00,1-1-13-1-4,151521,32769
2011-10-10 10:46:00,1-1-13-1-5,151522,32769
2011-10-10 10:46:00,1-1-13-1-6,151518,167
2011-10-10 10:46:00,1-1-13-1-7,151523,-183
2011-10-10 10:46:00,1-1-13-1-8,151517,2
2011-10-10 10:46:00,1-1-13-1-9,151512,3FE51469AAAA01
2011-10-10 10:46:00,1-1-13-1-10,151513,BVM5400CRAO00240VA
2011-10-10 10:46:00,1-1-13-1-11,151519,11984
2011-10-10 10:46:00,1-1-13-1-12,11124,0
2011-10-10 10:46:00,1-1-13-1-13,151516,0
2011-10-10 10:46:00,1-1-13-1-14,151514,0
2011-10-10 10:46:00,1-1-13-1-15,121255,4673
2011-10-10 10:46:00,1-1-13-1-16,121256,333
2011-10-10 10:46:00,1-1-13-1-17,121254,50
2011-10-10 10:46:00,1-1-13-1-18,121257,16
2011-10-10 10:46:00,1-1-13-1-19,151511,WILDCARD

regards
ygemici
# 3  
Old 10-12-2011
I need the same set of data for all the n numbers

eg :
2011-10-10 10:46:00,1-1-13-1-1,151510,ALCLA0A84D2C
2011-10-10 10:46:00,1-1-13-1-1,151520,65537
2011-10-10 10:46:00,1-1-13-1-1,151515,46932
2011-10-10 10:46:00,1-1-13-1-1,151521,32769

2011-10-10 10:46:00,1-1-13-1-2,151510,ALCLA0A84D2C
2011-10-10 10:46:00,1-1-13-1-2,151520,65537
2011-10-10 10:46:00,1-1-13-1-2,151515,46932
2011-10-10 10:46:00,1-1-13-1-2,151521,32769

2011-10-10 10:46:00,1-1-13-1-3,151510,ALCLA0A84D2C
2011-10-10 10:46:00,1-1-13-1-3,151520,65537
2011-10-10 10:46:00,1-1-13-1-3,151515,46932
2011-10-10 10:46:00,1-1-13-1-3,151521,32769
# 4  
Old 10-12-2011
Please post the input file.
# 5  
Old 10-12-2011
Something like this?

Code:
awk -F, -v val=10 'NR==FNR{a[$0]++;next} 
END{while(++j<=val){for(i in a){gsub(/-[0-9]*,/,"-"j",",i);print i}}}' input_file

val = no of times you want to repeat

If Solaris, use nawk

--ahamed

Last edited by ahamed101; 10-12-2011 at 03:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to use xargs to repeat as a loop to grab date string?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: My goal to find how many requests in 14 days from weblog server. I know to cat a weblog file to wc -l to find the... (8 Replies)
Discussion started by: scopiop
8 Replies

2. 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

3. Homework & Coursework Questions

How to use loop to repeat task?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. How can i use loop to repeat task. 2.shirt=15 black=13.50 echo "how many shirt you want" read num echo echo "Please enter a choice" echo "1 ---> normal... (5 Replies)
Discussion started by: Tauatioti
5 Replies

4. Shell Programming and Scripting

Ping and repeat ?

How do i write a loop ping to see if it get timeout or hang ? it should loop every 30 second to ping a server ? ping -c 5 -t 15 www.google.com if ]; then date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /home/sabercats/checkconnection.log else date '+%Y-%m-%d %H:%M:%S Connection... (3 Replies)
Discussion started by: sabercats
3 Replies

5. Shell Programming and Scripting

case loop... repeat on bad input?

I'm trying to get a case statement to start over if an undefined option is selected... But I am ata loss on how to actually do it. Here is a quick example of what I have. Echo "1) do this/n 2) Do that/n 3) Quit/n Make a selection/n" Read answer Case answer in 1) Dothid;; 2) Dothat;;... (3 Replies)
Discussion started by: trey85stang
3 Replies

6. UNIX for Dummies Questions & Answers

repeat each record n times

I have: aa01 aa02 aa03 aa04 ab01 ab02 ab03 ab04 I would like each record printed 5 times: aa01 aa01 aa01 aa01 aa01 aa02 aa02 (6 Replies)
Discussion started by: kenneth.mcbride
6 Replies

7. Shell Programming and Scripting

to copy and repeat

Hi All, I have done some looking at other threads but haven't found quite what I am looking for. I am a newbie to scripting and haven't got to where I want to you but here is my basic question. I have a script to copy a file and send it to another file with a date and time stamp. What I want to... (4 Replies)
Discussion started by: falcondown01
4 Replies

8. 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

9. UNIX for Dummies Questions & Answers

any idea to repeat a action in VI

Any idea to repeat an action to all the lines in vi... suppose i want to delete the first word from all the lines in VI .. how would i do it ? in general i am also looking for a way to apply a action to all the lines in VI . (6 Replies)
Discussion started by: myelvis
6 Replies

10. UNIX for Dummies Questions & Answers

Repeat a command in a shell

Hi there, i would like to repeat a command in a shell sript (bash) the script starts with a menu to choose a menu point to do something .... on the end of the script i would like to restart the programm to choose the menu points on the beginning. I would also make a sript that send... (2 Replies)
Discussion started by: scotty
2 Replies
Login or Register to Ask a Question