Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete a list of line numbers 1

Status
Not open for further replies.

latebeat

IS-IT--Management
May 18, 2010
4
US
Hello to everyone,
I'm pleased to say this is my first post on this forum :)

Guys, I'm trying to find a quick way to delete certain lines numbers from a text file. This text file is generated daily, has about 5000 lines every time and is then processed by another application.
However some lines are malformed and I get an error message from the aforementioned application telling me the line numbers that are erroneous.
So far I have been searching/deleting these lines manually but it's a pain in the butt to do this for 40-80 every time plus I have to do this at 5am in the morning so I really want to do it as fast as possible so I can get back to sleep!
So my question is how can I delete these particular line numbers with awk?

My idea is to create a new text file with the problematic line numbers like:

7
450
560
1002
3004
4000

and then pass this new file to awk to remove them from the other text file.

So far I've found this post here:
but it only works for 2 numbers. Any ideas how I can modify it to work with a random number of lines every time?

thanks,
Anestis
 
awk 'NR==FNR{n[$1];next}!(FNR in n)' linenumbers input > output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
this is really weird... I tried it again today and it's not working? How's that possible? Does it have any limitations like a fixed number of input line-numbers?

I've uploaded here the two text files
hxxp://
One is the lines.txt and the other is the input text file (~4000lines). When I pass the exact same argument as above the output is giving me the original file! No lines are removed whatsoever!

Any help is *greatly* appreciated
 
Worked fine for me using those input files.

What OS are you doing this under? Those input files are in DOS format with CR/LF line terminators, which may be confusing your implementation of awk if it's on Unix. Try using dos2unix on the files first... or tr -d '\r'.

Annihilannic.
 
That was it! I created the linenumbers file under windows and then tried to use it under linux. It never crossed my mind to change the line ending :)

thanks again,

you are a life saver!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top