ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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'm writing a script in BASH to clean up some text files, wich went well until this problem: I must remove all lines wich start with "VAKVM" and DON'T have one of the words "RAS","PROD" or "DRC". so imagine this file:
Code:
Hello
Everybody
VAKVM hello
VAKVM there is RAS in this sentence
VAKVM this one stays not
VAKVM PROD=15, DRC=14
PROD without VAKVM at start
And so on
Next
VAKVM please remove
VAKVM thanx PROD
must become this:
Code:
Hello
Everybody
VAKVM there is RAS in this sentence
VAKVM PROD=15, DRC=14
PROD without VAKVM at start
And so on
Next
VAKVM thanx PROD
is the 'sed' command good for this? if so.... how do I do this?
thanx in advance,
.-=~ iluvatar ~=-.
edit: to make it even more clear: I cannot check for RAS first and then DRC 'cause while checking for RAS, a line with DRC could be deleted already... if someone can help me out on this? btw: I wrote the script in BASH but if someone gives me a simple solution in C / C++ or something that's ok too...
wich blanks the lines with VAKVM at the start and no RAS in the rest of the line, but how can I make sed check for more words in the line?
like this way it doesn't work:
sed '/{RAS|DRC}/!s/VAKVM.*$//g' test_file
anyone has an idea? the blank lines can simply be removed with another sed-command so I'm happy enough if this is gonna work...
You could produce a sed script which consists of two lines. One line for a RAS line and one line for a DRC line. Then call sed like:
sed -f RasDrc.sed test_file
SED reads a line of the input file and applies each line of the script to that line before reading the next line. A sed script step which causes the input line to be deleted, causes SED to start on a new input line.
This would make future modifications easier also. The bash script wouldn't need to change, just the SED script.
this blanks all lines starting with VAKVM except the lines wich have both RAS and DRC... I need the lines with just RAS or DRC too... I guess sed now does this: the first script line looks for RAS, finds it and sed doesn't execute anything, but the second script line will blank the line anyway because DRC is not found...
ok now I'm going crazy I use this test file now wich has every possible situation:
Code:
dit is regel 1
regel 2 RAS
VAKVM hallo
VAKVM RAS nr is
VAKVM dit is met RAS
VAKVM DRC
VAKVM dit is RAS en DRC
en dit is een losse regel
(yes that's dutch)
Now I tried this sed command:
sed -e '/VAKVM.*[RAS|DRC].*$/,/^!VAKVM/!d' test_file
And I got this output:
Code:
VAKVM RAS nr is
VAKVM dit is met RAS
VAKVM DRC
VAKVM dit is RAS en DRC
en dit is een losse regel
as we can see here, the VAKVM line with 'hello' is deleted, wich is correct the last line is kept too because it doesn't state VAKVM at start, but what happened to the first two lines???
I figured it's always good to post the solutions... I came up with another awk command even more complex. because the file looks actually like this:
Code:
bla
...
bla
VAKVM 31 1 blabla
VAKVM 31 2 blabla and RAS
VAKVM 31 3 blabla
VAKVM 44 1 blabla
VAKVM 44 2 blabla and DRC
VAKVM 44 3 etc.
VAKVM 44 4 last one DRC and RAS
blah
...
blah
as you can see the VAKVM lines are numbered (third column) an those need to be re-numbered after some lines are deleted, therefor, I came up with this:
LOL it's unbelievable to see how many solutions there are in 2 days for a problem you knew nothing about 2 days ago... I don't have any knowledge about perl but if awk can do the line numbering in a one-liner I bet perl can do it too...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.