Using awk -v with a search pattern with a "*"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk -v with a search pattern with a "*"
# 1  
Old 08-03-2010
Using awk -v with a search pattern with a "*"

Hello

I'm trying to write a script that will grab a chunk of a log file. The 'timestamp' of the log has a "*" in it. The code I'm using is:

Code:
#/bin/bash
#
TIMESRT=2010JUL21*00:00:00
TIMEEND=2010JUL21*01:00:00
RLOG=log.log
#
awk -v START1=$TIMESRT -v END1=$TIMEEND '{
        if (match($0,START1))
        printflag=1
        if (printflag==1)
        print
        if (match($0,END1))
        printflag=0
}' $RLOG > output.log

When the above is executed via a script.. 'set-x' reveals this:

Code:
+ awk -v 'START1=2010JUL21*00:00:00' -v 'END1=2010JUL21*01:00:00' '{

Whereas, the "START1" & "END1" variables are being wrapped in " ' ' "? And the search and grab works just fine.

If I'd simplify the search to just the "time" and not the "date*time", the variables aren't wrapped in " ' ' "?

Example:
Code:
+ awk -v START1=00:00:00 -v END1=01:00:00 '{

(of course, a timestamp in this manner, though it works, I can grab 'days' worth of information... can't have that)

I don't understand this? Smilie If I do a search manually using awk, like:

Code:
awk '/'"2010JUL21\*00:00:00"'/' file.log

... it works fine.

To sum up... if I edit the "TIMESRT" or "TIMEEND" variables, w/ the added '"timestamp"', that too fails.

Can someone explain why this is happening, and more important, what I am doing wrong (so I can learn).

Smilie

Thanks for any assitance
# 2  
Old 08-03-2010
Please post a relevant part of your log file and an example of the desired output.
# 3  
Old 08-04-2010
I certainly will tomorrow when I have access to the system again. Thanks!

---------- Post updated 08-04-10 at 10:23 AM ---------- Previous update was 08-03-10 at 08:52 PM ----------

Excerpt from the log(s):

Code:
****0000004461*soapcommon.cpp*00384*07000*2010JUL21*21:41:55
 Client has closed the connection
****0000004461*soapcommon.cpp*00384*07000*2010JUL21*21:41:55
 Client has closed the connection
****0000004461*soapcommon.cpp*00384*07000*2010JUL21*21:41:55
 Client has closed the connection
****0000004461*soapcommon.cpp*00384*07000*2010JUL21*21:41:55
 Client has closed the connection
****0000004461*rsseService.cpp*00749*07000*2010JUL21*21:41:55
  
.....
****0000004884*acsoapendpoint.cpp*00890*06000*2010JUL21*21:46:33
 AcSoapEndPoint::UDPSoapHandler() Hanling UDP message
****0000004884*acsoapendpoint.cpp*00890*06000*2010JUL21*21:46:34
 AcSoapEndPoint::UDPSoapHandler() Hanling UDP message
****0000004884*acsoapendpoint.cpp*00890*06000*2010JUL21*21:46:39
 AcSoapEndPoint::UDPSoapHandler() Hanling UDP message
****0000004884*acsoapendpoint.cpp*00890*06000*2010JUL21*21:46:40
 AcSoapEndPoint::UDPSoapHandler() Hanling UDP message

Believe it or not, there is relevant data (eventually) w/in these logs. Smilie

The timestamp is in this format:
YYYYMMMDD*TT:TT:TT
or
Code:
date +%Y%b%d*%T | tr [a-z] [A-Z]

# 4  
Old 08-04-2010
OK,
and what is the desired result (and what's the problem)?
# 5  
Old 08-04-2010
Quote the strings with a single quote and use 2 backslashes \\ to quote the asterix sign:
Code:
#!/bin/sh

TIMESRT='2010JUL21\\*00:00:00'
TIMEEND='2010JUL21\\*01:00:00'

awk -v START1="$TIMESRT" -v END1="$TIMEEND" '
$0 ~ START1{f=1}
$0 ~ END1{print;f=0}
f' $RLOG > output.log

This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 08-04-2010
Wow... excellent, thanks Franklin52!

I never tried, (or knew) the "\\" before the "*".
Smilie


@radoulov, I am trying to grab a chunk of a log file using the timestamps w/in the log file.

The problem was with the "*" in the timestamp, I wasn't capturing data.


Thanks fellas for your replies, and your knowledge.
# 7  
Old 08-04-2010
Quote:
Originally Posted by Matthias03
[...]

@radoulov, I am trying to grab a chunk of a log file using the timestamps w/in the log file.

The problem was with the "*" in the timestamp, I wasn't capturing data.
[...]
I was just trying to avoid guessing ..., that's why I wanted to see the required output.

When you need to match an exact string, instead of a regular expression, just use the right tool - the index function.
Otherwise you'll need to quote (sometimes more than once) every special character in the pattern.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Awk to Search and Replace inside the pipe "|"

Hi, Anyone can help me on how to replace the qoutes inside the pipe | in my Text File like belows; "AAAA"|"Test "1-A""|"Test AAAA"|"This is A" "BBBB"|"Test "1-B""|"Test BBBB"|"This is B" "CCCC"|"My Test C"|"Test "CCCC""|"This is C" The output I need like belows; "AAAA"|"Test 1-A"|"Test... (12 Replies)
Discussion started by: fspalero
12 Replies

7. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question