Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to remove a multi-line text (contained in a file) from another text file but only the first occurrence
The text file looks like this:
Code:
the
quick
the
brown
quick
brown
fox
jumps
quick
brown
fox
And I need to remove the following lines (contained in a file)
Code:
quick
brown
fox
And then the result will be:
Code:
the
quick
the
brown
jumps
quick
brown
fox
Sounds like an interesting homework question; what have you done/tried so far to get this done?? What language, shell, or utility do you want or need to use?? Read the "Question Guidelines" link in my posting signature...we're happy to help with things, but we aren't going to do homework for you.
I'd suggest looking at sed, and incorporating it into a simple bash script to loop through your input file. And based on your input file and what it takes in, the output SHOULD be:
Code:
the
the
quick
brown
jumps
quick
brown
fox
...since you'd remove the first occurrence of each word.
::EDIT::
Quote:
Originally Posted by joejobs
Somehow I messed the name of this post and I can't change it, is it possible to delete it please?
Again, read the "Question Guidelines" and LQ Rules...don't delete or edit posts like that, since it only makes things hard to follow when others search for things. You can always ask a moderator to edit a subject line, but deleting the whole thing for essentially no reason isn't good.
I reposted the question, sorry for trying to delete it and reposting again
I need to remove the entire multi-line text, I do not need to remove the individual lines of that multi-line text
I am using gnuwin32 on Windows.
I managed to find a very complicated solution:
- transform the newlines into a special character "@"
- read the content of the first file into a variable
- use sed to remove the line from the second file
- transform again the special character "@" into newline
However this is quite a mess for very large files that can contain the "@" character
I reposted the question, sorry for trying to delete it and reposting again I need to remove the entire multi-line text, I do not need to remove the individual lines of that multi-line text
I am using gnuwin32 on Windows.
I managed to find a very complicated solution:
- transform the newlines into a special character "@"
- read the content of the first file into a variable
- use sed to remove the line from the second file
- transform again the special character "@" into newline
However this is quite a mess for very large files that can contain the "@" character
Bolded a part above for emphasis only. Understand what you're asking now, and thanks for showing your efforts. Sed may be what you're looking for:
Code:
sed '/start word/{:a;N;/end word/!ba}/d'
Replace start/end words as needed, and this is untested. And use caution, since the Windows utilities aren't 100% compatible with their 'real' Linux counterparts. Suggest using Linux in a VM on your Windows system if you really want to test things in a more stable way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.