How to use Data of one file in another file???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use Data of one file in another file???
# 1  
Old 07-14-2010
How to use Data of one file in another file???

Hello People,,
I have two file file1.txt file2.txt,both file have content like following

file1.txt
Code:
v1 2 3 dc 5v
v2 4 5 dc 6v
v3 6 7 dc 2v

file2.txt
Code:
2
3
4


the number in last coloumn of file1.txt should be replaced by number in file2.txt as follows
Code:
v1 2 3 dc 2v<-----2 is coming from file2.txt
v2 4 5 dc 3v<-----3 is coming from file2.txt
v3 6 7 dc 4v<-----4 is coming from file2.txt


I do not know how to do it.I want to write a Shell script for it.Please help.Smilie,

Last edited by Franklin52; 07-14-2010 at 12:19 PM.. Reason: Please use code tags
# 2  
Old 07-14-2010
If the letter in the last field is always a "v":
Code:
awk '{getline s < "file2.txt"; $NF=s "v"}1' file1

Otherwise:
Code:
awk '{getline s < "file2.txt"; sub(int($NF),"",$NF);$NF=s $NF}1' file1.txt


Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.
# 3  
Old 07-15-2010
Hi Franklin

Code:
awk '{getline s < "file2.txt"; $NF=s "v"}1' file1.txt

it worked for me,Thanks

can you please let me know $NF=s "v"}1 what this part is doing i mean what this 1 is doing in last because without 1 in output nothing is coming

Last edited by Scott; 07-15-2010 at 07:27 AM.. Reason: Code tags, please...
# 4  
Old 07-15-2010
simple perl may help you

Code:
my @arr=qw/2 3 4/;
while(<DATA>){
	s/([0-9]+)(?=[a-z]+$)/$arr[$.-1]/e;
	print;
}
__DATA__
v1 2 3 dc 5v
v2 4 5 dc 6v
v3 6 7 dc 2v

# 5  
Old 07-15-2010
Quote:
Originally Posted by arvindk.monu
Hi Franklin

Code:
awk '{getline s < "file2.txt"; $NF=s "v"}1' file1.txt

it worked for me,Thanks

can you please let me know $NF=s "v"}1 what this part is doing i mean what this 1 is doing in last because without 1 in output nothing is coming
An awk statement has the form:
Code:
condition {action}

Conditions in awk control the execution of actions and actions are executed when the condition is true.

If the condition is true (1 is true) and there are no actions between braces, awk prints the current record by default.

Regards
This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 07-15-2010
Code:
awk 'NR==FNR{a[NR]=$1;next}{$NF=a[FNR]"v"}1' file2.txt file1.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

2. Shell Programming and Scripting

Replace data of a file with data from another file using shell scripting.

Dears, I'm new to shell scripting and i was wondering if you can help me with following matter. I have a file containing 400,000 records. The file contains two columns like: 00611291,0270404000005453 25262597,1580401000016155 25779812,1700403000001786 00388934,1200408000000880... (1 Reply)
Discussion started by: paniklas
1 Replies

3. Shell Programming and Scripting

Extract header data from one file and combine it with data from another file

Hi, Great minds, I have some files, in fact header files, of CTD profiler, I tried a lot C programming, could not get output as I was expected, because my programming skills are very poor, finally, joined unix forum with the hope that, I may get what I want, from you people, Here I have attached... (17 Replies)
Discussion started by: nex_asp
17 Replies

4. UNIX for Dummies Questions & Answers

Mapping a data in a file and delete line in source file if data does not exist.

Hi Guys, Please help me with my problem here: I have a source file: 1212 23232 343434 ASAS1 4 3212 23232 343434 ASAS2 4 3234 23232 343434 QWQW1 4 1134 23232 343434 QWQW2 4 3212 23232 343434 QWQW3 4 and a mapping... (4 Replies)
Discussion started by: kokoro
4 Replies

5. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

6. Shell Programming and Scripting

Find and replace data in text file with data in same file

OK I will do my best to explain what I need help with. I am trying to format an ldif file so I can import it into Oracle oid. I need the file to look like this example. Keep in mind there are 3000 of these in the file. changetype: modify replace: userpassword dn:... (0 Replies)
Discussion started by: timothyha22
0 Replies

7. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies

8. Shell Programming and Scripting

Post Shell programming: Question about source a file and read data from the file

This is shell programming assignment. It needs to create a file called .std_dbrc contains STD_DBROOT=${HOME}/class/2031/Assgn3/STD_DB (which includes all my simple database files) and I am gonna use this .std_dbrc in my script file (read the data from the database files) like this: .... (3 Replies)
Discussion started by: ccwq
3 Replies

9. Shell Programming and Scripting

Shell Script to Load data into the database using a .csv file and .ctl file

Since i'm new to scripting i'm findind it difficult to code a script. The script has to be an executable with 2 paramters passed to it.The Parameters are 1. The Control file name(.ctl file) 2. The Data file name(.csv file) Does anybody have an idea about it? :confused: (3 Replies)
Discussion started by: Csmani
3 Replies
Login or Register to Ask a Question