Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shell Script for addition ann run against multiple files

Posted on 2006-06-20
11
Medium Priority
?
347 Views
Last Modified: 2008-03-10
I have the following Shell Script  where it replacing old string/lines with new lines in a
file (actually it is a makefile)
The script works halfway though, , since when it encounters space or tab it does not replace old strings/lines

My question is using this K-Shell Script
                 1. how to take care space or tab fro the replacement ?
                  2. I want to add a new line (LINK = cc -G -mt -o) in all files  ?
                 3. I am running the script against one file

sed -f test.sed input.txt > output.txt



Instead how can I  run the script against all 100 files ( they are all same type makefile) ?

Following is the script I am using

!/bin/ksh
#
#The script substitute old lines with new lines in *.mak file

s/-D_HPUX_SOURCE -DFND_HPUX/-D__sun -DFND_SUN/
s/-Dfnd_hp -c -Aa $(DEBUG) +O2 +DS897/-Dfnd_sun \$(DEBUG) -c -Xa -O -xcg92 -xcode=pic32/
s/LINKFLAGS     =  -Wl,-B,immediate,+s,-q/#LINKFLAGS     =  -Wl,-B,immediate,+s,-q/
LINK = cc -G -mt -o/

#Run Syntax
#sed -f test.sed input.txt > output.txt

Thanks :)
0
Comment
Question by:dmshawkat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 16947755
Hi dmshawkat,

In the second line you need to escape the first $, too.   $(DEBUG)


Good Luck!
Kent
0
 
LVL 6

Expert Comment

by:JJSmith
ID: 16949402



ls -1 input* | while read infile                           # generate flist of filenames (could use find instead of ls)

do                                                                  # loop
   
    outfile=`echo $infile | sed s!input!output!`     # create output filename
 
    sed -f test.sed $infile > $outfile                     # edit contents  

done                                                               # end loop


OR if you want to overwrite original:

ls -1 input* | while read infile                           # generate flist of filenames (could use find instead of ls)

do                                                                  # loop
   
    sed -f test.sed $infile > /tmp/tempf$$           # edit contents  

    cp /tmp/tempf$$  $infile                               # overwrite original

done                                                               # end loop


Cheers
JJ
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 16952149
s/-D_HPUX_SOURCE -DFND_HPUX/-D__sun -DFND_SUN/
s/-Dfnd_hp -c -Aa $(DEBUG) +O2 +DS897/-Dfnd_sun $(DEBUG) -c -Xa -O -xcg92 -xcode=pic32/
s/LINKFLAGS.*-B,immediate.*/#&/
s/LINK = cc -G -mt -o/your replacement here/

write above 4 line in file called test.sed, then
#Run Syntax
#sed -f test.sed input.txt > output.txt
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dmshawkat
ID: 16980516

Hello Dear
 I am getting following erra when runnining the replacement


sed: command garbled: s/LINKFLAGS      = -G -mt -o/LINKFLAGS      = -G -mt/

Any hlep would be greatly appraciated

Thanks
Shawkat
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16981520
did you test my suggestion?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17555089
still believe in my suggestion ..
0
 
LVL 20

Expert Comment

by:Venabili
ID: 17555892
Hm.. I wondered a bit here :)
I will check it again but something is not quite ok for me... Just my gut probably but let me rethink
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17556056
> .. is not quite ok for me ..
ok, may be the + needs to be escaped if you're using Gnu sed
but I assumed standard AT&T sed 'cause we're in Unix TA :-)
0
 
LVL 20

Expert Comment

by:Venabili
ID: 17556134
:)Yeah... :)

Time to change the recommendation... :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question