[Fwd: question about sed.] 
Author Message
 [Fwd: question about sed.]

[ Attached Message ]

From:
To:
Cc:
Date: Sun, 11 Oct 1998 12:17:24 -0400
Local: Sun, Oct 11 1998 12:17 pm
Subject: question about sed.
Hi ,

 I could not find any news group on SED so I am posting this message here.

I have been trying to solve the following problem.

I am trying to add an "include statement" in 200 .c and .h files. I have to add
this statement only once.
The logic I am using is the first occurance of  "#" insertthe include statement.

This does not seem to work if the first line in the file is starting with '#".
Can you please suggest any other ideas.

I am working in C/UNIX enviornment under solaris operating system.
Following is the code that I am using.

1,/^#/{
/^#/i\
#ifdef MARK2000\
#include "mark2000.h"\
#endif\

Quote:
}

Thanks
Praveen Kamisetty



Thu, 29 Mar 2001 03:00:00 GMT  
 [Fwd: question about sed.]

Quote:
> I am trying to add an "include statement" in 200 .c and .h files. I
> have to add this statement only once. The logic I am using is the
> first occurance of "#" insert the include statement. This does not
> seem to work if the first line in the file is starting with '#".

> Can you please suggest any other ideas.

> I am working in C/UNIX enviornment under solaris operating system.
> Following is the code that I am using.

> 1,/^#/{
> /^#/i\
> #ifdef MARK2000\
> #include "mark2000.h"\
> #endif\
> }

Praveen,

You're using sed at Lucent Technologies to solve a problem for which
awk is the more natural choice? Does your boss know? ;-)

This simple Bourne shell loop uses awk instead of sed and makes a
backup copy of each file (with the extension ".bak"):

for file in *.c *.h
do
  echo "$file" 1>&2
  mv "$file" "$file".bak
  awk '
    /^#/ && FILENAME != filename {
      print "#ifdef MARK2000\n#include \"mark2000.h\"\n#endif"
      filename = FILENAME
    }
    { print }
  ' "$file".bak >"$file"
done

Perl has a nice command-line switch that makes editing multiple files
"in place" even easier than using a shell loop. The following Perl
script is functionally identical to the shell script above:

#!/usr/bin/perl -pi.bak

if (/^#/ && $ARGV ne $argv) {
    print "#ifdef MARK2000\n#include \"mark2000.h\"\n#endif\n";
    $argv = $ARGV;

Quote:
}

Hope this helps.

--
Jim Monty

Tempe, Arizona USA                                 ESCHEW OBFUSCATION!



Fri, 30 Mar 2001 03:00:00 GMT  
 [Fwd: question about sed.]
On Sun, 11 Oct 1998 12:23:01 -0400, Praveen Kumar Kamisetty
Quote:

>Hi ,
> I could not find any news group on SED so I am posting this message here.

[Followups set to: comp.editors]
Quote:
>I have been trying to solve the following problem.
>I am trying to add an "include statement" in 200 .c and .h files. I have to add
>this statement only once.
>The logic I am using is the first occurance of  "#" insertthe include statement.
>This does not seem to work if the first line in the file is starting with '#".
>Can you please suggest any other ideas.
>I am working in C/UNIX enviornment under solaris operating system.
>Following is the code that I am using.
>1,/^#/{
>/^#/i\
>#ifdef MARK2000\
>#include "mark2000.h"\
>#endif\
>}
>Thanks
>Praveen Kamisetty


Praveen,
The correct newsgroup is comp.editors.
The correct tool for this job is ed, edit, ex (aka vi).
You only want to do this once per file, whereas sed is designed to
repeatedly perform edits on selected lines in the file.
I have used this technique to add standard headers to files in scripts.
An ed script would be something like your lines 2-4:
        /^#/i\
        ...     insert your stuff
        .       end insert
        w       write
        q       quit
HTH.



Sat, 31 Mar 2001 03:00:00 GMT  
 [Fwd: question about sed.]
#!/usr/bin/sh
ls *.c > /tmp/filelist.tmp
ls *.h >> /tmp/filelist.tmp
for file in `cat /tmp/filelist.tmp`
do
nawk '
{
if ($0 ~ /^#/)
        {
        print $0
        print "your first line of inserted text here"
        print "your second line here, etc."
        }
else {print $0}
Quote:
}

' $file > /tmp/$file
cp /tmp/$file $file
rm /tmp/$file
done
rm /tmp/filelist.tmp

Erik

Quote:

> I am trying to add an "include statement" in 200 .c and .h files. I have to add
> this statement only once.
> The logic I am using is the first occurance of  "#" insertthe include statement.
> This does not seem to work if the first line in the file is starting with '#".



Sat, 31 Mar 2001 03:00:00 GMT  
 [Fwd: question about sed.]
Oops, a minor correction to my previous post (removed). I had forgotten
to disable text insertion after the first occurence of '#'. What did you
expect from a 5-minute hack? This modified "10-minute" code should do
the trick. Perhaps you could do this with sed/sh, but I'm really
disinclined to use sed for all but the simplest processing.

#!/usr/bin/sh
ls *.c > /tmp/filelist.tmp
ls *.h >> /tmp/filelist.tmp
for file in `cat /tmp/filelist.tmp`
do
nawk '
BEGIN{testctr = 0}
{
if ($0 ~ /^#/ && testctr < 1)
        {
        print $0
        print "your first line of inserted text here"
        print "your second line here, etc."
        ++testctr
        }
else {print $0}

Quote:
}

' $file > /tmp/$file
cp /tmp/$file $file
rm /tmp/$file
done
rm /tmp/filelist.tmp

--Erik

Quote:
> The logic I am using is the first occurance of  "#" insertthe include statement.
> This does not seem to work if the first line in the file is starting with '#".
> 1,/^#/{
> /^#/i\
> #ifdef MARK2000\
> #include "mark2000.h"\
> #endif\

> }



Sun, 01 Apr 2001 03:00:00 GMT  
 [Fwd: question about sed.]

Quote:
> I am trying to add an "include statement" in 200 .c and .h files. I have to add
> this statement only once.
> The logic I am using is the first occurance of  "#" insertthe include statement.

> This does not seem to work if the first line in the file is starting with '#".
> Can you please suggest any other ideas.

> I am working in C/UNIX enviornment under solaris operating system.
> Following is the code that I am using.

> 1,/^#/{
> /^#/i\
> #ifdef MARK2000\
> #include "mark2000.h"\
> #endif\

> }

awk '$0~/^#/&&d==0 { d=1
print "#ifdef MARK2000\n#include "mark2000.h\"\n#endif"}
/^/' infile>outfile

not tested
LMS
free sed/awk book:
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz



Mon, 02 Apr 2001 03:00:00 GMT  
 [Fwd: question about sed.]
[I haven't seen the original post, so I follow up here]

% > I am trying to add an "include statement" in 200 .c and .h files. I have to add
% > this statement only once.

This looks to me like a job for ed, which some systems I use still describe as
_the_ system text editor.

 for a in *.[ch]; do ed $a <<HERE
/^#/ i
#include <mrnoodlecopyright.h>
.
w
q
HERE
 done

--

Patrick TJ McPhee
East York  Canada



Mon, 02 Apr 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Newbie awk (sed??) question, regular expressions

2. Awk/Sed Filehandler question

3. A very simple question on SED or AWK for a GURU, and an enjoyable problem

4. How to do this.. (SED question)

5. A question about sed

6. a sed question

7. SED question

8. awk or sed: basic? question

9. Sed Question

10. 2 questions from book sed and awk programming

11. Urgent VI/SED question

12. Stupid question...Is sed in gawk??

 

 
Powered by phpBB® Forum Software