Computing.Net > Forums > Unix > Using variable string in AWK

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Using variable string in AWK

Reply to Message Icon

Name: Ravi
Date: May 18, 2003 at 08:07:52 Pacific
OS: HP
CPU/Ram: HP_UX
Comment:

Hi,

I was trying to pass variable search strings inside AWK and use it as START and END criteria.
This is how it goes.
Within shell script
declare START and END as

START_POINT="This is the Starting Point"
END_POINT="This is the End path"

Pass the two variables to awk and to output all the lines falling under the two search strings.

i.e

awk -v START1="$START_POINT" -v END1="$END_POINT" '/START1/,/END1/ { print }' inputfilename.

My intention is to output all the lines in file "inputfilename" which are in between lines
This is the Starting Point
and
This is the End path


I cannot hardcode the lines inside awk programme as the start and end criteria has to be passed dynamically.

Please help me out.



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: May 28, 2003 at 06:20:22 Pacific
Reply:

It is expecting RE's for the search arguments (between the slashes). For /START1/ it looks for string "START1".

But you can code your own:

awk -v START1=$x -v END1=$y '{
if (match($0,START1))
   printflag=1
if (printflag==1)
   print
if (match($0,END1))
   printflag=0
}' infile

The above simulates the awk construct, printing possibly multiple groups of lines, including a group that "starts" and never "ends".

To process one group of lines only, you can include an exit when end string is detected.

To print each group of lines less the triggering "start" and "end" lines, just switch the first and third if-statements.


1

Response Number 2
Name: Ravi1
Date: June 18, 2003 at 01:12:40 Pacific
Reply:

Thanks James

Your solution is prefect

Regards
Ravi


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Using variable string in AWK

using string variable in awk www.computing.net/answers/unix/using-string-variable-in-awk/8040.html

Square brackets as string in awk www.computing.net/answers/unix/square-brackets-as-string-in-awk/5206.html

Shell variable in AWK www.computing.net/answers/unix/shell-variable-in-awk/6651.html