repeat each record n times


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers repeat each record n times
# 1  
Old 09-18-2008
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
aa02
aa02
aa02
aa03
aa03
aa03
aa03
aa03
aa04
aa04
aa04
aa04
aa04
ab01
ab01
ab01
ab01
ab01
ab02
ab02
ab02
ab02
ab02
ab03
ab03
ab03
ab03
ab03
ab04
ab04
ab04
ab04
ab04

Thank You,
Kenny.
# 2  
Old 09-18-2008
Quote:
awk '{print $0"\n"$0"\n"$0"\n"$0"\n"$0}' filename
there is more efficient way than this ...
# 3  
Old 09-18-2008
You may want to read up on 'for' and 'while' loops. What language is this in?
# 4  
Old 09-18-2008
Code:
while read line
 do 
   for i in 1 2 3 4 5
     do 
      echo $line
     done
 done < file

or in awk:

Code:
awk '{for(i=1;i<=5;i++) print }' file

# 5  
Old 09-19-2008
Neither of those solutions would work, because he is iterating by two values - X and Y. He needs to use a nested for statement, which is why I asked what language this is.

Either way, I have a funny feeling this is a homework assignment... Call me naive...
# 6  
Old 09-19-2008
On the contrary, both solutions would work since the stated aim is to repeat each line five times.

Here's my effort...
Code:
paste -d '\n' $(yes file1|head -5)

...or...
Code:
sed 'p;p;p;p' file1

# 7  
Old 09-19-2008
And mine:

Code:
perl -pe'$_=$_ x5' file

Code:
ruby -pe'4.times{print}' file

Code:
awk '{while(++i<6)print;i=0}' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need code for updating second record to first record in shell scripting

Hi,, I have requirement that i need to get DISTINCT values from a table and if there are two records i need to update it to one record and then need to submit INSERT statements by using the updated value as a parameter. Here is the example follows.. SELECT DISTINCT ID FROM OFFER_GROUP WHERE... (1 Reply)
Discussion started by: Samah
1 Replies

2. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

3. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

4. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

5. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

7. AIX

how would you know your server was rebooted 3 times or 5 times

Is there such location or command to know how many times did you reboot your server in that particular day?in AIX. (3 Replies)
Discussion started by: kenshinhimura
3 Replies

8. UNIX for Advanced & Expert Users

Print Full record and substring in that record

I have i got a requirement like below. I have input file which contains following fixed width records. 00000000000088500232007112007111 I need the full record and concatenated with ~ and characters from 1to 5 and concatenated with ~ and charactes from 10 to 15 The out put will be like... (1 Reply)
Discussion started by: ukatru
1 Replies

9. Shell Programming and Scripting

splitting a record and adding a record to a file

Hi, I am new to UNIX scripting and woiuld appreicate your help... Input file contains only one (but long) record: aaaaabbbbbcccccddddd..... Desired file: NEW RECORD #new record (hardcoded) added as first record - its length is irrelevant# aaaaa bbbbb ccccc ddddd ... ... ... (1 Reply)
Discussion started by: rsolap
1 Replies
Login or Register to Ask a Question