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?
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.
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?
## 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 ##
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.
The "RawContent" variable is populated as an output from another program. It is not a file.
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.
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.