Re: awk and string variable... (337 Views)
Reply
Advisor
Posts: 28
Registered: ‎10-12-2004
Message 1 of 10 (337 Views)

awk and string variable...

Given the following:

Program Start => 10:47
name time day
John 11:09 Monday
Bill 11:11 Tuesday
Sam 11:38 Wednesday
Program End => 12:15

Program Start => 15:38
name time day
John 15:55 Monday
Bill 16:10 Tuesday
Sam 18:21 Wednesday
Program End => 18:44

How do I print just the first block???

awk '/Program Start => 10:47/,/Program End => 12:15/ {print $0}

Putting it in quotes doesn't work... neither does using a variable or maybe I am just assigning it wrong???

Honored Contributor
Posts: 744
Registered: ‎06-15-1998
Message 2 of 10 (337 Views)

Re: awk and string variable...

You missed a closing single quote: -

awk '/Program Start => 10:47/,/Program End => 12:15/ {print $0}' thefile

Also if you just want to display the block you can ommit {print $0} altogether, as awk will just display the matching line(s).
Honored Contributor
Posts: 2,448
Registered: ‎05-22-2001
Message 3 of 10 (337 Views)

Re: awk and string variable...

Hi,

I put your sample text into a file named testawk.txt and I got this to work:

>awk '/Program Start => 10:47/,/Program End => 12:15/ {print $0}' testawk.txt
Program Start => 10:47
name time day
John 11:09 Monday
Bill 11:11 Tuesday
Sam 11:38 Wednesday
Program End => 12:15


JP
Honored Contributor
Posts: 744
Registered: ‎06-15-1998
Message 4 of 10 (337 Views)

Re: awk and string variable...

In addition, if you wanted to print the first block of such an output, regardless of the time, you could use: -

awk 'found != 1 && /Program Start/,/Program End/{print ; found=1}' filename

This way the time stamp wouldn't matter, and it would always display just the first block.
Advisor
Posts: 28
Registered: ‎10-12-2004
Message 5 of 10 (337 Views)

Re: awk and string variable...

How about variables?

start_time="Program Start => 10:47"
end_time="Program End => 12:15"

awk -v start=$start_time end=$end_time '/start/,/end/ {print $0}'

????
Honored Contributor
Posts: 744
Registered: ‎06-15-1998
Message 6 of 10 (337 Views)

Re: awk and string variable...

start="Program Start => 10:47"
end="Program Start => 12:15"
awk "/$start/,/$end/" filename
Advisor
Posts: 28
Registered: ‎10-12-2004
Message 7 of 10 (337 Views)

Re: awk and string variable...

OK...

how about printing out the lines where the first header is the same for every block but the last line is different:

Program Start
name time day
John 11:09 Monday
Bill 11:11 Tuesday
Sam 11:38 Wednesday
Program End => 12:15

Program Start
name time day
John 15:55 Monday
Bill 16:10 Tuesday
Sam 18:21 Wednesday
Program End => 18:44

The awk prints both blocks...
How do I print just the first one if the header is the same??

Honored Contributor
Posts: 4,030
Registered: ‎06-09-2004
Message 8 of 10 (337 Views)

Re: awk and string variable...

We can do your's

1> awk '/Program Start => 10:47/,/Program End => 12:15/ {print $0}'


2>
start_time="Program Start => 10:47"
end_time="Program End => 12:15"
awk -v start="$start_time" -v end="$end_time" '{ if ($0==start) while ( $0!=stop ) { getline;if ($0!=stop ) print $0 }}' testfile


Easy to suggest when don't know about the problem!
Honored Contributor
Posts: 4,030
Registered: ‎06-09-2004
Message 9 of 10 (337 Views)

Re: awk and string variable...

We can get first block when the headers are being same then,

Example:
# cat testfile
Program Start
name time day
John 11:09 Monday
Bill 11:11 Tuesday
Sam 11:38 Wednesday
Program End => 12:15

Program Start
name time day
John 15:55 Monday
Bill 16:10 Tuesday
Sam 18:21 Wednesday
Program End => 12:15

# awk '/Program Start/,/Program End => 12:15/ { if ($0=="Program Start") i++; if (i==1) print $0 }' testfile
Program Start
name time day
John 11:09 Monday
Bill 11:11 Tuesday
Sam 11:38 Wednesday
Program End => 12:15

If you want to change block number then chnage on,

if (i==)

HTH.
Easy to suggest when don't know about the problem!
Advisor
Posts: 28
Registered: ‎10-12-2004
Message 10 of 10 (337 Views)

Re: awk and string variable...

thanks
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.