Web Hosting Talk







View Full Version : unix - remove/delete row from datafile


hoachen
06-15-2005, 10:35 PM
does anybody know how to remove/delete a row from a datafile?
EXAMPLE
1:abc:lahlah
6:dec:blah blah
3:fig:hahaha

let say i want to remove row #3 which is 3:fig:hahaha

how do i do that to delete line #3? I tried to use rm but unsuccessful. Any idea or advise?

Pleae help :bawling: :bawling:

Tim Greer
06-15-2005, 10:59 PM
rm is to remove a file or symbolic link, not a specific entry or content within a file. You can use grep, sed, awk, etc. and shell scripts, or you can do something more logical and use a programming language such as Perl, PHP, etc. to open the file, parse it and update it.

hoachen
06-15-2005, 11:04 PM
grep, sed, awk will do that? How? any reference?

This is Unix assignment I can't use other languages, that's the problem!


Originally posted by Tim_Greer
rm is to remove a file or symbolic link, not a specific entry or content within a file. You can use grep, sed, awk, etc. and shell scripts, or you can do something more logical and use a programming language such as Perl, PHP, etc. to open the file, parse it and update it.

Tim Greer
06-15-2005, 11:10 PM
How are you searching the file or parsing it to determine what line to remove? Are you doing it by line number, or by the word, full text/entry, or? What languages/tools are you allowed to use? Finally, if this is an assignment, why are you here asking us to do your work for you? If you're looking for help, why don't you post what you have? If you're looking for a free ride and aren't going to do the work yourself, drop the class and don't waste our time--no offense.

hoachen
06-15-2005, 11:17 PM
it is very mean of you! If I can find the answer I already did it by myself and why I would ask for somebody help. Don't you think it is wasted time? I did not ask for do my homework I just asking how to use and what command i should use for this particular task.


Originally posted by Tim_Greer
How are you searching the file or parsing it to determine what line to remove? Are you doing it by line number, or by the word, full text/entry, or? What languages/tools are you allowed to use? Finally, if this is an assignment, why are you here asking us to do your work for you? If you're looking for help, why don't you post what you have? If you're looking for a free ride and aren't going to do the work yourself, drop the class and don't waste our time--no offense.

innova
06-16-2005, 01:58 AM
You can use grep, sed, awk, etc. and shell scripts, or you can do something more logical and use a programming language such as Perl, PHP, etc. to open the file, parse it and update it.

It is not more logical to use a full scripting language versus command line tools - remember, this is the stuff they are MADE for.

Here is probably the most basic, boring script I can think of... keep in mind, this does no checking whatsoever and is a crude example:


grep -v 'content of line to exclude' FILENAME > NEWFILENAME

Obviously, that will not remove line 3, it will remove the content of line three.

Read the 'man' pages of those apps you mentioned for further inspiration.

Tim Greer
06-16-2005, 02:36 AM
Originally posted by hoachen
it is very mean of you! If I can find the answer I already did it by myself and why I would ask for somebody help. Don't you think it is wasted time? I did not ask for do my homework I just asking how to use and what command i should use for this particular task.

You didn't post any code or any information about what you've tried. You just said you want someone to tell you how to do it, and it's an assignment. I'd assume that you were educated on how and you should therefore apply the knowledge they gave you.

Also, I said no offense and I didn't accuse you of doing that, I just said if you are, don't waste people's time as it wouldn't be right to do that. I gave you commands, and it depends greatly on how you are going to have it work (or are supposed to have it work) to give you any answer anyway. But, then I've just done your work for you, and you don't want that. Without further information, I am not able to guide you anyway.

Tim Greer
06-16-2005, 02:39 AM
Originally posted by innova
It is not more logical to use a full scripting language versus command line tools - remember, this is the stuff they are MADE for.


Well, that's what I said myself, as you can see--use grep, etc. As for more logical, I thought it would be, because he didn't specify what he was needing to do or how and didn't say, at that time, he had to use specific tools.

With a one liner with the same amount of typing in Perl on the command line, it could do more, more easily (usually). But, sure, if what you needed to do is simple and you can use grep, there you go.


Here is probably the most basic, boring script I can think of... keep in mind, this does no checking whatsoever and is a crude example:


grep -v 'content of line to exclude' FILENAME > NEWFILENAME


That indeed does work, but now that data is in a new file and still remains in the old one. As I said, I'd need more information to offer any suggestion, but that's a good start.


Obviously, that will not remove line 3, it will remove the content of line three.


Yep.


Read the 'man' pages of those apps you mentioned for further inspiration.

I'm not sure what you mean, but I assume you are speaking to me, since I "mentioned those apps". As I'm well aware of the functions and how they work, this isn't required of me. If you meant the poster, indeed, they should be making use of the man pages I was offering you help, remember? I just said I didn't want to give him the easy way out--if that was indeed what he were looking for--as it wouldn't do him any good and would be dishonest. Good, you've found one part of the solution by the looks of it.

hoachen
06-16-2005, 07:09 AM
Thank you guys. That's what I need, I just want to know which way can be work. I have tried the "grep" before but unsucessful. Now you guys said that's away to do it then I will try.

P/s if we post a question, should we post all the method that we have tried? I saw majority of discussion did not post what they do so I assume it is not necessary to do that. But anyway thank you very much guys

Tim Greer
06-16-2005, 02:32 PM
Unless you want to ask someone to write it all out for you, you should indeed post what you've done/have now and what you've tried, and perhaps the results. There's a lot of ways to do this, truly. Again, you said the 3rd line should be removed, but you didn't say if it's to be removed by the value, if it'll always be the 3rd line, etc. Perhaps it won't always be the 3rd line? Perhaps it'll always just be a certain word that it should find and ignore that entire line for, just for an example. It makes it difficult to suggest an answer.

hoachen
06-16-2005, 05:29 PM
o.k it is my fault i did write it clear say i want to delete entire line when the command-line input that match the id number from the datafile. But again, I did not said I need somebody to write the entire function for me, I just asking what should i do and have tried the rm function.

Alright, alright, never mind, programming discussion misunderstanding and debate.

anyway, thank you for your lesson and advise. Have a good one.

Originally posted by Tim_Greer
Unless you want to ask someone to write it all out for you, you should indeed post what you've done/have now and what you've tried, and perhaps the results. There's a lot of ways to do this, truly. Again, you said the 3rd line should be removed, but you didn't say if it's to be removed by the value, if it'll always be the 3rd line, etc. Perhaps it won't always be the 3rd line? Perhaps it'll always just be a certain word that it should find and ignore that entire line for, just for an example. It makes it difficult to suggest an answer.

sonic10
06-16-2005, 06:45 PM
Well if you wanted to delete the 346th record or line.

grep -v 346 old_file > new_file

or maybe sed

sed '346' file > newfile

just be sure to backup your data first.....

Tim Greer
06-16-2005, 06:56 PM
Well, since that number can appear anywhere in a line of data, you should probably grep -v ^number: as in ^start of string, number, and a colon, to get an accurate match against the line that starts with that number to be ignored. Of course, there's still several other things about the way it should work that I'm not clear on, but I'm sure you'll figure it out.

hoachen
06-16-2005, 08:20 PM
Hey, good new I got it. Thanks for the hard thinking. Cheer!


Originally posted by Tim_Greer
Well, since that number can appear anywhere in a line of data, you should probably grep -v ^number: as in ^start of string, number, and a colon, to get an accurate match against the line that starts with that number to be ignored. Of course, there's still several other things about the way it should work that I'm not clear on, but I'm sure you'll figure it out.