How would i delete a line at specific line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How would i delete a line at specific line number
# 1  
Old 06-15-2010
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

# 2  
Old 06-15-2010
use double quotes instead when using shell variables.
This User Gave Thanks to clx For This Post:
# 3  
Old 06-15-2010
Quote:
Originally Posted by anchal_khare
use double quotes instead when using shell variables.
+
i tried
Code:
sed -n "$variable!p" vm.cfg

and

Code:
sed -n '"$variable"!p' vm.cfg

None of them are working.
Please help.
# 4  
Old 06-15-2010
Code:
[house@leonov] cat in.file
1st line
2nd line
3rd line
[house@leonov] var=2; sed "${var}d" in.file
1st line
3rd line

This User Gave Thanks to dr.house For This Post:
# 5  
Old 06-15-2010
Another way:
Code:
sed ''${variable}'d' vm.cfg
    ^^
This is two single quotes, not a double!

This User Gave Thanks to zaxxon For This Post:
# 6  
Old 06-15-2010
You don't really need quotes at all.

Code:
$ cat file1
a
b
c
d

$ X=3
$ sed ${X}d file1
a
b
d

This User Gave Thanks to Scott For This Post:
# 7  
Old 06-15-2010
Thanks for your input . But now i would like to enter a line at specific line number .

How would i establish this using sed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete a specific line containing non consecutive number?

Dear Specialists, I have following data 1 1 2 2 2 3 3 3 6 4 3 4 5 4 9 6 5 11 7 6 7 and I would like to obtain data like below 1 1 2 2 2 3 4 3 4 7 6 7 (2 Replies)
Discussion started by: Ryan Kim
2 Replies

2. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

3. Shell Programming and Scripting

Cut from specific line number to a line number

Hi All, I've a file like this.. Sheet1 a,1 a,2 a,3 a,4 a,5 Sheet2 a,6 a,7 a,8 a,9 a,10 Sheet3 a,11 a,12 a,13 (7 Replies)
Discussion started by: manab86
7 Replies

4. Shell Programming and Scripting

Delete specific line in awk

Hi, I'm totally new to awk, so thanks in advance for your help. I want to make a csv file out of a text file I've exported from a database I want to do two things: 1. Change pipes ("|") to commas (",") to make a text file into CSV 2. Remove the second line of the file, which is useless junk. ... (3 Replies)
Discussion started by: pip56789
3 Replies

5. Shell Programming and Scripting

how to delete the line by line number

Hi guys, is there a way to delete a line from a text file by providing a line number for the line I want to remove? I work in ksh88... Thanks a lot (3 Replies)
Discussion started by: aoussenko
3 Replies

6. Shell Programming and Scripting

Delete all lines after a specific line ?

Hello. My file is like this: a b c d e f g h i I want to delete all lines after the 3rd line, means after the "c". Is there any way to do this? The lines differ between them and the lines I want to delete does not have a specific word, or the lines I want to keep (a,b,c) does not have a... (4 Replies)
Discussion started by: hakermania
4 Replies

7. Shell Programming and Scripting

Find specific line and delete line after.

I'm looking for a way to search a file, in this case init.rc for a specific match, service adbd /sbin/adbd, and delete the a specific line after it Original: # adbd is controlled by the persist.service.adb.enable system property service adbd /sbin/adbd disabled After deletion: #... (5 Replies)
Discussion started by: GabrialDestruir
5 Replies

8. Shell Programming and Scripting

Delete specific line containing null after '='

Hi, I'm genrating a file from sql and the file looks like : $value1=A $value2=B $value3= $value4= $value5=C I want to delete those two lines which has Null after '='. Could you please guide me how to do it either using sed or awk ? (9 Replies)
Discussion started by: santanu83_cse
9 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies
Login or Register to Ask a Question