Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

awk and string variable...

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???

9 REPLIES
Honored Contributor

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

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

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.
Highlighted

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

Re: awk and string variable...

start="Program Start => 10:47"
end="Program Start => 12:15"
awk "/$start/,/$end/" filename

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

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

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!

Re: awk and string variable...

thanks