LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How would i delete a line at specific line number (https://www.linuxquestions.org/questions/linux-newbie-8/how-would-i-delete-a-line-at-specific-line-number-814225/)

pinga123 06-15-2010 05:10 AM

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

syg00 06-15-2010 05:22 AM

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

pixellany 06-15-2010 05:26 AM

Also, if you are using a variable, the SED command string needs to be in double-quotes.

pinga123 06-15-2010 06:01 AM

Thanks for your input . But now i would like to enter a line at specific line number .

How would i establish this using sed.

grail 06-15-2010 06:06 AM

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.

colucix 06-15-2010 06:07 AM

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)! :)

onebuck 06-15-2010 08:22 AM

Hi,

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

:hattip:
The above link and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!

schneidz 06-15-2010 10:47 AM

Code:

sed -n "$var1,$var2"p


All times are GMT -5. The time now is 11:43 AM.