awk - ignore metacharacters, search shell variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - ignore metacharacters, search shell variables
# 1  
Old 06-17-2012
awk - ignore metacharacters, search shell variables

Can I use awk to search for a string, passed from the shell, that might include metacharacters?

  • File1 entries: Bob Marley Jammin (Bonus Track).mp3
  • File2 entries: Bob Marley Jammin (Bonus Track).mp3 32000 /Music/Bob Marley/ Jammin (Bonus Track).mp3

So far, I have this;
Code:
$ sed -e 's/.*\ -\ //' -e 's/.mp3$//' File1.txt | while read z; do tit="$z" ; awk -v title="$tit" '{FS = "\t"} $2 ~ title {print $0}' File2.txt ; done

It's working, but I'm not figuring the best way to treat the variable passed from shell as pure text, to disregard any interpretations.

When it sees the ( , it chokes.

I'm trying to search a music playlist (file1) for the song title (Jammin (Bonus Track.mp3) in another file2, field 2. If found, print the line.

Can someone please help correct the above code?

Thank you.
# 2  
Old 06-17-2012
Try this:

Code:
awk -F'\t' 'NR==FNR{gsub(/.* - /,"",$0);need[$0]++;next}
{for(N in need){if(index($2,N)){print; next}}}' File1.txt File2.txt

---------- Post updated at 10:43 AM ---------- Previous update was at 10:37 AM ----------

The issue wasn't anything to do with variable passing that was working OK it was just that "(" means something to ~.

Using index() avoids this issue as index dosn't support any regex searches.
# 3  
Old 06-18-2012
Thank you.

If I change the ~ to == does that also acomplish the objective ?

I copy and pasted your code, it does not result in any activity...


Code:
awk -F'\t' 'NR==FNR{gsub(/.* - /,"",$0);need[$0]++;next}{for(N in need){if(index($2,N)){print; next}}}' File1.txt File2.txt


Last edited by Scrutinizer; 06-18-2012 at 04:24 AM.. Reason: State failed result; mod: code tags
# 4  
Old 06-18-2012
like this?
Code:
# awk 'NR==FNR{gsub("[()]","");x=$0}NR!=FNR{xx=$0;gsub("[()]","");if($0~x)print xx}' File1.txt File2.txt
Bob Marley Jammin (Bonus Track).mp3 32000 /Music/Bob Marley/ Jammin (Bonus Track).mp3

# 5  
Old 06-18-2012
OK.

But how does one protect against "All" characters that could be in the filename?

The filename may contain "(-.%$@", etc. Nice line ygemici,

I believe it will only work hardcoded with the "("... if I understand it correctly.

Thank you.

Last edited by DSommers; 06-18-2012 at 05:42 AM.. Reason: Improved formatting
# 6  
Old 06-18-2012
Will

Code:
fgrep -f file1 file2

serve your purpose?
# 7  
Old 06-18-2012
Quote:
Originally Posted by DSommers
OK.

But how does one protect against "All" characters that could be in the filename?

The filename may contain "(-.%$@", etc. Nice line ygemici,

I believe it will only work hardcoded with the "("... if I understand it correctly.

Thank you.
try this
Code:
# awk 'NR==FNR{gsub(/[[:punct:]]/," ");$NF="";x=$0;}NR!=FNR{xx=$0;gsub(/[[:punct:]]|mp3/,"");if($0~x)print xx}' file1 file2

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