LinuxQuestions.org
Help answer threads with 0 replies.
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 07-15-2020, 06:32 AM   #16
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,799

Rep: Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854

Quote:
Originally Posted by rnturn View Post
Code:
cat file | grep '\.jsp' | cut -d= -f2 | sort | uniq
using such a command chain is deprecated because it eats up a lot of resources and slow.
Code:
cat file | grep pattern
# is identical:
grep file pattern

sort | uniq
# is equal to
sort -u
and finally cut can be combined into that grep (as it was already shown) - or you can use awk/perl/python/whatever
 
Old 07-16-2020, 09:14 AM   #17
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 396

Rep: Reputation: 245Reputation: 245Reputation: 245
Quote:
Originally Posted by pan64 View Post
using such a command chain is deprecated because it eats up a lot of resources and slow.
Code:
cat file | grep pattern
# is identical:
grep file pattern
Pattern comes before file...
"Usage: grep [OPTION]... PATTERN [FILE]..."


Quote:
Code:
sort | uniq
# is equal to
sort -u
Since there's no indication that the results should be sorted, adding a uniqueness filter could be done within grep (no piping needed) - e.g. this adaptation of Shruggy's answer...

Code:
grep -Poz '(?s)\.jsp\?id=\K(\d++)(?!.*\.jsp\?id=\1(?!\d))' "$file"
...though it's not immediately obvious what's being done and is probably less efficient, so I wouldn't recommend it for cases like this, but it does demonstrate a couple of useful regex concepts.

 
1 members found this post helpful.
Old 07-16-2020, 11:47 AM   #18
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,799

Rep: Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854Reputation: 4854
Quote:
Originally Posted by boughtonp View Post
Pattern comes before file...
"Usage: grep [OPTION]... PATTERN [FILE]..."
Yes, obviously, that was only a mistake.
 
Old 07-17-2020, 01:09 PM   #19
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,975

Rep: Reputation: 325Reputation: 325Reputation: 325Reputation: 325
Quote:
Originally Posted by pan64 View Post
using such a command chain is deprecated because it eats up a lot of resources and slow.
Code:
cat file | grep pattern
# is identical:
grep file pattern

sort | uniq
# is equal to
sort -u
and finally cut can be combined into that grep (as it was already shown) - or you can use awk/perl/python/whatever
My assumption was that this is not something that needs to be done often. And certainly not while the OP's PC is controlling the landing of his spacecraft on the lunar surface---those 1201 alarms are so annoying.

I can imagine the flak I'd have received from some if I'd suggested using Perl.

/s
 
Old 07-18-2020, 07:50 AM   #20
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,357

Rep: Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452
Quote:
Originally Posted by rnturn View Post
My assumption was that this is not something that needs to be done often. And certainly not while the OP's PC is controlling the landing of his spacecraft on the lunar surface---those 1201 alarms are so annoying.

I can imagine the flak I'd have received from some if I'd suggested using Perl.

/s
And I note, now that you mention PERL, that it would (up to a point) scale far better than other most other options. It would not require calling other/outside utilities, and could slurp the entire file and work it in memory and spit out the answer with no other I/O. Thank you for the reminder!
 
  


Reply

Tags
shell script


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
Shell Script to read all the lines from the specified string until an unmatched string anweshbabu Linux - Newbie 4 07-15-2018 05:09 PM
i am not able to concatenate both the string in shell script as below string spatil20 Linux - Newbie 16 04-24-2016 02:59 AM
Shell script to find a string and print x lines before and y lines after the string igorza Linux - Newbie 6 04-18-2013 04:31 PM
extract a string within a string using a pattern adshocker Linux - Newbie 1 11-04-2010 10:44 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM

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

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