[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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
0
rdashokraj
Asked:
rdashokraj
3 Solutions
 
ozoCommented:
perl -i -lpe 'print "string_insert"' myfile
0
 
ghostdog74Commented:
awk '{ print "string_insert"}1' file
0
 
omarfaridCommented:
Hi,

Try

awk '{ print "string_insert" ; print $0 }' myfile > myfile.$$
mv myfile.$$ myfile
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
rdashokrajAuthor Commented:
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
0
 
rdashokrajAuthor Commented:
I thank everyone for the solution !!
0
 
ozoCommented:
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.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now