awk or sed? change field conditional on key match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or sed? change field conditional on key match
# 1  
Old 03-02-2012
awk or sed? change field conditional on key match

Hi. I'd appreciate if I can get some direction in this issue to get me going.

Datafile1:
-About 4000 records, I have to update field#4 in selected records based on a match in the key field (Field#1).
-Field #1 is the key field (servername) . # of Fields may vary
Code:
# comment
server1 bbb ccc ddd eee fff
server2 bbb ccc ddd eee fff # comment 3 words
server3 bbb ccc ddd eee fff
server4 bbb ccc ddd eee fff # extra comments here too
...
server499 bbb ccc ddd eee fff
server500 bbb ccc ddd eee fff # comment that drags on and on and on

Datafile2:
I have a second datafile listing identifying which servenames need an update on field#4.
Code:
server2
server4
server12
server15
....
server499

When I match this name to datafile1:field#1 to a name in datafile2, I want to change field#4 to static value e.g."444" and update datafile1.
Desired output:
Code:
server1 bbb ccc ddd eee fff
server2 bbb ccc 444 eee fff # comment 3 words
server3 bbb ccc ddd eee fff
server4 bbb ccc 444 eee fff # extra comments here too
...
server499 bbb ccc 444 eee fff
server500 bbb ccc ddd eee fff # comment that drags on and on and on and on

I'm thinking along these lines
-while Read datafile1
-move record to storage
-grep datafile2 for a match to datafile1:field1
-no match: write record unchanged
-match: alter field4 to '444' and write record

Would awk or sed suit this best?
I'm thinking awk, but can I easily contain the necessary logic to grep an external file looking for a match?
# 2  
Old 03-02-2012
Code:
awk 'NR==FNR{a[$1];next}$1 in a{$4="444"}1' Datafile2 Datafile1

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-02-2012
Thanks bartus11, that's exactly what the Dr. ordered.

Now I've got to go read up on FNR and get a good handle on how it works and where else I can use it.
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 match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to match value to a field within +/- value

In the awk below I use $2 of filet to search filea for a match. If the values in $2 are exact match this works great. However, that is not always the case, so I need to perform the search using a range of + or - 2. That is if the value in filea $2 is within + or - 2 of filet $2 then it is matched.... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

match two key columns in two files and print output (awk)

I have two files... file1 and file2. Where columns 1 and 2 of file1 match columns 1 and 2 of file2 I want to create a new file that is all file1 + columns 3 and 4 of file2 :b: Many thanks if you know how to do this.... :b: file1 31-101 106 0 92 31-101 106 29 ... (2 Replies)
Discussion started by: pelhabuan
2 Replies

4. Shell Programming and Scripting

Split file when the key field change !

Hello, I have the following example data file: Rv.Global_Sk,1077.160523,D,16/09/2011 Rv.Global_Sk,1077.08098,D,17/09/2011 Rv.Global_Sk,1077.001445,D,18/09/2011 Rv.Global_Sk,1072.660733,D,19/09/2011 Rv.Global_Sk,1070.381557,D,20/09/2011 Rv.Global_Sk,1071.971747,D,21/09/2011... (4 Replies)
Discussion started by: csierra
4 Replies

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

6. Shell Programming and Scripting

Using AWK to format output based on key field

I have file which contains gene lines something like this Transcript Name GO POPTR_0016s06290.1 98654 POPTR_2158s00200.1 11324 POPTR_0004s22390.1 12897 POPTR_0001s11490.1 POPTR_0016s13950.1 14532 POPTR_0015s05840.1 13455 POPTR_0013s06470.1 12344... (6 Replies)
Discussion started by: shen
6 Replies

7. Shell Programming and Scripting

awk conditional expression to compare field number and variable value

Hi, I'm trying to compare the value in a field to the value in a variable using awk. This works: awk '$7 == "101"'but this is what I want (and it doesn't work): value=101 awk '$7 == "$value"' Any help or insight on this would be great. Thanks in advance. (1 Reply)
Discussion started by: goodbenito
1 Replies

8. Shell Programming and Scripting

Conditional tab replacement sed/awk

Hi I am struggling to find a solutions to this problem: I have a directory full of files and I wish to: read each line of each file and if any one line in those files is longer than 72 characters I want to replace any tab characters with a space character. Ive been... (3 Replies)
Discussion started by: benackland
3 Replies

9. Shell Programming and Scripting

Conditional edit for a field using sed

Hi I want to repalce a field in a txt file on solaris with say 100 records and each record having a total of 10 fields separated by a ~ . based on the following condition the record should be edited or else the record should be written as it is to a if the seventh field is 'XX' and if... (2 Replies)
Discussion started by: acharania2011
2 Replies

10. Shell Programming and Scripting

how do i pattern match a field with awk?

hi, let's say $numbers = "324 350 587" an so on... what i'm trying to do is this: awk -v numbers="$numbers" '{if (numbers ~ /$2/) print $0, "bla bla"}' file # file looks like this: 214 ..... 215 ... 216 .... 250 ... 324 325 ... 350 something ... ... 587 ... (4 Replies)
Discussion started by: someone123
4 Replies
Login or Register to Ask a Question