awk - ignore metacharacters, search shell variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - ignore metacharacters, search shell variables
# 8  
Old 06-18-2012
Please bear with me while I illustrate my objective again,
I am not sucessfully applying your suggestions.

File1: named "Playlist", delimiter is: " - "
  • Bob Marley - Jammin (Bonus Track).mp3
File1 fields are: "Artist" "Title"

File2: named "Master-List", delimiter is: tab
  • Bob Marley Jammin (Bonus Track).mp3 32000 /Music/Bob Marley/ Jammin (Bonus Track).mp3
File2 fields are: "Artist" "Title" "Bitrate" "Path"

Using the above examples... for each entry in "Playlist"
1. Use "Playlist" to store 2 variables for Field1 and Field2 (artist title)
2. Search for $title "Jammin (Bonus Track).mp3" inside Master-List
3. If matched, check that the $artist also is matched
4. if the $title and $artist match, print the 4th colunm in Master-List, else print "No Match".

Thanks for your interest in helping out.
# 9  
Old 06-18-2012
Code:
# awk -F'-' 'NR==FNR{art=$1;gsub(/[()-%$@]|^[ ]*/,"",$2);tit=$2;next}{l=split($0,a,"\t");gsub(/[()-%$@]/,"",a[2]);mas=a[1];
for(i=2;i<=l;i++)mas=mas" "a[i];if(mas~tit)if(mas~art)print a[l];else print "No Match"}' Playlist Master-List
/Music/Bob Marley/ Jammin (Bonus Track).mp3


Last edited by ygemici; 06-18-2012 at 04:25 PM..
# 10  
Old 06-20-2012
Thanks Chubler XL, ygemici.

I've carefully copied and pasted your codes, they do not return any result.
I'm using the debian version of awk (mawk), does that affect your suggestions?

Thank you for trying. I'm going to give it one last attempt to try and combine your suggestions into one.

1. The best way to avoid metacharacters is to use the 'index' function
2. The best way to approach comparing 2 files is to use an 'array'

With that in mind, to simplify, i've made all files tab seperated and removed the .mp3 .

Use 2 arrays from file1 "Playlist"...
  • a for <field1> "Artist"
  • t for <field2> "Title"
Example Line from Playlist;
  • Shaggy Angel (Bonus Track)

Then;
1. compare the indexed Title "Angel (Bonus Track)" to file2 "Master-Music-List" <field2> "Title"
2. check that the indexed Artist "Shaggy" matches "Master-Music-List" <field1> "Artist"

If 'both' array1 (title) , and array2 (artist) match, then print the result.

Example;

Match this from "Playlist" to "Master-Music-List";
  • Shaggy Angel (Bonus Track)
Master-Music-List Lines:
  • Shaggy Angel (Bonus Track) 32000 /Music/S/Shaggy/Angel (Bonus Track.mp3
  • The Rolling Stones Angel 128000 /Music/R/Rolling Stones/Angel.mp3

This way, the song 'Angel (Bonus Track)' will match for Shaggy and not for Rolling Stones.

Could someone please help to include the arrays and index functions ?

Thank you.

Last edited by DSommers; 06-21-2012 at 10:37 AM.. Reason: refined formatting
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using Shell variables in awk

Hi All, I have a file which I am reading and looking for "EXIT" statement. I want to insert ":JCWPROD" after each EXIT statement only if ":JCWPROD" doesn't exist. Here is the sample file : EXIT Testing EXIT tesing123 EXIT Desired file : EXIT :JCWPROD Testing EXIT :JCWPROD... (7 Replies)
Discussion started by: nua7
7 Replies

2. UNIX for Dummies Questions & Answers

Command line not recognizing metacharacters in awk

Hello, I'm new to command line coding (and coding in general) and have run into a problem. I'm using awk to perform a global find and replace in a text file in the Terminal provided by Mac. Here is a sample of my textfile where the fields are separated by tabs. 1Ps 1,1 VWB/(J VWB VWB... (7 Replies)
Discussion started by: jvoot
7 Replies

3. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

4. Shell Programming and Scripting

escaping metacharacters in paths for a shell command

I have a file which contains a list of paths separated by a new line character. e.g /some/path/to/a/file.png /some/path to/another/file.jpeg /some path/to yet/another/file Notice that these paths may contain metacharacters, the spaces for example are also not escaped. If I wanted... (5 Replies)
Discussion started by: cue
5 Replies

5. Shell Programming and Scripting

awk - take variables out to shell

Hi, How could we take the value of awk variables out to shell? I know the following methods 1. awk '{print $1}' < file | read a echo $a 2. a=`awk '{print $1}' < file` echo $a Please let me know if there are any other methods. Also, how do we take more than 1 variable value... (4 Replies)
Discussion started by: Thumban
4 Replies

6. UNIX for Advanced & Expert Users

use of variables in awk to search for pattern from a file

Hi, I need to extract all the content between two strings stored in two variables "startstring" and "endstring" startstring=hello enstring=world #notworking awk '/$startstring/, $NF ~ /$endstring/ ' file > file2 The above code is not working with variables. It works when actual string... (2 Replies)
Discussion started by: jeanjkj
2 Replies

7. Shell Programming and Scripting

search and replace using awk with variables

Hi, I have been trying to use awk with variables that needs to search for a pattern and replace it with another pattern, the patterns are supplied in a variable. I have tried several different ways without success and hope that someone can help me. Here are the details echo $UPC 07007457809... (2 Replies)
Discussion started by: jerardfjay
2 Replies

8. Shell Programming and Scripting

Awk, shell variables

Hello, I've been trying to figure out how to use variables inside the AWK command and use it back in the korn shell sript. in my script I have lots of awk commands like this grep Listen /etc/ssh/sshd_config | \ awk '{ if ($2 == "22" ) print "OK"; else print "not OK" }' ... (3 Replies)
Discussion started by: mirusko
3 Replies

9. Shell Programming and Scripting

Modify shell variables with AWK

Dear Folks, I have a command output something like: And I want to store PIN0 and SIG0 in two shell variables, now I do a double awk: PIN=`gsmctl -d /dev/ttyS0 pin sig | awk '/PIN0/ { print $2}'` SIG=`gsmctl -d /dev/ttyS0 pin sig | awk '/SIG0/ { print $2}'` It's possible to... (4 Replies)
Discussion started by: Santi
4 Replies

10. Shell Programming and Scripting

Using shell variables In awk

Oh its not my day for syntax... cat gzipsize.txt | awk '{print "echo",$1,$2} > master.txt I have read a lot about the awk -v but haven't been able to get it to work. I have a variable in my script and I'm looking just to push it into the awk after the $2 (or anywhere would do)!!! Every... (11 Replies)
Discussion started by: nortypig
11 Replies
Login or Register to Ask a Question