LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   replace matched pattern from 1st file into 2nd file using awk (http://www.linuxquestions.org/questions/programming-9/replace-matched-pattern-from-1st-file-into-2nd-file-using-awk-918428/)

sopier 12-12-2011 09:58 PM

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


Telengard 12-12-2011 10:23 PM

What have you got written so far? Where are you stuck?

sopier 12-12-2011 11:44 PM

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


colucix 12-13-2011 02:00 AM

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.

sopier 12-13-2011 04:34 AM

Quote:

Originally Posted by colucix (Post 4548536)
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.

colucix 12-13-2011 07:13 AM

Quote:

Originally Posted by sopier (Post 4548610)
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.

sopier 12-13-2011 09:58 AM

Thank you...


All times are GMT -5. The time now is 07:38 AM.