LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Awk: Search for string containing "/" (https://www.linuxquestions.org/questions/linux-newbie-8/awk-search-for-string-containing-4175440406/)

shivaa 12-07-2012 04:07 AM

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


markush 12-07-2012 04:40 AM

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

druuna 12-07-2012 04:49 AM

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

markush 12-07-2012 05:00 AM

Quote:

Originally Posted by druuna (Post 4844412)
...
@markush: Useless use of a pipe and cat ;)
...

Oh, too much code, thanks for the hint ;)

Markus

colucix 12-07-2012 05:43 AM

Quote:

Originally Posted by druuna (Post 4844412)
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).

shivaa 12-08-2012 10:45 PM

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


All times are GMT -5. The time now is 03:39 PM.