awk code to inspect variable before printing

i have a unique scenario id like help with.

im currently running this command and it does what i want:

printf '%s\n' "${RawContent}" | awk '/##  Beginning Stages  ##/,/## Ending Stages ##/' | awk '!/^#.*\!|^#\!|DefaultError/' 

Can this be shortened? I'm looking for something portable as i intend to use this across several different unix platforms.

EDIT: I'm adding the content of "RawContent":

RawContent:

##  Beginning Stages  ##

#!/bin/sh

echo "What is your name? "
read usname

echo 

echo "The date and time right now is `date`"

echo 

echo "Current directory is `pwd` "

DefaultError=none

## Ending Stages ##

The content of "RawContent" varies but my code basically does precisely what i need, which is:

1. Grabs the data that is between the two patterns of "##  Beginning Stages  ##" and "## Ending Stages ##"
2. Removes all lines that have contain specific text

The third thing i need it to do is to print absolutely nothing if the result of 1 and 2 contain the pattern "WrongValue=".

Here is how I would do it:

VarA=$(printf '%s\n' "${RawContent}" | awk '/##  Beginning Stages  ##/,/## Ending Stages ##/' | awk '!/^#.*\!|^#\!|DefaultError/')

case "${VarA}" in
*WrongValue=*)
   j=j  # do nothing
;;
*)
  printf '%s\n' "${VarA}"
;;
esac

How can I optimize this and also have the optimized version usable across all/most Unix systems?

You definitely know this already:

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

I really believe i provided more than adequate information.

os: all unix oses. linux, sunos, ubuntu, aix
shell: /bin/sh

my apologies for the inconvenience.

Knowing that you're going to be using /bin/sh on Solaris 10 and older UNIX systems eliminates the possibility of using $(command) command substitution. So you might have warned us that the code you say does what you want does not, in fact, do what you want on all UNIX systems.

One could also provide some sample inputs and the corresponding desired outputs.

Do you still believe you provided more than adequate information about your environment?

Do you really want people in here to build their own test cases from the sparse info that you provided? Or do you want untested proposals that might or might not work?

1 Like

My apologies everyone.

Below is the content of "RawContent":

##  Beginning Stages  ##

#!/bin/sh

echo "What is your name? "
read usname

echo 

echo "The date and time right now is `date`"

echo 

echo "Current directory is `pwd` "

DefaultError=none

## Ending Stages ##

As the sample doesn't contain the "WrongValue" string, you don't seem to want that condition tested.

Would this come close to what you need?

printf '%s\n' "${RawContent}" |
awk '
/##  Beginning Stages  ##/,
/## Ending Stages ##/   {if (!/^#.*\!|DefaultError/)    {TMP = TMP DL $0
                                                         DL = ORS
                                                        }
                         if (/## Ending Stages ##/)     {if (TMP !~ /WrongValue/) print TMP
                                                         DL = TMP = ""
                                                        }
                        }
' 

How is the "RawContent" variable populated? If by reading a file, it might be more efficient to work on that file immediately circumventing the variable assignment.

1 Like

Thank you for your patience with me.

  1. The "RawContent" variable is populated as an output from another program. It is not a file.
  2. My mistake. I forgot to include the WrongValue in the content. But, the thing is, sometimes the RawContent variable will contain it. Other times, it wont.

Thanks again!

Why not pipe that other program's output into the awk script immediately? If, on top, you need it for other, different purposes, use the tee command to create an temp file.