how to add text after a particular line on vi editor

hi

suppose i have file 0f 10 lines.

i want to add something after 8 line.

may i append it anyhow without opening file.

eg. i can appned at last.

echo text >> file

Thanks

If you have Perl...

$ cal 2009 | head > xxx
$ cat -n xxx
     1                                 2009
     2
     3         January               February                 March
     4  Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     5               1  2  3    1  2  3  4  5  6  7    1  2  3  4  5  6  7
     6   4  5  6  7  8  9 10    8  9 10 11 12 13 14    8  9 10 11 12 13 14
     7  11 12 13 14 15 16 17   15 16 17 18 19 20 21   15 16 17 18 19 20 21
     8  18 19 20 21 22 23 24   22 23 24 25 26 27 28   22 23 24 25 26 27 28
     9  25 26 27 28 29 30 31                          29 30 31
    10
$ perl -pi -le 'print "New line added!!" if $. == 9' xxx
$ cat -n xxx
     1                                 2009
     2
     3         January               February                 March
     4  Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     5               1  2  3    1  2  3  4  5  6  7    1  2  3  4  5  6  7
     6   4  5  6  7  8  9 10    8  9 10 11 12 13 14    8  9 10 11 12 13 14
     7  11 12 13 14 15 16 17   15 16 17 18 19 20 21   15 16 17 18 19 20 21
     8  18 19 20 21 22 23 24   22 23 24 25 26 27 28   22 23 24 25 26 27 28
     9  New line added!!
    10  25 26 27 28 29 30 31                          29 30 31
    11

Hey it worked. Thanx so much..

i have one more question pls.

I fired a script named "ch"

i am getting the below error messages and its not working . as it should mail me some text after executing.

i ran

bash-3.00# ./ch > /dev/null 2>&1

Error messages :

bash-3.00# ./ch > /dev/null 2>&1
bash-3.00# Mar 4 15:23:15 sdmdlbld01 sendmail[5458]: [ID 702911 mail.alert] unable to qualify my own domain name (sdmdlbld01) -- using short name
Mar 4 15:23:15 sdmdlbld01 sendmail[5460]: [ID 702911 mail.alert] unable to qualify my own domain name (sdmdlbld01) -- using short name
You have new mail in /var/mail/root

I think it should not flash any error message

Means you have a problem in your script. Try to execute it without the /dev/null/.

To insert a line simply use:

sed '9i New line' file

can you post your ch script?

Thanx ripat. This soluntion looks good. very small and effective. :b:

but the command is not executing

bash-3.00# sed '2i New line' file
sed: command garbled: 2i New line

may you please check ??

The script is fine. Its working fine on other servers. We manage around 80 servers. Working fine on all except this.

Regarding script:

I think its issue with server. its mail feature might be disabled . i m checking more on that.

If your script is working fine on 79 ouf of 80 servers, then it sounds obvious that something is different on this machine.

yes . i am digging into it. :slight_smile:

But now i am concerned about

bash-3.00# sed '2i New line' file
sed: command garbled: 2i New line

i am searching for a small command for this. :frowning:

check this one out to see if it helps.