How to delete lines above a certin line number in bash shell


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to delete lines above a certin line number in bash shell
# 1  
Old 05-10-2012
How to delete lines above a certin line number in bash shell

Hi,

I have written a script that returns the line number of the pattern i want and i stored the line number in a variable(getlinenumber).Now i want to delete all the lines in a file above this line number which is stored in a variable.

i am using sed '1,$getlinenumberd' > file1.txt which is not working(wrog syntax), but if its not stored in a variable it works like sed '1,100d' > file1.txt.

can some one tell me how to delete from 1 to n where n is the vaue stored in a variable.

Thanks.
# 2  
Old 05-10-2012
sed '1,$getlinenumberd' There are 2 problems with this, you haven't defined getlinenumberd and the shell var is never interpolated. Try
sed "1,${getlinenumber}d"
# 3  
Old 05-10-2012
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

2. Shell Programming and Scripting

Bash Command To Delete Number from Array

Hi, I am writing a script to split a log file - the log could contain multiple days worth of logs. The second line of the log contains the string "Version ". In my test log which comprises of two days worth of logs, this string appears twice - once each day. Essentially I would like to split... (7 Replies)
Discussion started by: pmurray21
7 Replies

3. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable.Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is not working(wrog... (5 Replies)
Discussion started by: learninguser235
5 Replies

4. Shell Programming and Scripting

Sed delete certain number of empty lines

Hi What I'm trying to do is delete every blank line upto a certain number, so for instance I only want to delete the first 4 empty lines within a file, I know how to delete every line with: sed '/^$/d' But I can't figure out how to limit it to only the first 4 occourances I though it... (5 Replies)
Discussion started by: duonut
5 Replies

5. Shell Programming and Scripting

How to delete several lines from file by line number?

Hi I am using the following command to delete a line from the file by line number: line_number=14 sed "${line_number}d" inputfilename > newfilename Is there a way to modify this command to specify the range of lines to be deleted, lets say from line 14 till line 5 ? I tried using the... (5 Replies)
Discussion started by: aoussenko
5 Replies

6. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

7. Shell Programming and Scripting

delete multiple lines by line number

I have file with 10000 records and i need to delete the lines in single shot based on line number range say from 10 to 51 , 53 to 59 , 105 to 107, 311 to 592 etc... between range works fine for me but how to achive for above case? please help sed '10,51 {d}' infile > outfile (5 Replies)
Discussion started by: zooby
5 Replies

8. Shell Programming and Scripting

delete first number of even-numbered lines

Hello ! I am trying to delete a number from the even-numbered lines of a pipeline after having extracted and sorted the desired data from an original text file using sed... sed -r 's/\(*\)*\((*)\),*,*,(*)*: * \w*\=(*).*/\3,\1/g' | sort -n then the data looks like : Now, I am... (3 Replies)
Discussion started by: ShellBeginner
3 Replies

9. UNIX for Dummies Questions & Answers

delete multiple lines by line number

I have been googling, but cannot find that works for me. I have a text file tmp.out with contents: sadfsdf sdfosuidhfousdhof soduhf osdfu osudfhosudhfd sdfgsdfg asdfiojhsdf asdoludhflsdjfhskldjfhsdjdlfsjdhnlj h sdja ouahsdjdafkljsa oljhljh I have another file... (11 Replies)
Discussion started by: ChicagoBlues
11 Replies

10. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question