Thread: How to delete many strings?

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    44

    How to delete many strings?

    Is there a way to search a text file and delete every line that for example begins with the word Repo or the number 9?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. However, instead of doing this in C, you might want to do this using sed if it is available.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Hi @laserlight,

    SED is great but in my program, my very first call is (system) and I don’t want to call system again unless it’s extremely urgent per application.

  4. #4
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Other then removing a line by its number, is what I asking that difficult for C?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's not that difficult to do in C.

    It's just that it's a hell of a lot easier to do in sed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Since my undertaking with C nearly every site I been about this subject a person would suggest using (system), but somewhere in that same threads somebody would provide a warning of why not to use shell-calls in C or why not to use too many.

    I figure, why I should be the challenger. That is why, I prefer to see how it’s done in C. If it eats up more resources then needed, or end up being a large code-set to handle, I don’t care! I will still be learning something that I will eventually need to learn how to do, anyway. Thank you for you reply @Salem

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Step 1. learn how to open the file and read it line by line
    Step 2. use string.h functions to edit the line.
    Step 3. Output the edited line to new output file
    Step 4. Delete input file and rename output file to new name.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jc2020
    Since my undertaking with C nearly every site I been about this subject a person would suggest using (system)
    I didn't suggest using system; I suggested using sed. There is no need to write a C program that calls system to invoke sed. Just invoke sed. If you do have other things to do that would be appropriate for a C program, either pipe output from sed as input into the C program, or vice versa. This is the basic Unix approach to having utilities that do one thing and do it well such that they can be used together to accomplish complex tasks.
    Last edited by laserlight; 07-05-2019 at 09:31 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Thanks @stahta01. Your breakdown prove it IS a lot of stress, especially for tons of unneeded strings inside a bunch of large files. It’s going to be interesting to see what kind of impact its has on system memory. That’s all that counts other then speed. It makes since so it’s going to get in where it fit in. I’ll post my hardware result right here. I waste more time comparing everything I can keep up with since day1 with my OS, and now, C Thanks again @stahta01
    Last edited by jc2020; 07-06-2019 at 12:06 AM.

  10. #10
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    I didn't suggest using system; I suggested using sed. There is no need to write a C program that calls system to invoke sed. Just invoke sed.
    @Laserlight, I apologize for my misinterpretation of your reply. Actually, I know nothing above using (system) in C. Can I say: May the sky cave in if one had no faith in your conclusions, codes and dedication to this community. I might be new but I was lucky to find and follow you for months though I understood nothing much.

    If you do have other things to do that would be appropriate for a C program, either pipe output from sed as input into the C program, or vice versa.
    I wish I knew how to pipe sed to a C program because my intent IS to complete this application in one C file. The best google turn up is one or the other but nothing about how to use both together. Makes me think of the future of AI misfires. Maybe someone will show a sample of how that is done. I'm going to try bing.com.

    Have a wonderful tomorrow++ Ms. @Laserlight
    Last edited by jc2020; 07-06-2019 at 01:52 AM.

  11. #11
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    I figured out a way where I can delete by line-numbers. All it took was a little bit more planning. There are tons of examples for that on the internet and here. My most important lesson about C is if you cannot find what you thinking of on the internet in C it probably can’t be done without massive coding if you want to keep everything in one C file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-07-2017, 01:10 PM
  2. Replies: 2
    Last Post: 04-13-2017, 01:29 AM
  3. List - Why my delete function doesn't delete?
    By juanjuanjuan in forum C Programming
    Replies: 7
    Last Post: 12-09-2014, 10:10 PM
  4. Concerning delete/delete[] at program exit
    By laserlight in forum C++ Programming
    Replies: 58
    Last Post: 01-09-2008, 01:40 PM
  5. Replies: 17
    Last Post: 11-16-2006, 09:06 PM

Tags for this Thread