unix shell script

set -x
for file in /env/test/test123/$env$len/local/*.txt
do sed -e "/third line/a\\
add a new line_2" $file >$file.done
cp $file.done $file

Hi experts, The above script run fine, but I will like the below to run if "len" is "af"  

sed -e "/third line/a\\
add a new line_2" $file >$file.done
cp $file.done $file
sed -e "/four line/a\\
add a new line_3" $file >$file.done
cp $file.done $file

and the below should run if "len" is "tmp"

sed -e "/third line/a\\
add a new line_3" $file >$file.done
cp $file.done $file
sed -e "/four line/a\\
add a new line_4" $file >$file.done
cp $file.done $file
jko n127Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can give sed more than one -e argument. You can also write a full-blown sed script with multiple transformations to apply. Once that gets a bit complicated, I'd reach for perl since it has a richer set of control structures. Or perhaps awk if I was daunted by how much it takes to learn perl.

Your use of ordinal lines in the example is going to become confusing as the inserted lines go in. You can use sed to add lines at specific line positions just as easily as it can append a line to a line with a pattern match.

Also, ksh has conditional statements.
if len=="tmp" ; then
...more commands here...

Open in new window

Maybe these hints will be enough to get you to the next stage?
You could as well set variables depending on the content of "len" to then use these variables in "sed".
Using several scripts in one "sed" call is also a good idea.

Testing $len and setting the variables could look like this:

if [[ $len = "af" ]]; then
  A1="add a new line_2"
  A2="add a new line_3"
    elif [[ $len = "tmp" ]]; then
      A1="add a new line_3"
      A2="add a new line_4"
  else echo "Wrong Parameter: $len" ; exit

Please note that if the script's first parameter is neither "af" nor "tmp" the script will exit (see the "else" branch above).

In a former thread we already discussed how to use variables in "sed",
so your new "sed" command could then look just like this:

sed -e "/third line/a\
$A1" -e "/four line/a\
$A2" $file > $file.done
cp ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
One other tiny point - the "#!" line *must* be the first line in the script, otherwise it is just a comment line and will not select the shell to use.  Put the "set -x" line *after* the "#!" line.
jko n127Author Commented:
I used the solution provided and it worked. Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.