awk - using variables in pattern which contain full pathname


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - using variables in pattern which contain full pathname
# 1  
Old 09-06-2019
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'
Code:
The command list all available repositories, 
search for the string 'zipper_local'
then on the same line search for the string 60
then on the same line search for the string which is contained in the variable 'ZYPPER_LOCAL_REP'

Code:
MY_RET_CODE=$(zypper lr -d | awk '/zypper_local/ && /60/ && /$ZYPPER_LOCAL_REP/')
RET_CODE="$?"
if [[ -z MY_RET_CODE || $RET_CODE -ne 0 ]] ; then
exit 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
# 2  
Old 09-06-2019
Give a try replacing your :



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

with :

Code:
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:


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


You may also want to give a try to :

Code:
zypper lr -d | sed -e '/zypper_local/!d' -e '/60/!d' -e '/'$ZYPPER_LOCAL_REP'/!d'


Last edited by ctsgnb; 09-06-2019 at 03:33 PM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 09-07-2019
Quote:
Originally Posted by ctsgnb
Give a try replacing your :
Code:
awk -vP="$ZYPPER_LOCAL_REP" '/zypper_local/ && /60/ &&($0~P)'

OK that works


Quote:
Originally Posted by ctsgnb
You can also try to get out the simple quote to make the $ZYPPER_LOCAL_REP interpreted by the shell:
Code:
awk '/zypper_local/ && /60/ && /'$ZYPPER_LOCAL_REP'/'

Does not works :
Code:
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]


Quote:
Originally Posted by ctsgnb
You may also want to give a try to :
Code:
zypper lr -d | sed -e '/zypper_local/!d' -e '/60/!d' -e '/'$ZYPPER_LOCAL_REP'/!d'

does not work :
Code:
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'

Code:
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?


Code:
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 :
Code:
awk: cmd. line:2: /zypper_local/ && /60/ && //some/path/to/somewhere/
awk: cmd. line:2:                                                    ^ unexpected newline or end of string

#
#
Code:
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 :
Quote:
sed: couldn't open file here/!d: No such file or directory
Any help is welcome.
# 4  
Old 09-07-2019
This might work:
Code:
awk "/zypper_local/ && /60/ && /${ZYPPER_LOCAL_REP//\//\\/}/" file

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

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


EDIT: adaption of sed proposal:

Code:
sed -e '/zypper_local/!d' -e '/60/!d' -e '\~'$ZYPPER_LOCAL_REP'~!d' file


Last edited by RudiC; 09-07-2019 at 09:06 AM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-11-2019
Thank you everybody fore helping.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comment all lines which are not already commented for each full path pattern matched

Hello. Question 1 : I want to comment out all lines of a cron file which are not already commented out for each full path pattern matched. Example 1 nothing to do because line is already commented out; pattern = '/usr/bin/munin-cron' # */5 * * * * munin test -x... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Find and replace the path value in files, pattern is not full known.

Hi, I need to do find and replace, but the pattern is not full known. for example, my file has /proj/app-d1/sun or /data/site-d1/conf here app-d1 and site-d1 is not constant. It may be different in different files. common part is /proj/xx/sun and /data/xxx/conf i want to find where ever... (6 Replies)
Discussion started by: rbalaj16
6 Replies

3. Shell Programming and Scripting

Include pathname in awk output?

I am running an awk to verify all the memory settings for tomcat, and need to include path or directory in output .... I am running: awk '{ print $3 }' /opt/dir1/dir2/*/tomcat/bin/setenv.sh Output results: -Xms1024m -Xmx1536m -Xmx1536m -Xmx1024m -Xms1024m -Xms1024m -Xms512m -Xms1024m... (3 Replies)
Discussion started by: kgolli
3 Replies

4. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

5. 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

6. Shell Programming and Scripting

print pattern between two variables awk sed

I am trying to print text between two variables in a file I have tried the following things but none seem to work: awk ' /'$a'/ {flag=1;next} /'$b'/{flag=0} flag { print }' file and also sed "/$a/,/$b/p" file But none seem to work Any Ideas? Thanks in Advance (5 Replies)
Discussion started by: forumbaba
5 Replies

7. Shell Programming and Scripting

Text pattern with AWK and variables

How to to pass variable to the below awk command I would like to pass variables to the awk command instead of the constants "a/cc" but I don't know exactly the correct syntax awk '/a/,/cc/' temp1.out file.txt a b s cc g d output a b s (8 Replies)
Discussion started by: ahmedamro
8 Replies

8. Shell Programming and Scripting

How to pass shell variables to awk's pattern?

How would I get folders owned by specific users.. I want to pass users as a shell variable to awk. drwxr-x--x 3 user1 allusers 512 Oct 14 2006 946157019/ drwxr-x--x 3 user2 allusers 512 Mar 9 2008 94825883/ drwxr-x--x 3 user3 allusers 512 Mar 9 2008 948390501/ ... (3 Replies)
Discussion started by: kchinnam
3 Replies

9. HP-UX

pgrep doesn't perform full command line pattern matching

Hi! I need to get PID of some particular process and I wonder if I can use pgrep tool for this purpose. The problem is that pgrep doesn't perform pattern matching on the whole command line, even if I use -f key. Parsing output of ps command is not quite convenient... Also deamon, which PID I need... (2 Replies)
Discussion started by: Sapfeer
2 Replies

10. Shell Programming and Scripting

Getting pathname variables with ksh

With C Shell you can get the root, head, tail and extension of a pathname by using pathname variable modifiers. Example Script: #! /bin/csh set pathvar=/home/WSJ091305.txt echo $pathvar:r echo $pathvar:h echo $pathvar:t echo $pathvar:e The result of executing this script is: ... (7 Replies)
Discussion started by: BCarlson
7 Replies
Login or Register to Ask a Question