Adding single value from one file into column of second file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding single value from one file into column of second file
# 1  
Old 04-30-2015
Adding single value from one file into column of second file

I have two files with the following content:

file1.txt looks like this (it always only have one single row):

Code:
ABC

file.txt looks like this (may have tons of rows):
Code:
123
456
789

I want to make a bash script that gives the following output:

Code:
ABC 123
ABC 456
ABC 789

So it takes the text in the first file and inserts that value in each of the rows in the second file. Can do this with some if/for statements but wonder if there is a quicker/shorter way of doing it? Have tried the "awk" and "paste" commands but just can't get what I want.

Cheers!
/Z

Last edited by Zooma; 04-30-2015 at 06:34 PM.. Reason: Corrected a typo.
# 2  
Old 04-30-2015
Hi,
with your sample:
Code:
awk -v A=$(<file1.txt) '$2=$1,$1=A' file.txt

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 05-13-2015
Thanks a lot. This works excellent when file.txt only has one column. Trying to add more columns there like this:

file.txt:
Code:
123 456 789
321 654 987
222 444 555

When I run your command (from #2) the result is:
Code:
ABC 123 789
ABC 321 987
ABC 222 555

So the second column in file.txt is dropped. I tried to do this instead:
Code:
awk -v A=$(<file1.txt) '$4=$3,$3=$2,$2=$1,$1=A' file.txt

That generates this error message:
Code:
awk: line 1: syntax error at or near ,

Have tried other ways to play around with the $-signs but just can't get it right. Any suggestions?

Thanks!
/Z
# 4  
Old 05-13-2015
Show us sample input for your new file.txt, file1.txt, and the output you want to produce from those two sample input files.

What operating system are you using?
# 5  
Old 05-13-2015
If post #3 did not produce an error then this should work, too.
Code:
awk -v A=$(<file1.txt) '{print A,$0}' file.txt

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 05-13-2015
Thanks a lot MadeInGermany, that did the trick!

Don, for the record here are my two files and the desired output (which I get with #5):

file1.txt
Code:
ABC

file.txt
Code:
123 456 789
321 654 987
222 444 555

And when running the command described in #5 I get this desired output:
Code:
ABC 123 456 789
ABC 321 654 987
ABC 222 444 555

Now, only because I'm curious, let's say that file1.txt instead would contain multiple columns and I want to insert all of them before the columns in file.txt. What would the awk command look like? Example:

file1.txt:
Code:
ABC DEF GHI

file.txt:
Code:
123 456 789
321 654 987
222 444 555

Desired output:
Code:
ABC DEF GHI 123 456 789
ABC DEF GHI 321 654 987
ABC DEF GHI 222 444 555

The command given in #5 doesn't solve this one. I'm using Linux and this is done in a bash script.

Thanks for all help!
/Z
# 7  
Old 05-13-2015
If you get an error message, prevent word splitting with quotes: ... A="$(<file1.txt)" ...
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Adding a new column to a csv file

Hi, Can anyone please explain me how can I add a new column to a Csv file ? Actaully I am setting one set of commands and creating a CSV file with 4 columns. I am executing another command and it's output should be added as column 5 with a column heading ? Can anyone explain how can... (26 Replies)
Discussion started by: rrb2009
26 Replies

3. Shell Programming and Scripting

Adding column values in a file

Hi, I am having a file in the following format. for aaaa 1111 1234 2222 3434 for bbbb 1111 3434.343 2222 2343 for cccc 3333 2343.343 4444 89000 for dddd 1111 5678.343 2222 890.3 aaaa 2343.343 bbbb 34343.343 (5 Replies)
Discussion started by: jpkumar10
5 Replies

4. Shell Programming and Scripting

Adding content of two file in a single file column wise

Hi, I am trying to get the file in particular pattern using shell script. I have to add one column to some other file. For example consider two file as below. File1: name1 name2 name3 File2: Add1 age1 Add2 age2 Add3 age3 I want this two file in a single file format something like... (3 Replies)
Discussion started by: diehard
3 Replies

5. Shell Programming and Scripting

Adding column in file

File contains 2 fields with tab delimeter. i want added the one column first of the file and incrementing as like sequence Sample file: Fname lanme raj rajkumar rani ranikumar output file should be. name Fname lanme 1 raj rajkumar 2 rani ranikumar Please help... (1 Reply)
Discussion started by: bmk
1 Replies

6. Shell Programming and Scripting

Adding a column to a file

Hello all, I need to add a coloumn at the 5th Position of a file, Can this be done using awk or sed. Sample Input 1008,300186,R,2009,0,2,2,3,2,0,1,1,1,1,2,1,1,0,2,1,1,0,2,2,2,2,0,0,0,0,1,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,3,2,3,1,1,1, Ouput: ... (4 Replies)
Discussion started by: Sri3001
4 Replies

7. Shell Programming and Scripting

adding quotes around each column in a csv file

I saved the csv file in a comma delimited format. Sample input input.csv 1 abc 2 2 def 4 3 ghi 6 4 jkl 8 5 mno 10 output.csv should look like this with single quotes around each field '1' 'abc' '2' '2' 'def' '4' '3' 'ghi' '6' '4' 'jkl' '8' '5' 'mno' '10' Please help me :confused:... (3 Replies)
Discussion started by: melannie
3 Replies

8. AIX

Adding column in a .txt file

Helle, I want to create a .ksh script in order to realize the following : I have a .txt file organized in a bloc of information, each bloc start with 000 as following: 000... 001... 003... 004... 000... 001... 003... 004... . . My aim is to add a new... (6 Replies)
Discussion started by: zainab2006
6 Replies

9. Shell Programming and Scripting

awk-adding a column to a file

Hello Friends, i used awk to sum up total size of files under a directory (with the help of examples, threads here). ls -l | awk '/^-/ {total += $5} END {printf "%15.0f\n",total}' >> total.txt After each execution of the script total result is appended into a text file: 7010 7794 8890 ... (7 Replies)
Discussion started by: EAGL€
7 Replies

10. Shell Programming and Scripting

FILE:Adding new column

Is it possible to add a new column in a file by matching a key element in second file ? File 1 2 a 3 b 4 d 5 g 6 j 7 m File 2 4 hjjjj 5 aaa 6 sasa 7 dsds 2 dsdf 3 fdsfg (2 Replies)
Discussion started by: sandeep_hi
2 Replies
Login or Register to Ask a Question