delete multiple lines by line number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers delete multiple lines by line number
# 1  
Old 10-24-2008
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 tmp_memrecno.dat that has numbers:
5
3

I want to delete lines (read from tmp_memrecno.dat) from file tmp.out.

Here is what I have so far, but doesn't really work:

while read line
do
sed "$line"'d' test.out > tmp.out
done < tmp_memrecno.dat

Basically, the end result of file test.out should be
sadfsdf sdfosuidhfousdhof soduhf osdfu osudfhosudhfd
sdfgsdfg
ouahsdjdafkljsa
# 2  
Old 10-24-2008
one way -
Code:
awk ' FILENAME=="tmp_memrecno.dat" { arr[$0]=1}
        FILENAME=="tmp.out" {
              if( FNR in arr){ continue}
              print $0
        } ' tmp_memrecno.dat  tmp.out > tmp.newfile


Last edited by jim mcnamara; 10-24-2008 at 03:44 PM..
# 3  
Old 10-24-2008
Quote:
Originally Posted by jim mcnamara
one way -
Code:
awk ' FILENAME=="tmp_memrecno.dat" { arr[$0]=1}
        FILENAME=="tmp.out" {
              if( FNR in arr){ continue}
              print $0
        }  tmp_memrecno.dat  tmp.out > tmp.newfile

This prints an error:

Syntax Error The source line is 5.
The error context is
} >>> tmp_memrecno. <<< dat tmp.out > tmp.newfile
awk: 0602-500 Quitting The source line is 5.
# 4  
Old 10-24-2008
See the edit above
# 5  
Old 10-24-2008
Thanks dude... you are the best.

- CB
# 6  
Old 10-24-2008
How would you use unix variables (tmp1=tmp_memrecno.dat and tmp2=tmp.out) in awk; for example $tmp1 and $tmp2.
# 7  
Old 10-24-2008
Code:
awk -v tmp1="$tmp1" -v tmp2="$tmp2" ' {  print tmp1, tmp2} '  somefile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
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. 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(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... (2 Replies)
Discussion started by: learninguser235
2 Replies

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

7. 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

8. 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

9. 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

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