how to add text after a particular line on vi editor


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add text after a particular line on vi editor
# 1  
Old 03-04-2009
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
# 2  
Old 03-04-2009
If you have Perl...

Code:
$ 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

# 3  
Old 03-04-2009
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
# 4  
Old 03-04-2009
Means you have a problem in your script. Try to execute it without the /dev/null/.

To insert a line simply use:
Code:
sed '9i New line' file

# 5  
Old 03-04-2009
can you post your ch script?
# 6  
Old 03-04-2009
Thanx ripat. This soluntion looks good. very small and effective. Smilie

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.
# 7  
Old 03-04-2009
Regarding script:

I think its issue with server. its mail feature might be disabled . i m checking more on that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add text to the end of line

Seems simple but ive been searching for a good hour of so I have a text file and would like to add a string to the end of line 5 ( as an example) to ake tings hard the line number we have to add the text to is stored in a variable cunningly name $Line_to_append any ideas on how this could... (2 Replies)
Discussion started by: dunryc
2 Replies

2. Shell Programming and Scripting

How to add the line to previous line in | delimited text?

Hi All, I am new to Unix and I have one challenge and below are the details. I have pipe delimited text file in that data has span into multiple lines instead of single line. Sample data. Data should be like below for entire file. 41|216|398555|77|provided complete NP outcome data ... (21 Replies)
Discussion started by: Narasimhasss
21 Replies

3. Shell Programming and Scripting

sed to add text in new line

help i need to add a "nfsd" in new line after cron ex: cron rpcbind output: cron nfsd rpcbind i use sed -e "/cron/G; s/$/nfsd/" myfile output: cron nfsd rpcbindnfsd (5 Replies)
Discussion started by: jamilzain
5 Replies

4. Windows & DOS: Issues & Discussions

Trying to add text to the beginning of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (6 Replies)
Discussion started by: pasc
6 Replies

5. Shell Programming and Scripting

how to add text into the last line of text file

I need help with insert text to the last line of text file with echo command I know can do something like echo "i4\n$logtext\n.\nwq" | ex -s $file can insert to first line, but how can i change this code in order to insert to the last line of text file? please help, thank you :( (2 Replies)
Discussion started by: gavin_L
2 Replies

6. Shell Programming and Scripting

Add text to specified line

Hi All, I am writing a script in which i need to add text "ABC" at line 14 in file input.txt Can someone please help me how to do it? Thanks. Regards, Tushar (6 Replies)
Discussion started by: tushar_shah06
6 Replies

7. Shell Programming and Scripting

how to add text as the new first line without getting ^M?

hi all, first post here how to add text as the new first line without getting the "^M"s in vim? I've tries echo "new_test" cat "file_to_be_edited" > new file and sed -i '1i\ new_test' file_to_be_edited these two can successfully add new_test as the new first line, but both of them... (3 Replies)
Discussion started by: sunnydanniel
3 Replies

8. Shell Programming and Scripting

Add ; to every line in text file

Please help to add ; to every line in a text file i Have tired sed 's/$/ ; /g' > /tmp/drop_tables.sql but not working :( Thanks (2 Replies)
Discussion started by: bluebird5m
2 Replies

9. UNIX for Dummies Questions & Answers

Pasting text in VI editor from a different editor

Hi, I knw its a silly question, but am a newbie to 'vi' editor. I'm forced to use this, hence kindly help me with this question. How can i paste a chunk 'copied from' a different editor(gedit) in 'vi editor'? As i see, p & P options does work only within 'vi'. (10 Replies)
Discussion started by: harishmitty
10 Replies

10. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies
Login or Register to Ask a Question