Find the answer to your Linux question:
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 ...
  1. #1
    jls
    jls is offline
    Just 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:

    Code:
    cat /etc/squid/squid.conf | awk "/INTERNET/{ $0=$0 " $UNAME" } {print} " > /tmp/sq.conf
    mv /tmp/sq.conf /etc/squid/squid.conf
    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.
    At the moment it is adding the user to the ACL and again where it finds 'INTERNET' in the http_access directive.
    Thanks

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    just a concept, you can try add a flag
    Code:
    awk '/INTERNET/ && flag != 1{ "dosomething" ; flag=1 } {print }'
    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 print

  3. #3
    tpl
    tpl is offline
    Linux 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 more
    the sun is new every day (heraclitus)

  4. #4
    jls
    jls is offline
    Just Joined!
    Join Date
    Apr 2007
    Posts
    13
    Thanks guys.

    sed is now my freind

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by tpl View Post
    awk '/INTERNET/ {$0=$0 "$UNAME";print;exit}'

    the flag idea would be needed for 2 or more
    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

  6. #6
    jls
    jls is offline
    Just 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

  7. #7
    Linux 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..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...