LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2011, 10:38 AM   #1
sunilsagar
Member
 
Registered: Jul 2011
Posts: 30

Rep: Reputation: Disabled
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
 
Old 07-25-2011, 11:08 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Try changing the single quotes to double and see how you go.
 
Old 07-25-2011, 07:46 PM   #3
sunilsagar
Member
 
Registered: Jul 2011
Posts: 30

Original Poster
Rep: Reputation: Disabled
Getting error

$ sed "$linei\abcd" 1.conf
sed: 1: "\abcd": unterminated regular expression
 
Old 07-25-2011, 08:13 PM   #4
sunilsagar
Member
 
Registered: Jul 2011
Posts: 30

Original Poster
Rep: Reputation: Disabled
Getting error

$ sed "$linei\abcd" 1.conf
sed: 1: "\abcd": unterminated regular expression
 
Old 07-25-2011, 09:27 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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
 
Old 07-25-2011, 09:54 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by sunilsagar View Post
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.
 
Old 07-25-2011, 10:25 PM   #7
sunilsagar
Member
 
Registered: Jul 2011
Posts: 30

Original Poster
Rep: Reputation: Disabled
Great !! thanks so much Guys ..
This worked
sed "${line} i abcd" 1.conf
 
Old 07-25-2011, 10:34 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
1 members found this post helpful.
Old 01-27-2012, 02:56 PM   #9
OldManRiver
LQ Newbie
 
Registered: Aug 2004
Posts: 26

Rep: Reputation: 5
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

Last edited by OldManRiver; 01-27-2012 at 02:59 PM.
 
Old 01-27-2012, 03:09 PM   #10
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
You want to append lines ? Try:
Code:
echo "New line appended" >> sources.list
 
Old 02-02-2012, 04:56 PM   #11
OldManRiver
LQ Newbie
 
Registered: Aug 2004
Posts: 26

Rep: Reputation: 5
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
 
Old 02-03-2012, 10:48 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
filter source line based on results line in log using awk and sed samanp Programming 5 04-06-2011 09:42 AM
[SOLVED] Sed - print line number along with the line ? ntpntp Linux - Newbie 6 01-30-2011 05:58 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
Pass a variable as a line number in sed dx0r515t Programming 7 03-30-2007 04:55 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration