LinuxQuestions.org
Register a domain and help support LQ
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 08-23-2012, 09:08 PM   #16
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,083

Original Poster
Rep: Reputation: 287Reputation: 287Reputation: 287

Quote:
Originally Posted by ntubski View Post
Code:
ruby -pe '$_=$_.chop.split(/ /, -1).reverse.join(" ")+"\n"' file
Thank you for this contribution. Without knowing ruby I can't make a judgement on readability. However, your solution scores points for being concise.

Daniel B. Martin
 
Old 08-23-2012, 11:31 PM   #17
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,475

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
@ntubski - you have just proven why I love learning this language. I was unaware of the split properties and uses and this makes my clunky scan solution obsolete. Thank you for the valuable lesson
 
Old 08-24-2012, 09:15 AM   #18
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,083

Original Poster
Rep: Reputation: 287Reputation: 287Reputation: 287
Sometimes code readability is improved by breaking a pipe into individual commands, one per line.

Contrived example:
Code:
cat $InFile   \
|rev          \
|cut -d, -f7- \
|rev          \
> $Work3
It would be even better to comment every line...
Code:
cat $InFile   \  # Read input file
|rev          \  # Reverse (flip each line end-for-end)
|cut -d, -f7- \  # Toss fields 1-6
|rev          \  # Reverse, again 
> $Work3         # Write interim result to a work file
Bash won't allow this.

Does ruby allow you to break a line such as
Code:
ruby -pe '$_=$_.chop.split(/ /, -1).reverse.join(" ")+"\n"' file
into pieces with a comment for each piece?

Daniel B. Martin
 
Old 08-24-2012, 11:24 AM   #19
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,475

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
The difference with your bash example and the ruby one is that bash has used 4 separate commands (5 if include redirection), whereas the ruby example is the equivalent of a single command.
So the alternative would be to perform each task and return a new value each time.
Code:
ruby -pe 'o = $_.chop; # Remove terminating newline
a = o.split(/ /, -1);  # Split using spaces and preserve trailing whitespace
r = a.reverse;         # Reverse the array items
s = r.join(" ");       # Join array items using a space as delimiter
$_ = s + "\n"          # Add new line back' file
The above works fine for me. May well be better suited to writing in a script file and using ruby as interpreter, similar to awk.
 
1 members found this post helpful.
  


Reply

Tags
awk, 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
How to get the line no. which have the max words...? Love@Nagpur Linux - Newbie 1 08-06-2012 09:14 AM
[SOLVED] awk - sort words within each line danielbmartin Programming 3 02-03-2012 11:17 AM
how to capture the line after the matching words another 1 Linux - General 2 11-15-2011 07:22 AM
break line into words nushki Programming 16 12-04-2009 09:15 AM
BASH: First words in a line JordanH Programming 7 10-24-2004 10:00 AM


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