awk - using variables in pattern which contain full pathname

Hello.

I would like to make this bash command working.
In the following code, the bash variable 'ZYPPER_LOCAL_REP' contain a full pathname like '/path/to/path/somewhere'

The command list all available repositories, 
search for the string 'zipper_local' 
then on the same line search for the string 60then on the same line search for the string which is contained in the variable 'ZYPPER_LOCAL_REP' 
MY_RET_CODE=$(zypper lr -d | awk '/zypper_local/ && /60/ && /$ZYPPER_LOCAL_REP/')
RET_CODE="$?"
if [[ -z MY_RET_CODE || $RET_CODE -ne 0 ]] ; thenexit 255
fi

I run into problems because of the use of a bash variables and because the variable contain a string with some '/'

Any help is welcome

Give a try replacing your :

awk '/zypper_local/ && /60/ && /$ZYPPER_LOCAL_REP/'

with :

awk -vP="$ZYPPER_LOCAL_REP" '/zypper_local/ && /60/ &&($0~P)'

You can also try to get out the simple quote to make the $ZYPPER_LOCAL_REP interpreted by the shell:

awk '/zypper_local/ && /60/ && /'$ZYPPER_LOCAL_REP'/'

You may also want to give a try to :

zypper lr -d | sed -e '/zypper_local/!d' -e '/60/!d' -e '/'$ZYPPER_LOCAL_REP'/!d'
1 Like

OK that works

awk '/zypper_local/ && /60/ && /'$ZYPPER_LOCAL_REP'/'

Does not works :

user_install@ASUS-G75VW-JC:~> zypper lr -d | awk '/zypper_local/ && /60/ && /'$ZYPPER_LOCAL_REP'/'
awk: cmd. line:2: /zypper_local/ && /60/ && //local/zypper_local/
awk: cmd. line:2:                                                ^ unexpected newline or end of string
 

[/quote]

does not work :

user_install@ASUS-G75VW-JC:~> zypper lr -d | sed -e '/zypper_local/!d' -e '/60/!d' -e '/'$ZYPPER_LOCAL_REP'/!d'
 sed: -e expression #3, char 4: extra characters after command

or depending of the output from "zypper lr -d'

sed: couldn't open file here/!d: No such file or directory

I think the problem come from the '/'

ZYPPER_LOCAL_REP="/some/path/to/somewhere"
here/!d come from the end of the content of variable ZYPPER_LOCAL_REP followed by "/!d"

Although the first solution works, I am interested in the other 2 proposals.
If you could solve the problem, i know very few about sed and awk?

ZYPPER_LOCAL_REP="/some/path/to/somewhere"
echo  "13 | zypper_local                             |  zypper_local                             | Yes     | ( p) Yes  | Yes      |   60     | plaindir | dir:/some/path/to/somewherel" | awk '/zypper_local/ && /60/ && /'$ZYPPER_LOCAL_REP'/'

which return :

awk: cmd. line:2: /zypper_local/ && /60/ && //some/path/to/somewhere/
awk: cmd. line:2:                                                    ^ unexpected newline or end of string

#
#

echo  "13 | zypper_local                             |  zypper_local                             | Yes     | ( p) Yes  | Yes      |   60     | plaindir | dir:/some/path/to/somewherel" |  sed -e '/zypper_local/!d' -e '/60/!d' -e '/'$ZYPPER_LOCAL_REP'/!d'

which return :

Any help is welcome.

This might work:

awk "/zypper_local/ && /60/ && /${ZYPPER_LOCAL_REP//\//\\/}/" file

; or try (given the order of the substring is as given):

grep "zypper_local.*60.*$ZYPPER_LOCAL_REP" file

EDIT: adaption of sed proposal:

sed -e '/zypper_local/!d' -e '/60/!d' -e '\~'$ZYPPER_LOCAL_REP'~!d' file
1 Like

Thank you everybody fore helping.