Link to home
Start Free TrialLog in
Avatar of rdashokraj
rdashokraj

asked on

Script to insert a string in alternative lines

Hi Folks,

I have a requirement to write a script which will insert a particular string (say string_insert) in every alternative line of a file.

Let say I have a file like this:

#cat myfile
this is line no 1
this is line no 2
this is line no 3
this is line no 4
this is line no 5
..
..

After executing the script the file should appear like this.

#cat myfile
string_insert
this is line no 1
string_insert
this is line no 2
string_insert
this is line no 3
string_insert
this is line no 4
string_insert
this is line no 5
string_insert
..
...
so on.....


Please help me. I tried some logic using sed but couldn't succeed.

Thanks,
Ashok
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rdashokraj
rdashokraj

ASKER

Ozo,

Can i know how your perl syntax works for this solution?  I'm not getting the option "-lpe" ? Please explain.

perl -i -lpe 'print "string_insert"' myfile

Thanks
I thank everyone for the solution !!
perldoc perlrun

       -e commandline
            may be used to enter one line of program.  If -e is given, Perl
            will not look for a filename in the argument list.  Multiple -e
            commands may be given to build up a multi-line script.  Make sure
            to use semicolons where you would in a normal program.

       -l[octnum]
            enables automatic line-ending processing.  It has two separate
            effects.  First, it automatically chomps $/ (the input record sep-
            arator) when used with -n or -p.  Second, it assigns "$\" (the
            output record separator) to have the value of octnum so that any
            print statements will have that separator added back on.  If oct-
            num is omitted, sets "$\" to the current value of $/.  For
            instance, to trim lines to 80 columns:

                perl -lpe 'substr($_, 80) = ""'

            Note that the assignment "$\ = $/" is done when the switch is pro-
            cessed, so the input record separator can be different than the
            output record separator if the -l switch is followed by a -0
            switch:

                gnufind / -print0 | perl -ln0e 'print "found $_" if -p'

            This sets "$\" to newline and then sets $/ to the null character.


       -p   causes Perl to assume the following loop around your program,
            which makes it iterate over filename arguments somewhat like sed:

              LINE:
                while (<>) {
                    ...             # your program goes here
                } continue {
                    print or die "-p destination: $!\n";
                }

            If a file named by an argument cannot be opened for some reason,
            Perl warns you about it, and moves on to the next file.  Note that
            the lines are printed automatically.  An error occurring during
            printing is treated as fatal.  To suppress printing use the -n
            switch.  A -p overrides a -n switch.

            "BEGIN" and "END" blocks may be used to capture control before or
            after the implicit loop, just as in awk.