LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-15-2010, 06:10 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
How would i delete a line at specific line number


Hi guys ,

I m writing a script to delete a line at particular location.

But i m unable to use variable for specifying line number.
for example.
Code:
sed -n '7!p' filename
works fine and deletes 7th line from my file
but
Code:
sed -n '$variable!p' filename
gives following error.

Code:
sed: -e expression #1, char 3: extra characters after command
 
Old 06-15-2010, 06:22 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
This would have to be covered in the sed one-liners.
Instead of not printing try deleing the line in question
Code:
sed "${variable}d" filename
 
Old 06-15-2010, 06:26 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Also, if you are using a variable, the SED command string needs to be in double-quotes.
 
Old 06-15-2010, 07:01 AM   #4
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Thanks for your input . But now i would like to enter a line at specific line number .

How would i establish this using sed.
 
Old 06-15-2010, 07:06 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,244

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Have you met the man command or perhaps some of its friends on the web called search engines??

Sorry to be harsh but you have been given a solution which should guide you to what you require just that different options, ie not 'd' for delete,
would need to be used.

Remember that the community exists to educate, not to do the work for you.
 
Old 06-15-2010, 07:07 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Well, now that you've solved the issue about specifying a variable address (using double quotes), you can test with other sed commands as well. For example:
Code:
sed "${variable}a newline to append" filename
sed "${variable}i newline to insert" filename
This and many other sed tricks explained here: http://www.grymoire.com/Unix/Sed.html (a must-read)!
 
Old 06-15-2010, 09:22 AM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Hi,

Another good reference would be 'Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook'.


The above link and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
 
Old 06-15-2010, 11:47 AM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Code:
sed -n "$var1,$var2"p
 
  


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
Create File Listing in C++ that will generate a line number on every line of code marzrocks Programming 11 04-12-2010 07:10 AM
Editing a specific group of lines by line number? SirTristan Linux - Newbie 5 09-29-2009 08:30 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 01:29 PM
how to print a specific line number with cat new_2_unix Linux - Newbie 2 10-14-2007 11:47 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM


All times are GMT -5. The time now is 01:57 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration