LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2021, 07:55 AM   #16
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,841

Rep: Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649

[QUOTE=shruggy;6297960]Nice. This could be made a one-liner, but then it would be less readable:
Code:
awk 'NR==FNR{D[$0]=1}NR!=FNR&&!D[FNR]' "$DFile" "$InFile"
Wow! This solution wins the prize for fewest keystrokes! Excellent!

Are the double-quotes around $DFile and $InFile needed?

Daniel B. Martin

.
 
Old 11-03-2021, 08:02 AM   #17
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,037

Rep: Reputation: Disabled
The OP had lines.tmp as $DFile and out.file as $InFile. No white space in the names, so in this particular case they're not needed. But I prefer to always quote file names, just in case.

Last edited by shruggy; 11-03-2021 at 02:12 PM.
 
1 members found this post helpful.
Old 11-03-2021, 08:05 AM   #18
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,059

Rep: Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671
Hmmm - personally I preferred a solution similar to yours (danielbmartin), although I avoid else mostly.
Shooting for "least keystrokes" leads easily down the unreadable route that perl has been pilloried about forever.
 
1 members found this post helpful.
Old 11-03-2021, 01:58 PM   #19
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,841

Rep: Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649
Quote:
Originally Posted by syg00 View Post
... I avoid else mostly. ...
Please elaborate. Is this aversion based on ...
- execution efficiency
- readability
- personal coding style
- something else?

Daniel B. Martin

.
 
Old 11-03-2021, 03:03 PM   #20
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,204

Rep: Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824
Quote:
Originally Posted by danielbmartin View Post
Please elaborate. Is this aversion based on ...
From my side at least half of the cases that else is just misused, superfluous.
With a more "scientific" approach: the usage of else increases the complexity of the given code, in most cases there is a better/simpler way - with lower complexity.

But obviously there are cases when the usage of else is really reasonable.
 
Old 11-04-2021, 04:25 AM   #21
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,059

Rep: Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671
What more is there to add ?.
Once I have incremented the array I "next" - more readable to my eye.
 
Old 11-04-2021, 03:15 PM   #22
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,037

Rep: Reputation: Disabled
@sysg00. Ha, just one hour before you posted this I changed my post #15 to include next. Great minds think alike, I guess.
 
Old 11-06-2021, 11:26 AM   #23
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,856

Rep: Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827
The awk code more compacted.
Code:
awk '
  NR==FNR { D[$1]; next }
  !(FNR in D)
' $DFile $InFile >$OutFile
(FNR in D) is the korrekt lookup.
D[FNR] adds an empty element if not present.
 
Old 11-06-2021, 02:41 PM   #24
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,841

Rep: Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649
Quote:
Originally Posted by MadeInGermany View Post
The awk code more compacted.
Code:
awk '
  NR==FNR { D[$1]; next }
  !(FNR in D)
' $DFile $InFile >$OutFile
...
Technical intuition (not always correct) suggests this solution is inefficient. Previous solutions used !D[FNR] which is a "rifle shot" test. By contrast !(FNR in D) is a "shotgun" test which requires searching the D array. With the sample files shown in post #6 the majority of searches would be through the entire D array.

Daniel B. Martin

.

Last edited by danielbmartin; 11-06-2021 at 02:42 PM.
 
Old 11-06-2021, 03:57 PM   #25
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,856

Rep: Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827
Not true.
The addressing technique is identical.
But D[FNR] cannot distinguish between "empty" and "not exist".
In fact, if not present it creates an empty element, so it can return a value.
The D[FNR] is more overhead and less precise.
 
1 members found this post helpful.
Old 11-06-2021, 04:41 PM   #26
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,841

Rep: Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649Reputation: 649
Quote:
Originally Posted by MadeInGermany View Post
... The D[FNR] is more overhead and less precise.
Thank you for this clarification. I'm interested in execution efficiency. Is there a web site or manual or tutorial which teaches this subject?

Daniel B. Martin

.
 
Old 11-07-2021, 02:41 AM   #27
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,856

Rep: Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827
It is well explained in
https://www.math.utah.edu/docs/info/gawk_12.html#SEC114
 
1 members found this post helpful.
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with reading multiple lines from multiple files VijayB Linux - Newbie 2 11-22-2014 09:31 AM
Sed issue with finding a match on two sequential lines and deleting the previous line ulto Programming 3 10-26-2014 07:26 PM
[SOLVED] CAT command | multiple lines to multiple lines udiubu Programming 11 10-28-2011 07:09 AM
[SOLVED] Deleting the same user in differents servers at the same time... W_Chevezman Linux - Software 3 02-25-2011 02:57 PM
deleting duplicate lines without deleting first instance of the duplicated line jkeertir Linux - Newbie 2 02-07-2011 07:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:26 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