Replicating many rows in unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replicating many rows in unix
# 1  
Old 12-08-2011
Replicating many rows in unix

Hi

If my input is

Code:
abcdefgh
ijklmnop

then

the output should be:


Code:
abcdefgh
abcdefgh
abcdefgh
abcdefgh
ijklmnop
ijklmnop
ijklmnop
ijklmnop

i.e.One row in the input becomes 4 records in the output.How we can achieve this in Unix?
# 2  
Old 12-08-2011
Many different ways, here's one:

Code:
awk ' { for( i = 0; i < 3; i++ ) print; }' input-file-name

These 2 Users Gave Thanks to agama For This Post:
# 3  
Old 12-08-2011
Fine.

If you want to add some characters with replication:
in my case,if i need the output as:

Code:
abcdefgh12
abcdefgh15
abcdefgh17
abcdefgh19
abcdefgh21
ijklmnop12
ijklmnop15
ijklmnop17
ijklmnop19
ijklmnop21

Those 12,15,17,19,21wil be always constant.

How we can achieve this?

Thanks
# 4  
Old 12-08-2011
Easiest way if they are all constant:

Code:
awk 'BEGIN { split( "12 15 17 19", add, " " ); } { for( i = 1; i < 5; i++ ) print $0 add[i]; }'

This User Gave Thanks to agama For This Post:
# 5  
Old 12-08-2011
Thanks Agama!

So i can use:

Code:
awk 'BEGIN { split( "12 15 17 19 21", add, " " ); } { for( i = 1; i < 5; i++ ) print $0 add[i]; }'

# 6  
Old 12-08-2011
Yep. What ever you pass in the string to split() will be added to the end of the respective lines of output, in the order. The numbers can be text, as long as you separate them with spaces. Easy to change if you need spaces, but I think that will work for you as is.
# 7  
Old 12-08-2011
Actually as per my requirement, i need to add those values in a comma delimited record as last field.
I believe the below will work in that case:
Code:
awk 'BEGIN { split( ",12 ,15 ,17 ,19 ,21", add, " " ); } { for( i = 1; i < 5; i++ ) print $0 add[i]; }'

Thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Glusterfs not replicating

0 down vote favorite I have replicated glusterfs on two servers, if i create file on one server it doesnt reflect on other, if i try to heal the volume on node2 i get this error Commit failed on node2. Please check log file for details. glustershd.log E 0-test-client-3: connection to... (0 Replies)
Discussion started by: davidbob
0 Replies

2. Shell Programming and Scripting

Replicating certain lines in a textfile

I am very new to to shell scripting and facing a problem that I can't seem to solve. I want to write a bash script that edits file1.txt and saves it as file2.txt. This is what the files should look like: file1: textline1 textline2 startCopy copyThis endCopy textline3 textline4 file2: ... (6 Replies)
Discussion started by: sandy90
6 Replies

3. UNIX for Dummies Questions & Answers

Columns to rows conversion in unix

Hi All, My requirement is to convert the rows to columns, please help me how to do in one command. Ex: source file is having data like ABC,XYZ,123,987,KKK,XXX,666 Need output like ABC XYZ 987 KKK XXX 666 Regards, Pavan. (3 Replies)
Discussion started by: madsongtel
3 Replies

4. Shell Programming and Scripting

Replicating jobs, renaming outout

Can anyone tell me if it's possible to write a script that will repeat the same job several times but give the output a slightly different name each time (i.e. change or add a number at the end of the output file)? Here is the script I use to run a single job: #!/bin/bash #PBS -N job0 #PBS -l... (1 Reply)
Discussion started by: peterjb100
1 Replies

5. Shell Programming and Scripting

Replicating rows by splitting column in text file

Hi, I have flat file with following format Col1, Col2, Col3, Col4 --------------------------------- r1_c1, r1_c2, r1_c3, abc | def | efg r2_c1, r2_c2, r2_c3, abcwdw | dweweef | efg | ijk r3_c1, r3_c2, r3_c3, abaac ........... The first three columns contain only one entry per... (3 Replies)
Discussion started by: nick2011
3 Replies

6. Shell Programming and Scripting

Find count of rows in excel sheet in Unix

Hi, Is there a way to find the count of number of rows of a.txt please? Where a.txt is as follows: /usr/bin/uuencode /tmp/a.csv a.csv > /tmp/a.txt #a.csv is a comma separated variable file Cheers, Girish. (7 Replies)
Discussion started by: girish1428
7 Replies

7. Shell Programming and Scripting

How to check count of rows in excel sheet in Unix

Hi, Would anyone be able to tell me how to check the number of rows in an excel sheet on unix box, please? Cheers, Girish. (2 Replies)
Discussion started by: girish1428
2 Replies

8. UNIX for Dummies Questions & Answers

Replicating content using sed

if we want to replicate the content of the file twice, then we can use sed 'p' filename In the same way, if i want to replicate the content thrice or 4 times, how we can achieve in SED? Thanks (5 Replies)
Discussion started by: pandeesh
5 Replies

9. AIX

Command for replicating a directory

Morning all, Is there a command on AIX that can replicate/synchronise two directories? I'm after functionality similar to robocopy on Windows. Scenario: I have a directory of Oracle forms for my production system and one for my dev system, these directories start off identical. The forms... (1 Reply)
Discussion started by: huggie
1 Replies

10. UNIX for Dummies Questions & Answers

replicating restricted sam users

I'm in the process of setting up two new HP-UX 11.23 i64 servers. On my existing server (HP-UX B.11.0) we have several users defined to have restricted sam access. I'm having trouble finding those definitions and copying them over to the new servers. Is this possible - to just copy over the... (1 Reply)
Discussion started by: LisaS
1 Replies
Login or Register to Ask a Question