LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-02-2008, 05:36 AM   #1
santhoshv
LQ Newbie
 
Registered: Apr 2008
Posts: 8

Rep: Reputation: 0
SHELL Script to insert a line.


Hi,

I am using the Korn shell and I wanted to write a SHELL script for the following requirement.

1. Delete the first line and last line of the input file.
2. Insert a line at the first and last line position of the file.

I am able to delete the first line with the following command.

SED '1d' Testfile.xml

But I dont know how to get the last line number and delete it.
Even I am not aware of which command to be used to insert a line at the first and last line position.

Any help is appreciated...!!!!

Thanks
SaNv
 
Old 04-02-2008, 05:40 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Sed uses $ for the last line, so sed '$d' will delete the last line.

To insert a line use the "i" command.
To add a line use the "a" command. You could use >> to append to the file as well.
 
Old 04-02-2008, 06:06 AM   #3
santhoshv
LQ Newbie
 
Registered: Apr 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
To insert a line use the "i" command.
To add a line use the "a" command. You could use >> to append to the file as well.

Can u Please give the syntax using "i" command to inser a line at the first position.

Thanks
SaNv..
 
Old 04-02-2008, 06:12 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
sed '1i\
Inserted Line.' file >newfile

Note: Sed has both a man page and an info manual. You may also be able to find "Sed & Awk" 1st edition on the net.

Last edited by jschiwal; 04-02-2008 at 06:13 AM.
 
Old 04-02-2008, 06:18 AM   #5
santhoshv
LQ Newbie
 
Registered: Apr 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
sed '1i\Inserted Line.' file >newfile
Hi jschiwal
Thanks for ur response.

However even I have tried this before and it is throwing an error
sed: 0602-404 Function 1i\hello cannot be parsed.
I am using the following command
sed '1i\hello' Testfile.xml
 
Old 04-02-2008, 06:20 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Press [enter] after the backslash. The text to be inserted should be on the next line. The backslash escapes the newline.
There shouldn't be any text after the backslash.
 
Old 04-02-2008, 06:32 AM   #7
santhoshv
LQ Newbie
 
Registered: Apr 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Hi jschiwal,

Thank U very much..!
That worked.....

However It is not inserting the last line.
I found that there is only a blank space in the last line.
I am using the following command.

sed '$d' Testfile.xml | sed '1d' | sed '1i\
<?xml version="1.0" encoding="UTF-8"?><ns0:MT_gasxml xmlns:ns0="urn:Germanytrial">
' | sed '$i\
</ns0:MT_gasxml>' > output.xml


Please help me in this...!
 
Old 04-02-2008, 06:37 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Use the add command instead. Or substitute your new line for the last line instead of deleting.

Last edited by jschiwal; 04-02-2008 at 06:38 AM.
 
Old 04-02-2008, 06:47 AM   #9
santhoshv
LQ Newbie
 
Registered: Apr 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Hi jschiwal

How can I declare a variabe and assign the last line number to it?
 
Old 04-03-2008, 06:10 AM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You don't need to know the last line number. You can use $ instead. To answer your question directly, you could use "wc -l" to count the lines in a file.

Last edited by jschiwal; 04-03-2008 at 03:04 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
SHELL Script to insert lines after certain Intervals rahulruns Linux - General 9 02-26-2008 01:21 AM
shell script: insert line in a file noir911 Programming 6 02-04-2008 10:42 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 07:48 AM
Script to insert string in first line of a file minil Programming 13 01-02-2006 11:56 PM


All times are GMT -5. The time now is 08:41 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration