LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 01-22-2015, 07:10 AM   #1
Sushukka
LQ Newbie
 
Registered: Jan 2015
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 01-22-2015, 07:42 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Well your script successfully selects required lines, then what with sed?
 
Old 01-22-2015, 08:05 AM   #3
J Martin Rushton
Member
 
Registered: Jan 2015
Location: England
Distribution: Mainly CentOS
Posts: 31

Rep: Reputation: Disabled
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.

All other lines are output unchanged.
 
Old 01-22-2015, 11:05 AM   #4
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
Quote:
Originally Posted by Sushukka View Post
SnameNN, SnameYY and SnameZZ need to be deleted .. Sname lines have also "Dedicated" written in them
use egrep?

Code:
egrep -v "SnameNN|SnameYY|SnameZZ" file | grep "Dedicated"
output
Code:
"number|Dedicated|SnameXX|number|number|number"
"number|Dedicated|SnameXX|number|number|number"

Last edited by Miati; 01-22-2015 at 11:09 AM.
 
Old 01-22-2015, 11:59 AM   #5
Slax-Dude
Member
 
Registered: Mar 2006
Location: Valadares, V.N.Gaia, Portugal
Distribution: Slackware
Posts: 528

Rep: Reputation: 272Reputation: 272Reputation: 272
This should work:
Code:
cat yourfilename | sed '/Sname[NN|YY|ZZ]/d'
If you are happy with the output, then this will mod the file directly:
Code:
sed -i '/Sname[NN|YY|ZZ]/d' yourfilename
 
Old 01-22-2015, 01:36 PM   #6
Sushukka
LQ Newbie
 
Registered: Jan 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
More details

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

Last edited by Sushukka; 01-22-2015 at 02:44 PM.
 
Old 01-23-2015, 04:17 AM   #7
J Martin Rushton
Member
 
Registered: Jan 2015
Location: England
Distribution: Mainly CentOS
Posts: 31

Rep: Reputation: Disabled
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.

I used the following data file:
Code:
qwertDedicatedtyuinameghjk
qwertDedicatedtyui
sdfghj
and put it through the following script:
Code:
while read line;
do
    case $line in
        *Dedicated*name*)       echo $line ;;
        *Dedicated*)            ;;
        *)                      echo $line ;;
    esac
done
The first and third lines were printed as expected.
 
1 members found this post helpful.
Old 01-23-2015, 04:27 AM   #8
Sushukka
LQ Newbie
 
Registered: Jan 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-23-2015, 08:29 AM   #9
Slax-Dude
Member
 
Registered: Mar 2006
Location: Valadares, V.N.Gaia, Portugal
Distribution: Slackware
Posts: 528

Rep: Reputation: 272Reputation: 272Reputation: 272
This should delete all lines that contain "Dedicated" except those that also contain "name":
Code:
cat yourfilename | sed -e '/Dedicated/{/name/!d;}'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C Program Trouble Removing Multiple New Lines CincinnatiKid Programming 6 01-20-2014 08:50 AM
Copy lines starting and ending with specific pattern from multiple files to a file ssn Linux - Newbie 2 07-27-2011 10:44 AM
Create a file from two files with specific search conditions.(a bit Difficult) vysakh@gmail.com Linux - Server 4 05-06-2010 09:18 AM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 05:47 AM
Removing lines from file Aylar Programming 2 04-22-2004 06:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration