LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-12-2011, 10:58 PM   #1
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Rep: Reputation: Disabled
replace matched pattern from 1st file into 2nd file using awk


Hi,

I have these two files:

albums_data.csv
Code:
Monkey Business;album_cover_B000WLRXPW.jpg;June 7, 2005;1:06:09;B000WLRXPW;A&M;(C) 2005 Interscope Records;The Black Eyed Peas
Michael Buble;album_cover_B00122HPDU.jpg;February 11, 2003;48:45;B00122HPDU;143/Reprise;2002 Reprise Records for the U.S. and WEA International Inc. for the world outside of the U.S.;Michael Buble
Crazy Love (Amazon MP3 Exclusive Version);album_cover_B002RB7VMA.jpg;October 6, 2009;50:52;B002RB7VMA;143/Reprise;2009 Reprise Records for the U.S. and WEA International Inc. for the world outside the U.S.;
artists_data.csv
Code:
1;The Black Eyed Peas
2;Michael Buble
If the pattern match, I want to replace $8 in albums_data.csv with $1 in artists_data.csv

So the data will looks like this:
Code:
Monkey Business;album_cover_B000WLRXPW.jpg;June 7, 2005;1:06:09;B000WLRXPW;A&M;(C) 2005 Interscope Records;1
Michael Buble;album_cover_B00122HPDU.jpg;February 11, 2003;48:45;B00122HPDU;143/Reprise;2002 Reprise Records for the U.S. and WEA International Inc. for the world outside of the U.S.;2
Crazy Love (Amazon MP3 Exclusive Version);album_cover_B002RB7VMA.jpg;October 6, 2009;50:52;B002RB7VMA;143/Reprise;2009 Reprise Records for the U.S. and WEA International Inc. for the world outside the U.S.;
 
Old 12-12-2011, 11:23 PM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
What have you got written so far? Where are you stuck?
 
Old 12-13-2011, 12:44 AM   #3
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: Disabled
Here is my command
Code:
awk 'NR==FNR{a[$1]=$2;next} {$8=a[$8]}1' artists_data.csv albums_data.csv

and here is the result
Code:
Monkey Business;album_cover_B000WLRXPW.jpg;June 7, 2005;1:06:09;B000WLRXPW;A&M;(C) 2005 Interscope Records;The  Eyed Peas
Michael Buble;album_cover_B00122HPDU.jpg;February 11, 2003;48:45;B00122HPDU;143/Reprise;2002 Reprise Records for  U.S. and WEA International Inc. for the world outside of the U.S.;Michael Buble
Crazy Love (Amazon MP3 Exclusive Version);album_cover_B002RB7VMA.jpg;October 6,  Reprise Records for the U.S. and WEA International Inc. for the world outside the U.S.;
 
Old 12-13-2011, 03:00 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
In your code you missed the definition of FS and OFS accordingly. Then you exchanged $1 with $2 in the first rule. Here is a corrected (and expanded) version of your code:
Code:
BEGIN {
  FS = OFS = ";"
}

NR==FNR {
  a[$2] = $1
  next
}

{
  $8 = a[$8]
}

1
Hope this helps.
 
1 members found this post helpful.
Old 12-13-2011, 05:34 AM   #5
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by colucix View Post
In your code you missed the definition of FS and OFS accordingly. Then you exchanged $1 with $2 in the first rule. Here is a corrected (and expanded) version of your code:
Code:
BEGIN {
  FS = OFS = ";"
}

NR==FNR {
  a[$2] = $1
  next
}

{
  $8 = a[$8]
}

1
Hope this helps.
This works! Just curious with the meaning of 1 in the end of the code? If I remove it, the code doesn't print anything.
 
Old 12-13-2011, 08:13 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by sopier View Post
Just curious with the meaning of 1 in the end of the code?
Think about the general structure of an awk program: you have different rules whose syntax is
Code:
pattern { action }
pattern { action }
...
If you omit the action, the default is applied, that is print $0 or print alone, which simply prints the entire record (line). Now the pattern: it can be a logical expression, a regular expression, a range and so on. If it is a logical expression it is evaluated as TRUE or FALSE. Since in awk true and false are a non-zero (or a non-empty string) and 0 (or an empty string) respectively, you can use 1 or any other number different than 0 to state TRUE.

In brief, your rule is made of an expression without action, the expression is TRUE and the default action is applied, that is the whole line is printed out.
 
1 members found this post helpful.
Old 12-13-2011, 10:58 AM   #7
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thank you...
 
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replace 2nd occurrence of a string in a file - sed or awk? kushalkoolwal Programming 10 05-02-2011 03:30 PM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 08:57 AM
Sed to read the file and replace and insert the pattern saurabhchokshi Programming 2 06-12-2009 02:15 PM
Pattern matching in a text file - use of AWK?? wtaicken Programming 19 02-06-2009 06:54 PM
vim: search pattern / replace with file naflan Programming 5 01-07-2006 04:16 PM


All times are GMT -5. The time now is 10:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration