Copy Column Multiple Times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy Column Multiple Times
# 1  
Old 11-04-2011
Copy Column Multiple Times

Hello,
I wonder if it my problem can be solved. Inside File1.txt, there are 3 columns. Inside File 2.txt, contain certain variable(in this case, only "3"). So I want to : (copy File 1 x Variable in File 2). Expected result are File 3.txt.
Any help are really appreciated.

File 1.txt
Code:
-92.033 -105.36 -5.38472
-34.8985 -103.205 -2.71757

File 2.txt
Code:
3
3

File 3.txt
Code:
-92.033 -105.36 -5.38472 -92.033 -105.36 -5.38472 -92.033 -105.36 -5.38472
-34.8985 -103.205 -2.71757 -34.8985 -103.205 -2.71757 -34.8985 -103.205 -2.71757


Last edited by Franklin52; 11-04-2011 at 05:54 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 11-04-2011
Try this...
Code:
awk 'NR==FNR{_1[$0]++;next}{for(i in _1){for(j=1;j<=i;j++){printf $0 FS}printf "\n"}}' 
File2.txt File1.txt > File3.txt

--ahamed
# 3  
Old 11-04-2011
The script succeed to copy into File3.txt , but the multiplying number (based on variable File2.txt) is not correct. In above case, I try using "3", and it is perfectly working. But when I change the variable File2.txt into "9", it multiplies it into 89 times..
# 4  
Old 11-04-2011
A small change... Try this...
Code:
awk 'NR==FNR{_1[$0]++;next}{for(i in _1){for(j=1;j<=i+0;j++){printf $0 FS}printf "\n"}}'  
File2.txt File1.txt > File3.txt

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 11-04-2011
Working like a charm..
I wonder about the meaning of this command. I really want to learn.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. UNIX for Dummies Questions & Answers

Append no of times a column is repeated at the end

Hi folks, Iam working on a bash script, i need to print how many times column 2 repeated at the end of each line. Input.txt COL1 COL2 COL3 COL4 1 XX 45 N 2 YY 34 y 3 ZZ 44 N 4 XX 89 Y 5 XX 45 N 6 YY 84 D 7 ZZ 22 S Output.txt COL1 COL2 COL3 COL4 COL5 1 XX 45 N 3 2 YY 34... (6 Replies)
Discussion started by: tech_frk
6 Replies

3. Shell Programming and Scripting

Copy n paste n times

I have one mainframe copy book where I want to copy n times depend on occurs which mention below. Example: Below highlighted row mention “occurs 2 times” so I need to copy 2 times till next label 10. C14992 10 FILLER PIC X(2835). 01 ... (7 Replies)
Discussion started by: srivalli
7 Replies

4. UNIX for Dummies Questions & Answers

How to copy set of lines n times?

I have a file with following data A B C I would like to print like this n times(For eg:n times) A B C A B C A B C A B C A (3 Replies)
Discussion started by: nsuresh316
3 Replies

5. UNIX for Advanced & Expert Users

Copy a column to another column in UNIX fixedwidth file

Hi All, I have a fixedwidth file of length 3000. Now i want to copy a column of 4 chars i.e( length 1678-1681) to column 1127 – 1171 to the same file. Please let me know how can i achive using a single command in fixed width file. Also source column length is 4 chars and target column length... (4 Replies)
Discussion started by: kiranparsha
4 Replies

6. Shell Programming and Scripting

Print each column 3 times

Hi All, I have a file with more than 2000 columns and I would like to print each column 3 times, so that I will get a file like col1 col1 col1 col2 col2 col2 ........coln coln coln. I have tried the following code: awk '{for(i=1; i<=NF; i++) {s=s FS $i,$i,$i} print s;s=""}' input >... (2 Replies)
Discussion started by: Fredrick
2 Replies

7. Shell Programming and Scripting

need help using one variable multiple times

I apologize for the title but I am not even sure myself what to call this. I am going to use an example of a pizza delivery. I need to make an interactive script that allows users to order a certain number of pizzas, and then choose what they want on each pizza. Here is my code so far.... ... (1 Reply)
Discussion started by: cstadnyk1
1 Replies

8. Shell Programming and Scripting

Running Multiple Times ?

hey mates, I was wondering if someone could assist me with this one, I have couple scripts that I would like to invoke from .sh ( One after another ) Script1 Script2 Script3 Is it possible to run script 2 after script one finished running ? And again start script 3 after script 2... (6 Replies)
Discussion started by: NDxiak
6 Replies

9. Shell Programming and Scripting

Copy 1 file 5000 times and Rename each +1

Hi, Found lots of stuff that is close but no cigar... I have a file ie. a.txt, and I want to copy it to another directory 5000 times and call it: a1.txt a2.txt ... a5000.txt Struggling to put a loop together in this new world of AIX. please include full script for me to understand... (3 Replies)
Discussion started by: terry2009
3 Replies

10. Shell Programming and Scripting

Doing one thing multiple times

Hi, I wrote a awk line here: awk 'BEGIN {OFS="\t"} {print $0, int(rand()*($2-$1 + 1) + $1) }' filename Basically what it does is that it takes the start (column 1) and stop (column 2) and generates a random # in between start and stop. I want to take this a step further and have it... (2 Replies)
Discussion started by: phil_heath
2 Replies
Login or Register to Ask a Question