Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using variable in bash for awk 1

Status
Not open for further replies.

thedaver

IS-IT--Management
Jul 12, 2001
2,741
US
I'm having a heck of time getting a silly little awk script to run correctly. I know I'm not escaping/quoting the variable correctly, but I cannot figure out what's wrong.

Objective, match a leading (time) string in a logfile and print the rest of the file from that matching poing to the end of the file:

This works on the command line
Code:
> date --date="1 hour ago" +'%b %d %H:%M'
May 29 09:05

This works on the command line
Code:
export FOO=`date --date="1 hour ago" +'%b %d %H:%M'` | tail -5000 auth.log | awk '/^May 29 06/,EOF'
(it outputs the expected output

This works in a bash shell script
Code:
#!/bin/bash

FOOFOO=`date --date="1 hour ago" +'%b %d %H:%M'`
SOURCELOG="/var/log/auth.log"
tail -5000 $SOURCELOG | awk '/^May 29 06/,EOF'

This works in a bourne shell script
Code:
#!/bin/sh

FOOFOO=`date --date="1 hour ago" +'%b %d %H:%M'`
SOURCELOG="/var/log/auth.log"
tail -5000 $SOURCELOG | awk '/^May 29 06/,EOF'

However, when I change the awk to use a variable $FOOFOO, the script generates no output... examples of brokenness

Code:
awk '/^$FOOFOO/,EOF'
awk '/^${FOOFOO}/,EOF'
awk '/^{${FOOFOO}}/,EOF'
awk '/^{$${FOOFOO}}/,EOF'

This generates an error while parsing the variable
Code:
awk '/^'$FOOFOO'/,EOF'

awk: /^May
awk:  ^ unterminated regexp

Can anyone shortcut my fast trip to shell variable quoting madness? THANKS!



D.E.R. Management - IT Project Management Consulting
 
Code:
awk [red]"[/red]'/^$FOOFOO/,EOF'[red]"[/red]
The shell won't interpolate stuff inside single quotes. Use double quotes instead, even if they contain the single quotes you want to pass to awk...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
...also, if your server goes through a quiet patch exactly an hour ago, and nothing gets written to the /var/auth/log for a minute, it is possible that you won't trigger the start condition in the awk, so you won't get any output.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I loved that idea, but now it's a new error...

Code:
#!/bin/bash

FOOFOO=`date --date="1 hour ago" +'%b %d %H:%M'`
SOURCELOG="/var/log/auth.log"
tail -5000 $SOURCELOG | awk "'/^$FOOFOO/,EOF'"



awk: '/^May 29 11:21/,EOF'
awk: ^ invalid char ''' in expression

Ideas?




D.E.R. Management - IT Project Management Consulting
 
OK, well I never actually tested it, you see...[blush]

Try it without the single quotes maybe?
Bash:
tail -5000 $SOURCELOG | awk "/^$FOOFOO/,EOF"

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top