using field 2 in file2 to complete field 3 in file1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using field 2 in file2 to complete field 3 in file1
# 1  
Old 11-02-2011
using field 2 in file2 to complete field 3 in file1

Hello,

I was hoping someone could help me with this work related problem...

basically what I want to do is the following:

file2:
Code:
1 o
2 t
4 f
5 v
7 n
8 e
10 a

file1:
Code:
1 :
2 :
3 :
4 :
5 :
6 :
7 :
8 :
9 :
10 :

Output:
Code:
1 : o
2 : t
3 :
4 : f
5 : v
6 :
7 : n
8 : e
9 :
10 : a

I am using "foreach" to implement this and it works fine when the number of lines is low, but when I hit numbers in the thousands the script becomes really slow.
I am sure there is a faster way of doing this using awk or sed but just dont know the command.
if you could also provide an explanation of the command that implements this I would appreciate it.


Thanks,

Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by zaxxon; 11-02-2011 at 05:40 AM.. Reason: code tags, see PM
# 2  
Old 11-02-2011
Code:
awk 'NR==FNR {_[$1]=$2; next} _[$1] {print $0 FS _[$1]; next}1 ' file2 file1
1 : o
2 : t
3 :
4 : f
5 : v
6 :
7 : n
8 : e
9 :
10 : a

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 11-02-2011
If the real files are sorted you should just be able to use join:
Code:
join -a1 file1 file2

(untested)
This User Gave Thanks to CarloM For This Post:
# 4  
Old 11-02-2011
Thanks guys, I will try both methods and let you know.

---------- Post updated at 04:44 AM ---------- Previous update was at 04:12 AM ----------

Hi zaxxon, this is what I got when I tried your method:

Code:
 
> awk 'NR==FNR {_[$1]=$2; next} _[$1] {print $0 FS _[$1]; next}1 ' Site_Package.txt user_input.txt
awk: syntax error near line 1
awk: bailing out near line 1
> echo $SHELL
/usr/bin/tcsh
>

Hi CarloM,
Your method also didnt work for me:

Code:
 
> join -al user_input.txt Site_Package.txt 
usage: join [-a file_number | -v file_number] [-o list [-e string]]
[-t char] [-1 field] [-2 field] file1 file2
join [-a file_number] [-j field] [-j1 field] [-j2 field]
[-o list [-e string]] [-t char] file1 file2

any other way of doing it?

Last edited by smarones; 11-02-2011 at 06:54 AM..
# 5  
Old 11-02-2011
Quote:
Originally Posted by smarones
Hi zaxxon, this is what I got when I tried your method:

> awk 'NR==FNR {_[$1]=$2; next} _[$1] {print $0 FS _[$1]; next}1 ' Site_Package.txt user_input.txt
awk: syntax error near line 1
awk: bailing out near line 1
> echo $SHELL
/usr/bin/tcsh
>
Use nawk or /usr/xpg4/bin/awk on Solaris.
This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 11-02-2011
Works like a charm when using nawk, thanks alot guys Smilie
# 7  
Old 11-02-2011
Quote:
Originally Posted by smarones
Hi CarloM,
Your method also didnt work for me:

Code:
 
> join -al user_input.txt Site_Package.txt

That's a one, not the letter L.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to look up values in File 2 from File 1, & printingNth field of File1 based value of File2 $2

I have two files which are the output of a multiple choice vocab test (60 separate questions) from 104 people (there are some missing responses) and the question list. I have the item list in one file (File1) Item,Stimulus,Choice1,Choice2,Choice3,Choice4,Correct... (5 Replies)
Discussion started by: samonl
5 Replies

2. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to update field in file2 if not the same as file1

Trying to use awk to: update $2 in file2 with the $2 value in file1, if $1 in file1 matches $13 in file2, which is tab-delimeted. The $2values may already be the same so in that case nothing happens and the next line is processed. There are exactly 4,605 unique $13 values. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

Replacing first field of file2 with the second filed of file1 for matching cases

Dear All, Need your help..:D I am not regular on shell scripts..:( I have 2 files.. Content of file1 cellRef 4};"4038_2_MTNL_KALAMBOLI" cellRef 1020};"4112_3_RAINBOW_BLDG" cellRef 134};"4049_2_TATA_HOSPITAL" cellRef 1003};"4242_3_HITESH_CONSTRUCTION" cellRef... (6 Replies)
Discussion started by: ailnilanjan
6 Replies

6. Shell Programming and Scripting

Match complete field

hello, i have a little problem, i want match the complete field ($1 or $2) with a complete word in another variable. example: i have a file with either one or two words per lane: hsa-mir-4449 hsa-mir-4707 hsa-mir-4707* hsa-mir-4707 novelMiR_3551 novelMiR_3563 novelMiR_4330... (4 Replies)
Discussion started by: ace13
4 Replies

7. Shell Programming and Scripting

Retreive the records from file2 by using the first field in file1

Hi Freinds, i have a file1 as below file1 1|ndmf|fdd|d3484|34874 2|jdehf|wru7|478|w489 3|dfkj|wej|484|49894 file2 contains lakhs of records and not in sorted order i want to retrive only the records from file2 by searcing the first field of file 1 i used grep ^1 file2... (4 Replies)
Discussion started by: i150371485
4 Replies

8. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

9. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

10. Shell Programming and Scripting

Read Field from file1 and find and replace in file2

Hi All, I have file1 line below: $myName$|xxx Now I need to read the file1 and find for $myName$ in file2 and replace with xxx file1: $myName$|xxx file2: My name is $myName$ expected output in file2 after executing the script is below: my name is xxx Thanks, (8 Replies)
Discussion started by: gdevadas
8 Replies
Login or Register to Ask a Question