LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Insert line using sed or awk at line using line number as variable (https://www.linuxquestions.org/questions/programming-9/insert-line-using-sed-or-awk-at-line-using-line-number-as-variable-893652/)

sunilsagar 07-25-2011 10:38 AM

Insert line using sed or awk at line using line number as variable
 
Hello All,

I want to insert a line at a particular line number using sed or awk.
where line number is not fixed and is in form of variable.

I want to use variable in sed or awk command.

I tried something like below, but no luck.

line=10
sed '$linei\newline' file

grail 07-25-2011 11:08 AM

Try changing the single quotes to double and see how you go.

sunilsagar 07-25-2011 07:46 PM

Getting error

$ sed "$linei\abcd" 1.conf
sed: 1: "\abcd": unterminated regular expression

sunilsagar 07-25-2011 08:13 PM

Getting error

$ sed "$linei\abcd" 1.conf
sed: 1: "\abcd": unterminated regular expression

Nominal Animal 07-25-2011 09:27 PM

I prefer to use awk for this, since then I don't need to worry about escaping:
Code:

awk -v "n=line-number" -v "s=line to insert" '(NR==n) { print s } 1' input-file
First line is line number 1. If the file is too short, it will not insert anything.

This second variant will append the line, if the line number is larger than the number of lines in the file. It will not add empty lines, though; just the line to be inserted:
Code:

awk -v "n=line-number" -v "s=line to insert" '
    (NR==n) { n=-1 ; print s }
            { print $0 }
        END { if (n>0) print s }' input-file

To do the above safely to any file, I'd write a small shell script. I prefer Bash, but since I don't know which shell you prefer, here's something that should work with any /bin/sh. Note, this is untested code:
Code:

#!/bin/sh

LINENUM=55
LINESTR="inserted line"

if [ ":$*" = ":" ] || [*":$*" = ":-h" ] || [ ":$*" = ":--help" ]; then
    exec >&2
    echo ""
    echo "Usage: $0 [ -h | --help ]"
    echo "      $0 filename(s)"
    echo ""
    exit 0
fi

WORK=`mktemp -d` || exit $?
trap "rm -rf '$WORK'" EXIT

while [ $# -gt 0 ]; do
    if [ ! -f "$1" ]; then
        echo "$1: File not found." >&2
        exit 1
    fi

    awk -v "n=$LINENUM" -v "s=$LINESTR" '
            (NR==n) { n=-1; print s }
                    { print $0 }
            END    { if (n>0) print s }'
        ' "$1" > "$WORK/temp" || exit $?

    chown --reference="$1" "$WORK/temp" 2>/dev/null
    chmod --reference="$1" "$WORK/temp" 2>/dev/null

    mv -f "$WORK/temp" "$1" || exit $?

    echo "$1: Modified." >&2
    shift 1
done


crts 07-25-2011 09:54 PM

Quote:

Originally Posted by sunilsagar (Post 4425053)
Getting error

$ sed "$linei\abcd" 1.conf
sed: 1: "\abcd": unterminated regular expression

Hi,

which sed version are you using? This should work with GNU sed:
Code:

sed "${line} i abcd" 1.conf
Notice the spaces. Make sure that the variable ${line} is not empty.

sunilsagar 07-25-2011 10:25 PM

Great !! thanks so much Guys ..
This worked
sed "${line} i abcd" 1.conf

grail 07-25-2011 10:34 PM

If you are not sure why it worked? It is because your previous invocation had it looking for a variable called $linei
and then passing \a which sed does not understand as an escape character.

OldManRiver 01-27-2012 02:56 PM

New last line
 
All,

Seeing the code here, what method is used to count all the lines in a current file and put the new line at X+1?

I particular I need a script to add lines to my "sources.list" repositories file, when configuring new servers.

Thanks!

OMR

Cedrik 01-27-2012 03:09 PM

You want to append lines ? Try:
Code:

echo "New line appended" >> sources.list

OldManRiver 02-02-2012 04:56 PM

Replacing existing lines
 
All,

OK the "echo "string" >> "path/to/file" works, but got a couple where I need SED or AWK to rewrite the existing lines,

What is correct syntax for that?

Thanks!

OMR

David the H. 02-03-2012 10:48 AM

Since you seem to want to learn how to do more, it's time for you to start studying up on the tools you want to use.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

A couple of regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html


All times are GMT -5. The time now is 07:03 AM.