LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 12-07-2012, 05:07 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Awk: Search for string containing "/"


Hello!
I want to search 2 strings (i.e. 6/Dec/2012 and string2) in a file file1 using awk. But problem is, how to avoid "/" in search string.
Code is somethnig like:
Code:
day=6   # Input taken from user
month=Dec # Input taken from user
year=2012 # Input taken from user
nawk '/$day\/$smonth\/$year/ && /string2/ {gsub(/string2=/,"",$11); print $11}' file1 > output.txt
And even if I do:
Code:
day=6   # Input taken from user
month=Dec # Input taken from user
year=2012 # Input taken from user
format=$day\/$month\/$year
nawk '/$format/ && /string2/ {gsub(/string2=/,"",$11); print $11}' file1 > output.txt
But nothing is working and everytime I can see an empty output.txt file. So any suggestion on how to rectify this escape sequence problem?

--------- ADDING INFO ---------
I have tried with following also, but...
Code:
format=$day\\/$month\\/$year
nawk '/$format/ && /string2/ {gsub(/string2=/,"",$11); print $11}' file1 > output.txt

Last edited by shivaa; 12-07-2012 at 05:19 AM.
 
Old 12-07-2012, 05:40 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
I can't help with awk, but for sed the following works
Code:
#!/usr/bin/bash

DAY="6"
MONTH="Dec"
YEAR="2012"

echo $DAY
echo $MONTH
echo $YEAR
cat datefile | sed -n "/$DAY\/$MONTH\/$YEAR/p"
where datefile is a file with the string you're searching for.

This means: When Bash should expand the variables, they have to be between doublequotes " and not singlequotes '

Markus
 
Old 12-07-2012, 05:49 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
The problem arises from the use of single quotes (the shell does not expand variable).

You can still use single quotes, but you need to do it this way:
Code:
awk '/'$DAY'\/'$MONTH'\/'$YEAR'/ { print }' infile
Or (not sure nawk supports this) use double quotes:
Code:
awk "/$DAY\/$MONTH\/$YEAR/ { print }" infile
@markush: Useless use of a pipe and cat
Code:
sed -n "/$DAY\/$MONTH\/$YEAR/p" datefile
 
1 members found this post helpful.
Old 12-07-2012, 06:00 AM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Quote:
Originally Posted by druuna View Post
...
@markush: Useless use of a pipe and cat
...
Oh, too much code, thanks for the hint

Markus

Last edited by markush; 12-07-2012 at 06:01 AM.
 
Old 12-07-2012, 06:43 AM   #5
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 druuna View Post
Or (not sure nawk supports this) use double quotes
Just an aside note: it's not a matter of nawk supporting double quotes. They change only the shell behaviour: nawk receives the first argument without quotes. The problem is that the original nawk program contains double quotes and $ signs, so that they are substituted by the shell and not passed literally. To avoid this problem I'd suggest the following:
Code:
nawk -v date="$day/$smonth/$year" '$0 ~ date && $11 ~ "string2" {gsub(/string2=/,"",$11); print $11}' file1
Note that it doesn't need to escape slashes in the date variable assignment and it uses string constants instead of regexp (since you cannot put a variable name inside a regexp, otherwise it will be interpreted literally).
 
1 members found this post helpful.
Old 12-08-2012, 11:45 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Thanks everyone. It's resolved using both solutions, either by using single quotes inside single quotes or declaring the varialbe outside of search pattern.
 
  


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
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 12:26 PM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 11:01 PM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 07:14 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 08:38 AM
vim search string question with /" and \" m_lp_ql_m Programming 2 08-03-2006 01:48 AM


All times are GMT -5. The time now is 04:33 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