LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-21-2012, 03:30 PM   #16
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,100

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288

Quote:
Originally Posted by Cedrik View Post
Code:
 grep -P '(?=(....))(.).*\'
Perfect! Thank you!

Daniel B. Martin
 
Old 02-21-2012, 03:42 PM   #17
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
You can remove the last parentheses (like my edited previous post)
They are not needed and slow down performance (they were left from my trial and error attempts)

ps: is it me or there is a forum bug in this page ?

(edit: fixed now )

Last edited by Cedrik; 02-22-2012 at 06:19 AM.
 
2 members found this post helpful.
Old 02-21-2012, 04:22 PM   #18
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,100

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by Cedrik View Post
... is it me or there is a forum bug in this page ?
Something is broken.

Daniel B. Martin
 
Old 02-22-2012, 07:22 AM   #19
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,100

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by danielbmartin View Post
Something is broken.
... and has been fixed. Good.

Daniel B. Martin
 
Old 02-22-2012, 07:42 AM   #20
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Quote:
Originally Posted by danielbmartin View Post
I have not ignored your advice. To my eyes, the cat improves code readability. Since it performs no logic I assume the cost of the cat is negligible, particularly in code where it is the first of a long string of piped commands. I have not tested this assumption and may be wrong.
Daniel B. Martin
Well a "grep 'something' inputFile doesn't either perform any logic to get it's input.

And you are spawning a subprocess.
This script might stop, if there are no more process ids available while the script without the useless use of cat would still work.
Admitted a very rare condition.

But imagine running the cat construct in a long running loop you WILL see a a heavy performance penalty.

I can't get the point of readability.
The "grep what where" pattern is to basic to be unreadable imho

The more you use the more errors can arise.
Are you aware of the buffering mechanism which bash does when it comes to piping?
The source of subtle failures.
Lucky debugge!

Adhere to the KISS KeepItStupidSimple pattern.
Do only do what really is required.
 
1 members found this post helpful.
Old 02-22-2012, 07:45 AM   #21
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Trying to explain the reg exp...
Code:
(?=(....))..*\1
With (....) match, the reg exp would capture 4 chars in buffer and advance position in the string by 4 chars
So with 'ratatat' example
Code:
echo ratatat | perl -ne 'print "$1\n" while /(....)/g'
rata
With (?=(....)), the reg exp captures 4 chars in buffer and position stays in place, the next search will advance position by one char
Code:
echo ratatat | perl -ne 'print "$1\n" while /(?=(....))/g'
rata
atat
tata
atat
The dot following (?=(....)) enforces the match only if there is at least one char before the captured buffer match, else any >=4 chars words would match with only .*\1

Last edited by Cedrik; 02-22-2012 at 07:52 AM.
 
1 members found this post helpful.
Old 02-22-2012, 09:35 AM   #22
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,100

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by uhelp View Post
I can't get the point of readability.
The "grep what where" pattern is to basic to be unreadable imho
Coding style is a matter of personal preference. Readable to you might not be to me, and vice-versa. In some workplaces a particular style may be mandated for the sake of uniformity. My programming is purely recreational so I am free to suit my own preferences.

Quote:
Are you aware of the buffering mechanism which bash does when it comes to piping?
Most of my Linux pipes are embedded in REXX programs, so I assume bash is not involved.

Quote:
Do only do what really is required.
Agreed, but your idea of "required" may be different from mine. I prize code readability. As a Linux newbie, readable code may be more important to me than you or others.

Daniel B. Martin
 
Old 02-23-2012, 11:51 AM   #23
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,100

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
Thanks to everyone who contributed to this thread.
Having received several excellent solutions, this problem will be marked SOLVED!

Daniel B. Martin
 
  


Reply

Tags
grep, sed, strings


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
[SOLVED] Select words with alphabetical-order character strings danielbmartin Programming 7 02-17-2012 12:08 AM
printing rows with repeated strings verse123 Linux - Newbie 4 12-20-2011 04:56 AM
[SOLVED] Array of words(strings)- please help! stefanolima Programming 4 07-12-2010 04:15 AM
[SOLVED] How can I extract strings based on character positions? btacuso Linux - Newbie 8 03-25-2010 12:31 PM


All times are GMT -5. The time now is 09:16 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration