Replace two values in a file with input from two different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace two values in a file with input from two different files
# 1  
Old 05-16-2014
Replace two values in a file with input from two different files

Hi,

I was having the following issue

Code:
cat input

hello1, my name is unix.com. I am awesome. Hope you know this, hello2!

Code:
cat hello1.txt
Hi Friends
Hi Folks
Hi Well-Wishers

Code:
cat hello2.txt
Honey
Sweety
Darling

Required Output

1. I should be getting three files, which is nothing but the same as wc -l of hello1.txt and hello2.txt.
3. Each line in hello1.txt is related to the same line in hello2.txt. Which means, Hi Friends is related to Honey. Hi Folks is related to Sweety and Hi Well-Wishers is related to Darling.
4. The three files will be like this


Code:
cat output1
Hi Friends, my name is unix.com. I am awesome. Hope you know this, Honey!

Code:
cat output2
Hi Folks, my name is unix.com. I am awesome. Hope you know this, Sweety!

Code:
cat output3
Hi Well-Wishers, my name is unix.com. I am awesome. Hope you know this, Darling!

Thanks in advance.
# 2  
Old 05-16-2014
try
Code:
 awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A[i]","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input

output
Code:
[Makarand] # awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A[i]","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input
Hi Friends,my name is unix.com. I am awesome. Hope you know this, Honey!
Hi Folks,my name is unix.com. I am awesome. Hope you know this, Sweety!
Hi Well-Wishers,my name is unix.com. I am awesome. Hope you know this, Darling!

for 3 files
Code:
awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A[i]","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input| | sed -n '1,1p'  > output1

Code:
awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A[i]","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input| sed -n '2,2p' > oouput2

Code:
awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A[i]","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input| sed -n '3,3p' > output3


Last edited by Makarand Dodmis; 05-16-2014 at 11:22 AM..
This User Gave Thanks to Makarand Dodmis For This Post:
# 3  
Old 05-16-2014
Quote:
Originally Posted by Makarand Dodmis
try
Code:
awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A[i]","A[c]A[i+3]}}' hello1.txt hello2.txt input

output
Code:
[Makarand] # awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A[i]","A[c]A[i+3]}}' hello1.txt hello2.txt input
Hi Friends,my name is unix.com. I am awesome. Hope you know this, Honey
Hi Folks,my name is unix.com. I am awesome. Hope you know this, Sweety
Hi Well-Wishers,my name is unix.com. I am awesome. Hope you know this, Darling

Makarand,

Thanks for the reply.

There are multiple hello1s and hello2s in the input file at on different lines. Like a paragraph filled with hello1 and hello2 and other text.

I just gave you a simple example.

Your code is not replacing all the instances of hello1 and hello2 and in addition it is printing the hello1 and hello2 into the output file. Any pointers, please?

Thanks a lot for your time.

Last edited by jacobs.smith; 05-16-2014 at 11:58 AM.. Reason: More info included.
# 4  
Old 05-16-2014
Could you please post the code you have now - the one that fails?
Then post exactly the output you want to get.

I do not get why you say wc -l should match for each file after you run your command.
# 5  
Old 05-16-2014
Quote:
Originally Posted by jim mcnamara
Could you please post the code you have now - the one that fails?
Then post exactly the output you want to get.

I do not get why you say wc -l should match for each file after you run your command.
Jim,

I was talking about the hello1.txt and hello2.txt file record numbers and not the output file counts.
# 6  
Old 05-16-2014
Okay - given what you said you want all three #1 records from each file output onto the same line in the new file. New file has three lines, each the result of concatenation.

That is what related means. In the interest of getting the thread going this code does that. But I'll bet it is not what you meant. Lacking examples of output you therefore get:

Code:
awk '{arr[FNR]=arr[FNR] $0} END {for(i in arr) {print arr[i]}}'  \
    file1 file2 file3 > file4

# 7  
Old 05-16-2014
Quote:
Originally Posted by jim mcnamara
Okay - given what you said you want all three #1 records from each file output onto the same line in the new file. New file has three lines, each the result of concatenation.

That is what related means. In the interest of getting the thread going this code does that. But I'll bet it is not what you meant. Lacking examples of output you therefore get:

Code:
awk '{arr[FNR]=arr[FNR] $0} END {for(i in arr) {print arr[i]}}'  \
    file1 file2 file3 > file4

No, Jim. You got me wrong.

I want to replace hello1 in my input with the content from hello1.txt. And hello2 from my input in hello2.txt.

So, for each line in hello1.txt and hello2.txt, and each presence of hello1 and hello2 in the input, I want separate files.

Thanks for your time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace Stub Values In One Group Of Files With Actual Values From Another Group Of Files

I have two directories of files (new-config-files and old-config-files): new-config-files/this-db/config.inc.php new-config-files/that-db/config.inc.php new-config-files/old-db/config.inc.php new-config-files/new-db/config.inc.php new-config-files/random-database/config.inc.php etc. ... (4 Replies)
Discussion started by: spacegoose
4 Replies

2. Shell Programming and Scripting

Match value in two files and replace values in selected columns

The purpose is to check if values for column 3 and 4 in file1 match with column 1 in file2. If any value match do: 1) Replace values in file2 for column 2 and 3 using the information of file1 columns 5 and 6 2) Replace string ($1,1,5) and string ($1,6,5) in file2 with values of columns 7... (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

Replace values between 2 files

I want to replace the third and fourth lines of a 2nd file by the first two lines of a file. Input: file_1 file_1.line_1 file_1.line_2 file_2 file_2.line_1 <file_2.line_2_blank> file_2.line_3 file2.line_4 <file_2.line_5_blank> Output: file_2.line1 <file_2.line_2_blank>... (1 Reply)
Discussion started by: arpagon
1 Replies

4. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

5. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

6. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

7. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

8. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

9. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

10. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies
Login or Register to Ask a Question