Copying a single value to the entire column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying a single value to the entire column
# 1  
Old 09-03-2015
Copying a single value to the entire column

Hello everone,

I have a text file as input, which lokks like for exemple:
Code:
#     #    #
12   2    3
12
12
12
12
12
12
12

It has three column. My question is, i want to copy the value of 2nd column, 1st row to the entire 2nd column. similarly for the third column also. My result should look like:
Code:
#     #     #
12   2     3
12   2     3
12   2     3
12   2     3
12   2     3
12   2     3
12   2     3
12   2     3

Actually i am dealing with large number of rows and columns and the values are automatically generated. I would e very glad if any one could help me to solve this problem. Thanks Smilie

Last edited by dinesh.n; 09-04-2015 at 04:53 AM..
# 2  
Old 09-03-2015
Hello dinesh.n,

Welcome to forum, following may help you in same.
Code:
awk 'NR==1{A=$2 OFS $3;print;next}{;$0=$0 OFS A;print}' Input_file

Output will be as follows.
Code:
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3

EDIT: Adding one more solution to same.
Code:
awk '{$2=NF>1?$2:A;$3=NF>2?$3:B;A=$2;B=$3} 1'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-03-2015 at 09:17 AM.. Reason: Added one more solution for same too.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-03-2015
Please use code tags as required by forum rules!

How about
Code:
awk 'NR==2 {split ($0, T); MX=NF} NR>2 {for (i=2; i<=MX; i++) $i=T[i]} 1' file
# # #
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3

---------- Post updated at 14:19 ---------- Previous update was at 14:13 ----------

Or
Code:
awk '{print $0, T}  NR==2 {sub ($1 FS, ""); T=$0} ' file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 09-03-2015
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

How about
Code:
awk 'NR==2 {split ($0, T); MX=NF} NR>2 {for (i=2; i<=MX; i++) $i=T[i]} 1' file
# # #
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3
12 2 3

Hello RudiC,

Little modification to your code as above will not copy fields from line 2nd to last line.
Code:
awk 'NR==1 {split ($0, T); MX=NF} NR>=2 {for (i=2; i<=MX; i++) $i=T[i]} 1' Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-03-2015
Thank you so much guys. You guys are awesome. I am very new to this forum and i would like to learn alot from you all. SmilieSmilie
# 6  
Old 09-03-2015
You are welcome Dinesh, trust me this is one of the BEST forums I have seen till date for learning UNIX and related stuff.
You are very much welcome to join the learning camp. Enjoy learning Smilie

Thanks,
R. Singh
"Where there is a SHELL there is a way."
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 09-03-2015
Quote:
Originally Posted by RavinderSingh13
Hello RudiC,

Little modification to your code as above will not copy fields from line 2nd to last line.
Code:
awk 'NR==1 {split ($0, T); MX=NF} NR>=2 {for (i=2; i<=MX; i++) $i=T[i]} 1' Input_file

Thanks,
R. Singh
I don't understand - applying your proposal to the sample file yields
Code:
# # #
12 # #
12 # #
12 # #
12 # #
12 # #
12 # #
12 # #
12 # #

which doesn't seem to be the desired output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

2. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

3. UNIX for Dummies Questions & Answers

Fill csv entire column with content from another text file

I have a csv that looks like this: ,yude-to-nap2,0,0,0,0,0 ,2twis-yude-to-nap2,0,0,0,0,0 ,2tiws-yude-to-nap2,0,0,0,0,0 ,2arcos-yude-to-nap2,0,0,0,0,0 and another file named m1 that has a single line of text as content: Feb 1 15:30:20 How can I fill the whole the empty column of the... (1 Reply)
Discussion started by: RobertoRivera
1 Replies

4. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

5. Shell Programming and Scripting

Delete an entire column from a tab delimited file

Hi, Can anyone please tell me about how we can delete an entire column from a tab delimited file? Mu input_file.txt looks like this: And I want the output as: I used the below code nawk -v d="1" 'BEGIN{FS=OFS="\t"}{$d=""}{print}' input_file.txtBut in the output, the first column is... (5 Replies)
Discussion started by: sampoorna
5 Replies

6. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

7. UNIX for Advanced & Expert Users

How do I standardise an entire column?

I have a kind of a multi-step problem: What I want to do is: To find the maximum value in the second column, divide the entire column with this value and multiply with a user defined variable (e.g. x=400). The result should be written to a new file. I.e., find max value of column 2 and perform... (5 Replies)
Discussion started by: Medova
5 Replies

8. Shell Programming and Scripting

Print entire line based on value in a column

Friends, File1.txt abc|0|xyz 123|129|opq def|0|678 890|pqw|sdf How do I print the entire line where second column has value is 0? Expected Result: abc|0|xyz def|0|678 Thanks, Prashant ---------- Post updated at 02:14 PM ---------- Previous update was at 02:06 PM ---------- ... (1 Reply)
Discussion started by: ppat7046
1 Replies

9. UNIX for Advanced & Expert Users

Updating entire column irrespective of any data in a file

Hi, I have a file A.txt (tab separated) as below: pavan chennai/tes/bangalore 100 sunil mangalore/abc/mumbai 230 kumar delhi/nba/andhra 310 I want to change only second column as below . Rest of columns as it is ;The ouput file is also tab... (4 Replies)
Discussion started by: kpavan2004
4 Replies

10. Shell Programming and Scripting

To cut entire column from a file and apend it to another file as another column

file1.txt : india pakistan bangladesh japan canada africa USA srilanka Nepal file2.txt Delhi Tokyo washington I have to cut the first column of file1.txt and apend it with file2.txt as another column like this Delhi india Tokyo japan washington USA ... (4 Replies)
Discussion started by: sakthifire
4 Replies
Login or Register to Ask a Question