awk compare column n replace with in one file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk compare column n replace with in one file
# 1  
Old 11-10-2012
Linux awk compare column n replace with in one file

hi Friends
need to compare columns in one file where the data looks like below
Code:
laptop,IBM
phone,samsung
car,rental
user1,laptop
user2,laptop
user3,phone

want to get output as
Code:
laptop,IBM
phone,samsung
car,rental
user1,IBM
user2,IBM
user3,samsung

need to seach $2 in array of $1 and if found print the $2 of the found...
if not print as it is

Last edited by Scott; 11-10-2012 at 09:20 AM.. Reason: Code tags
# 2  
Old 11-10-2012
With assumptions:
Code:
awk -F, '$2 in a{$2=a[$2]}{a[$1]=$2}1' OFS=, file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 11-10-2012
Hi

Code:
awk -F, 'NR==FNR{a[$1]=$2;next}{$2=a[$2]?a[$2]:$2}1' OFS=,  file file

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 11-10-2012
Quote:
Originally Posted by elixir_sinari
With assumptions:
Code:
awk -F, '$2 in a{$2=a[$2]}{a[$1]=$2}1' OFS=, file

thanks for that,i tried all this while to understand the idea. still coudnt
# 5  
Old 11-10-2012
awkishly speaking:
Code:
awk -F, '                # set "," as field separator
$2 in a{$2=a[$2]}        # if field 2 is an array a key set field 2 to array a value for field 2 key
{a[$1]=$2}1              # store field 2 in array a value for field 1 key and print line
' OFS=, file             # use "," as output field separator, use file as input

This User Gave Thanks to rdrtx1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Shell Programming and Scripting

Use awk to replace numbers in a file with a column from another file

Hello, I am trying to make a awk code that will take 2 files, a txt file like this : 1 1 88 c(1:38, 42, 102) 2 2 128 c(39:41, 43:101, 103:105, 153, 155:189, 292, 344:369) 3 3 84 c(190:249, 603, 606:607, 609:629) 4 4 12 ... (8 Replies)
Discussion started by: nastaziales
8 Replies

4. Shell Programming and Scripting

How to compare the values of a column in a same file using awk?

Dear Unix experts, I have got a file where I would like to compare the values of second column if first column is same in such a way that the difference between the values is >50. If not, I would like to discard both values. For example, my input file looks like - comp275_c0_seq2 73... (7 Replies)
Discussion started by: utritala
7 Replies

5. Shell Programming and Scripting

Compare first column of 2 files and replace

Hi All, I have 2 files in the following format : File 1 S00999999|BHANU|TEST|007 JOHN DOE APT 999||VENGA HIGHWAY|MA|09566|SCO DUAL|20140201|20140331|20140401|20140630|20140327| S00888888|BU|TES|009 JOHN DOE APT 909||SENGA HIGHWAY|MA|08566|SCO... (1 Reply)
Discussion started by: nua7
1 Replies

6. Shell Programming and Scripting

Compare the second column of a file with the second column of another in awk

Hi, I know that this topic has been discussed in the past and I've tried to follow all the guidelines. Anyhow, I following describe my problem. I have a file (file1 , no. records = 67) containing pairs of IP addresses as follows (with single space as delimiter between the fields): example... (5 Replies)
Discussion started by: amarn
5 Replies

7. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies

8. Shell Programming and Scripting

Find in first column and replace the line with Awk, and output new file

Find in first column and replace the line with Awk, and output new file File1.txt"2011-11-02","Georgia","Atlanta","x","","" "2011-11-03","California","Los Angeles","x","","" "2011-11-04","Georgia","Atlanta","x","x","x" "2011-11-05","Georgia","Atlanta","x","x","" ... (4 Replies)
Discussion started by: charles33
4 Replies

9. Shell Programming and Scripting

Need an awk for a global find/replace in a file, specific column

I am new to unix and awk/sed etc... using C-Shell. Basically, I have a fixed length file that has 4 different record types on it, H, D, V, W all in column 1. I need to change all the W's in column 1 to D's. in the entire file. The W's can be anywhere in the file and must remain in the same... (3 Replies)
Discussion started by: jclanc8
3 Replies
Login or Register to Ask a Question