[SOLVED] Removing specific lines from a file (multiple conditions)
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Removing specific lines from a file (multiple conditions)
Here is the problem. I have automatically generated file with looks something like this:
------------------------------------------------------
// Comment
// more comments
// blabla
javascript code
.
.
.
"number|Dedicated|SnameXX|number|number|number"
"number|Dedicated|SnameXX|number|number|number"
"number|Dedicated|SnameNN|number|number|number"
"number|Dedicated|SnameYY|number|number|number"
"number|Dedicated|SnameZZ|number|number|number"
...the list continues...
...more code...
.
.
.
);
------------------------------------------------------
What I need to do is to remove all other Sname lines than SnameXX. So in above example I need to spare all the other lines in the file but SnameNN, SnameYY and SnameZZ need to be deleted. All these Sname lines have also "Dedicated" written in them (other fields cannot be used for grouping). With grep I can isolate the "bad" lines with: grep -i -v SnameXX file.js | grep Dedicated. From here I'm stucked. What I have understood, having "inverse" selection in sed requires some strange tricks. In any normal programming language, you would need one loop running through the file with two if statements. However, I would like to believe that there are some neat solution to solve this with only command line tools. I'm playing with RHEL, but this level of fiddling probably doesn't have any os dependencies.
AWK is standard on (AFAIK) all *nixes, so consider:
Code:
awk '
/|Dedicated|SnameXX|/ { print ; next }
/|Dedicated|Sname/ { next }
{ print }
'
The first test looks for Dedicated|SnameXX lines which you want and therefore they are printed to standard output and processing starts with the next line.
The next line looks for a SnameYY or SnameZZ line and if found just goes on to the next line.
Thanks for the quick answers! However, the tricky part (which I didn't manage to mention well enough) is that there is no pattern with SnameYY, SnameZZ or SnameNN. Only pattern is found in SnameXX which need to be saved. The list could look like this:
Code:
"number|Dedicated|prodName1|number|number|number" << save
"number|Dedicated|testName3|number|number|number" << save
"number|Dedicated|devName45|number|number|number" << save
"number|Dedicated|someblatext|number|number|number"
"number|Dedicated|ipsumlorem|number|number|number"
"number|Dedicated|x1vr23d|number|number|number"
So basically in pseudo code:
Code:
do until end of the file
if substr(line) = "Dedicated" then
if substr(line) != "name" then
do nothing
else
line > output
end
Your pseudo code is missing an else. If the line does contain "Dedicated" and does contain "name", then what should be done? You appear to be dealing with 3 forms of line:
*Dedicated*name* print
*Dedicated* ignore
* print
If this is correct, then a simple case statement in BASH is probably the easiest solution. Most importantly though, a clear and precise statement of the problem would help.
Yep, you're right. I just forgot the else statement from the pseudo code. Late evening here, long day, blabla, my mistake.
But again, that's the solution. Many thanks! Wasn't sure about the syntax (hence here in general noob area). Better to have a simple couple of lines of code than cryptic parameter jungle with one or many ready made utilities.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.