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
# 8  
Old 06-15-2010
Like this

Code:
# cat file
12
13
14
15

Code:
# sed '/12/d' file
13
14
15

# 9  
Old 06-15-2010
Like this?
Code:
sed '4s/.*/this is the new line\n&/' infile
one
two
three
this is the new line
four
five


Last edited by zaxxon; 06-15-2010 at 08:09 AM.. Reason: misunderstood op at first
# 10  
Old 06-15-2010
Quote:
Originally Posted by pinga123
Thanks for your input . But now i would like to enter a line at specific line number .

How would i establish this using sed.
Does it have to be sed?
Code:
# cat in
1
2
3
4

Code:
# export n=3
# export s="aaa"
# awk -vn=$n -vs=$s 'NR==n{print s}1' in
1
2
aaa
3
4

# 11  
Old 06-15-2010
Quote:
Originally Posted by pinga123
Thanks for your input . But now i would like to enter a line at specific line number .

How would i establish this using sed.
What do you mean by "enter a line"? Insert a new line, or?

Code:
$ cat file1
a
b
c
d

$ sed '3 i\
new line goes here' file1
a
b
new line goes here
c
d

# or
$ sed '3 a\
new line goes here' file1
a
b
c
new line goes here
d

# 12  
Old 06-15-2010
insert variable $line at line 3:
Code:
sed "3s/^/$line\n/" infile

# 13  
Old 06-15-2010
Hi ,

Code:
#!/usr/bin/perl

while (<>) {
if ($. == $linenum ) { print "test line \n"; }
print $_;
}

OR

Code:
#!/bin/sh

linecount=`cat test1.txt | wc -l`
linetoinsert="test line to insert"
linenumber=4

linecount_head=`expr $linenumber - 1`
linecount_tail=`expr $linecount - $linenumber + 1`

head -$linecount_head test1.txt;echo $linetoinsert;tail -$linecount_tail test1.txt

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