Sponsored Content
Top Forums Shell Programming and Scripting awk - replacing stings in file1 with column1 in file2 Post 302369517 by upstate_boy on Monday 9th of November 2009 12:53:52 AM
Old 11-09-2009
I see where I went wrong.. my examples were not accurate. My apologies.

in file2 (ref.txt) the values look more like this:

File2:
DD 3213
CCA 1231
AA BC 1112
ADFDFF 1232
DDF SCC 2131

I ran this:

awk 'FILENAME=="ref.txt"{A[$2]=$1}
FILENAME=="master.txt"{if(A[$1]){print A[$1]}else{print $0}}' ref.txt master.txt > output.txt

Any string in file 2 that had spaces (AA BC, DDF SCC) did not work for obvious reasons. I've regenerated file 2 to look like this now with sed:

DD:3213
CCA:1231
AA BC:1112
ADFDFF:1232
DDF SCC:2131

Is there any way I can use the awk script you gave me to recognize the columns separated by the colon? I can change the colon to any other value as well if there is something that will work better with awk.

Thanks for the help and patience.

---------- Post updated at 01:39 AM ---------- Previous update was at 01:18 AM ----------

I tried this but no lucK:

awk 'FILENAME=="ref.txt"{FS = : ; A[$2]=$1}
FILENAME=="master.txt"{if(A[$1]){print A[$1]}else{print $0}}' ref.txt master.txt > results.txt

awk: FILENAME=="ref.txt"{FS = : ; A[$2]=$1}
awk: ^ syntax error

---------- Post updated at 01:53 AM ---------- Previous update was at 01:39 AM ----------

Thank you for the help vidyadhar85.

I was able to get it to work with this:

Code:
awk '
BEGIN{FS=":"}
FILENAME=="ref.txt"{A[$2]=$1}
FILENAME=="master.txt"{if(A[$1]){print A[$1]}else{print $0}}' ref.txt master.txt > results.txt

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk Compare f1,f2,f3 of File1 with f1 of File2

I have an Awk string-compare problem and have searched the internet and forums for a solution i could use but cannot find a solution i understand to make work with my particular problem: I need to compare (field1 field2 field3 of File1) against (field1 of File2) and if they match print out... (6 Replies)
Discussion started by: RacerX
6 Replies

2. Shell Programming and Scripting

replacing text in file1 with list from file2

I am trying to automate a process of searching through a set of files and replace all occurrences of a formatted text with the next item in the list of a second file. Basically i need to replace all instances of T????CLK???? with an IP address from a list in a second file. the second file is one IP... (9 Replies)
Discussion started by: dovetail
9 Replies

3. Shell Programming and Scripting

Awk Compare File1 File2 on f2

I'm trying to compare two files using AWK, where if field2 of both files match, replace field1 of file1 with field1 of file2 and if there is no match just print the line of file1. file1.txt (has empty first field) :ABBATOM:B:H:1992 :ABBA TROJAN:B:H:1993 :ABBES FIRST HOPE:B:M:1997 :ABBEYS... (4 Replies)
Discussion started by: RacerX
4 Replies

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

5. Shell Programming and Scripting

[awk] split file1 and save it as var from file2

I have 2 files: file_1: file_2: expected result: name file: "artV1" "artV2" etc. I have: but why don;t work save to file 'out'?? (3 Replies)
Discussion started by: ffresz
3 Replies

6. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

7. Shell Programming and Scripting

Looking for lines, which is present in file1 but not in file2 using UNIX and awk

I have 2 files with 7 fields and i want to print the lines which is present in file1 but not in file2 based on field1 and field2. Logic: I want to print all the lines, where there is a particular column1 and column2. And we do not find the set of column1 and column2 in file2. Example: "sc2/10... (3 Replies)
Discussion started by: NamS
3 Replies

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

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

10. 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
GIT-SYMBOLIC-REF(1)						    Git Manual						       GIT-SYMBOLIC-REF(1)

NAME
git-symbolic-ref - Read, modify and delete symbolic refs SYNOPSIS
git symbolic-ref [-m <reason>] <name> <ref> git symbolic-ref [-q] [--short] <name> git symbolic-ref --delete [-q] <name> DESCRIPTION
Given one argument, reads which branch head the given symbolic ref refers to and outputs its path, relative to the .git/ directory. Typically you would give HEAD as the <name> argument to see which branch your working tree is on. Given two arguments, creates or updates a symbolic ref <name> to point at the given branch <ref>. Given --delete and an additional argument, deletes the given symbolic ref. A symbolic ref is a regular file that stores a string that begins with ref: refs/. For example, your .git/HEAD is a regular file whose contents is ref: refs/heads/master. OPTIONS
-d, --delete Delete the symbolic ref <name>. -q, --quiet Do not issue an error message if the <name> is not a symbolic ref but a detached HEAD; instead exit with non-zero status silently. --short When showing the value of <name> as a symbolic ref, try to shorten the value, e.g. from refs/heads/master to master. -m Update the reflog for <name> with <reason>. This is valid only when creating or updating a symbolic ref. NOTES
In the past, .git/HEAD was a symbolic link pointing at refs/heads/master. When we wanted to switch to another branch, we did ln -sf refs/heads/newbranch .git/HEAD, and when we wanted to find out which branch we are on, we did readlink .git/HEAD. But symbolic links are not entirely portable, so they are now deprecated and symbolic refs (as described above) are used by default. git symbolic-ref will exit with status 0 if the contents of the symbolic ref were printed correctly, with status 1 if the requested name is not a symbolic ref, or 128 if another error occurs. GIT
Part of the git(1) suite Git 1.8.5.3 01/14/2014 GIT-SYMBOLIC-REF(1)
All times are GMT -4. The time now is 06:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy