Results 1 to 7 of 7
Hi Guys,
I am using a BASH script to add users to an ACL in the squid.conf file. Basically the script searches for a string and adds a variable to ...
- 06-04-2007 #1Just Joined!
- Join Date
- Apr 2007
- Posts
- 13
awk question
Hi Guys,
I am using a BASH script to add users to an ACL in the squid.conf file. Basically the script searches for a string and adds a variable to the end of the string like this:
Now the problem is that there is more than one occurrence of the search string (INTERNET in this case). I want awk to stop after the first successful operation.Code:cat /etc/squid/squid.conf | awk "/INTERNET/{ $0=$0 " $UNAME" } {print} " > /tmp/sq.conf mv /tmp/sq.conf /etc/squid/squid.conf
At the moment it is adding the user to the ACL and again where it finds 'INTERNET' in the http_access directive.
Thanks
- 06-04-2007 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
just a concept, you can try add a flag
initially, flag is not equal 1, so it process the line and then sets the flag. For subsequent INTERNET patterns, because the flag is already 1, it will just printCode:awk '/INTERNET/ && flag != 1{ "dosomething" ; flag=1 } {print }'
- 06-05-2007 #3Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
hi jls,
thanks for an interesting question:
if the phrase is fixed, and short, try this:
sed 's/INTERNET/INTERNET '$UNAME'/;1q'
otherwise like this:
awk '/INTERNET/ {$0=$0 "$UNAME";print;exit}'
the flag idea would be needed for 2 or morethe sun is new every day (heraclitus)
- 06-05-2007 #4Just Joined!
- Join Date
- Apr 2007
- Posts
- 13
Thanks guys.
sed is now my freind
- 06-05-2007 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
i had interpreted OP wants to change only the first line with INTERNET , then carry on with the rest of the input file, where other INTERNET lines are not changed. He is changing som e config file called "http_access" and if he only exits after the first operation, won't his config file contain only that line? anyway, since OP has solved the issue, i guess its no problem...cheers
- 06-05-2007 #6Just Joined!
- Join Date
- Apr 2007
- Posts
- 13
Or maybe not........Using the sed string deletes everything except the first line of the squid.conf file
Am now trying the awk with flag options but the problem is that the ' ' around awk masks the $.
So the string $UNAME is added instead of the variable. I tried substituting the ' 'with " " but that gives me this error:Code:wk: /INTERNET/ && flag != 1{ -bash=-bash awk: ^ syntax error awk: cmd. line:1: /INTERNET/ && flag != 1{ -bash=-bash awk: cmd. line:1: ^ unexpected newline or end of string
- 06-05-2007 #7Linux User
- Join Date
- Aug 2006
- Posts
- 458
why don't you post a sample of your squid file, then what you want your output to be. its better this way..


Reply With Quote
