Insert text into file at given line number and position

Posted on 2006-03-20
Last Modified: 2008-01-09
I have text that I need to insert at specific points in a file...I know the line number and position in the line.  Is there a way to do this using sed or awk or some other unix function?
Question by:kncoughlin
    LVL 23

    Expert Comment

    In the past, I have used wc, head & tail to do this:
    Use wc to count the lines in the original file.
    Use head to take the first n lines, redirect it to a new file.
    Concatenate the new content to the new file.
    Use tail to take the last wc-n lines, concatenate that to the new file.


    Author Comment

    Brett--Thanks for the quick response.  That works great for the line...any ideas on how I could insert at a specific postition in the line??
    LVL 84

    Accepted Solution

    awk 'NR==3{$0=substr($0,1,5) "insert" substr($0,6)}{print}'
    LVL 27

    Expert Comment

    You don't need an awk, sed is enough in most circumstances.

    Suppose you have environment variables:

    If your sed supports -E flag, this will work:

    sed -E "${LINENO}s/^(.{${POSITION}})(.*)/\1${TEXT}\2/" < ${FILE}

    inserts text and outputs to stdout.

    Tested on FreeBSD.
    Will NOT work on Solaris when native sed is used, because it doesn't support extended regular expression.

    LVL 6

    Expert Comment

    amazing ozo !

    kncoughlin, I checked ozo's command,
    you just need to give AWK the parameters, like in this example :

     set LINE = 3 ; set LOC = 15 ; set TEXT = "BLABLA" ; set FILE = "file"
     awk 'NR==l{$0= substr($0,1,loc) text substr($0,loc+1)}{print}' l=$LINE loc=$LOC text=$TEXT $FILE

    the parameters given to awk between the closing "'" sign to the file_name


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
    Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    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.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now