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 09-27-2014, 11:56 AM   #16
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660

Quote:
Originally Posted by firstfire View Post
Code:
$ echo first | sed -rn 's/$/\nabcdefghijklmnopqrstuvwxyz/; s/(...)(.*)\n.*\1.*/\U\1\E/p'
fiRST
With this InFile ...
Code:
firstfire is an excellent programmer
the baseball player struck out
ghibli is a desert wind
excessive heat burst my wurst
now is the time
... this sed ...
Code:
sed -rn 's/$/\nabcdefghijklmnopqrstuvwxyz/;
  s/(...)(.*)\n.*\1.*/\U\1\E/p' $InFile >$OutFile
... produced this OutFile ...
Code:
fiRST
GHI
excessive heat buRST
... good, but the expected result was ...
Code:
fiRSTfire is an excellent programmer
the baseball player STRuck out
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
Daniel B. Martin
 
Old 09-27-2014, 12:18 PM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
@firstfire - I found a small glitch in the sed solution. Try it with something that starts with the consecutive letters, like 'define'

Again, to Daniel, the new question is ambiguous on the same point, ie. are we assuming english spoken words or could we receive 'abcdef' and in which case, should all 6 characters now be upper case?

As to the current solution, where we upper case the first 3 characters found to be consecutive:
Code:
ruby -ane 'a="";$F[0].chomp.each_char{|c| if ! a.empty? && c == a[-1].next; a<<c; else a = c;end; break if a.size == 3};puts $F[0].sub(a,a.upcase)' file

EDIT: Just saw new file requirement that there are multiple words on the lines and not single entries. Above works for a file with one word per line

Last edited by grail; 09-27-2014 at 12:20 PM.
 
Old 09-27-2014, 12:30 PM   #18
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
Again, to Daniel, the new question is ambiguous on the same point, ie. are we assuming english spoken words or could we receive 'abcdef' and in which case, should all 6 characters now be upper case?
You are right... ambiguous. Let's make the problem statement elastic: solve it any way you like, so long as you declare what your solution intends to do.

Daniel B. Martin
 
Old 09-27-2014, 12:46 PM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Ok, so I believe this solution will provide the output expected in post #16, with one slight caveat, the expected results shows the word 'struck' in the second line as being
part of the solution, but 'str' are not consecutive in this order. So presented solution looks for 3 characters in english alphabet order as they appear on the line and will only replace the first 3
that appear in a word, ie abcdef will become ABCdef:
Code:
ruby -ape '$F.each{|x| a="";x.each_char{|c| if ! a.empty? && c == a[-1].next; a<<c; else a = c;end; if a.size == 3; $_.sub!(x,x.sub(a,a.upcase)); break; end}}' file
 
Old 09-27-2014, 01:09 PM   #20
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Yes, there are cuple of bugs in my solution. Here is the v2:
Code:
$ cat infile 
firstfire is an excellent programmer
the baseball player struck out
ghibli is a desert wind
excessive heat burst my wurst
now is the time
abcdef
$ sed -rn 's/$/\nabcdefghijklmnopqrstuvwxyz/; :a; s/(...)(.*\n.*\1.*)/\U\1\E\2/; ta; s/\n.*//p' < /tmp/infile 
fiRSTfire is an excellent programmer
the baseball player struck out
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
ABCDEF
I marked with bold what was added (also one closing parenthesis has been moved).

This script will upper-case all occurences of 3-character substrings of english alphabet.

Last edited by firstfire; 09-27-2014 at 01:14 PM.
 
Old 09-27-2014, 01:15 PM   #21
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Unhappy

Quote:
Originally Posted by grail View Post
... the expected results shows the word 'struck' in the second line as being part of the solution, but 'str' are not consecutive in this order.
Human error. <shamefaced look>

Let's change to this InFile ...
Code:
firstfire is a skillful programmer
the truck was stuck in the mud
ghibli is a desert wind
excessive heat burst my wurst
now is the time
... and look for ...
Code:
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
Daniel B. Martin
 
Old 09-27-2014, 01:22 PM   #22
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by firstfire View Post
Here is the v2 ...
Tested, and perfect! Thank you!

Daniel B. Martin
 
Old 09-27-2014, 01:26 PM   #23
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
Ok, so I believe this solution will provide the output expected ...
My ruby is back-level so my test results are questionable. My OutFile was a copy of the InFile.

Daniel B. Martin
 
Old 09-27-2014, 02:03 PM   #24
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
I have checked back to version 1.8.7 and all commands I have used are available. After adding, 'abcdef' to the end I get the following output:
Code:
[grail@pilgrim]$ ruby -ape '$F.each{|x| a="";x.each_char{|c| if ! a.empty? && c == a[-1].next; a<<c; else a = c;end; if a.size == 3; $_.sub!(x,x.sub(a,a.upcase)); break; end}}' infile
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
ABCdef
 
Old 09-27-2014, 02:41 PM   #25
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
I have checked back to version 1.8.7 ...
A mystery.
Code:
daniel@daniel-desktop:~$ ruby --version
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
With this InFile ...
Code:
firstfire is a skillful programmer
the truck was stuck in the mud
ghibli is a desert wind
excessive heat burst my wurst
now is the time
abcdef
... this code ...
Code:
echo; echo "Method of LQ Member firstfire, using SED."
sed -rn 's/$/\nabcdefghijklmnopqrstuvwxyz/;
 :a; s/(...)(.*\n.*\1.*)/\U\1\E\2/; ta;
   s/\n.*//p' $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of File" 

echo; echo "Method of LQ Guru grail, using RUBY."
ruby -ape '$F.each{|x| a="";x.each_char{|c| if ! a.empty? && c == a[-1].next; a<<c; else a = c;end; if a.size == 3; $_.sub!(x,x.sub(a,a.upcase)); break; end}}' $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of File" 

echo; echo "Method of LQ member danielbmartin, using SED."
sed -f $PatFile $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of File"

echo; echo "Normal end of job.";echo; exit
... produced this result ...
Code:
daniel@daniel-desktop:~$ bash /home/daniel/Desktop/LQfiles/dbm1249.bin

Method of LQ Member firstfire, using SED.
OutFile ...
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
ABCDEF
End Of File

Method of LQ Guru grail, using RUBY.
OutFile ...
firstfire is a skillful programmer
the truck was stuck in the mud
ghibli is a desert wind
excessive heat burst my wurst
now is the time
abcdef
End Of File

Method of LQ member danielbmartin, using SED.
OutFile ...
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wurst
now is the time
ABCDEF
End Of File

Normal end of job.
Perhaps, due to my lack of ruby knowledge, I invoked your code improperly.

Daniel B. Martin
 
Old 09-28-2014, 06:32 AM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Well I didn't know what PatFile was supposed to be equal to so I got an error there, but I copied your script and got the following output:
Code:
Method of LQ Member firstfire, using SED.
OutFile ...
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
ABCDEF
End Of File

Method of LQ Guru grail, using RUBY.
OutFile ...
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
ABCdef
End Of File

Method of LQ member danielbmartin, using SED.
sed: file f1 line 1: unknown command: `f'
OutFile ...
End Of File

Normal end of job.
So you have run it correctly, but unfortunately I do not currently have access to a box running 1.8.7 to see why it is failing
 
Old 09-28-2014, 08:19 AM   #27
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Ok ... did some poking around and found the flaw, was quite tricky, but the below should work with 1.8.7 (on that note, is there any reason you cannot move to a later version? this one is quite
dated)
Code:
ruby -ape '$F.each{|x| a="";x.each_char{|c| if ! a.empty? && c == a[-1,1].next; a<<c; else a = c;end; if a.size == 3; $_.sub!(x,x.sub(a,a.upcase)); break; end}}'
 
1 members found this post helpful.
Old 09-28-2014, 11:08 AM   #28
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
... is there any reason you cannot move to a later version?
I've tried to install later versions of Ubuntu and (so far) my computer pukes so I continue to limp along with 10.04. One of these days I will try again!

Daniel B. Martin
 
Old 09-28-2014, 11:22 AM   #29
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
Well I didn't know what PatFile was supposed to be ...
I improved this file and changed the name to CmdFile.

This is the CmdFile ...
Code:
s/abc/ABC/g
s/bcd/BCD/g
s/cde/CDE/g
s/def/DEF/g
s/efg/EFG/g
s/fgh/FGH/g
s/ghi/GHI/g
s/hij/HIJ/g
s/ijk/IJK/g
s/jkl/JKL/g
s/klm/KLM/g
s/lmn/LMN/g
s/mno/MNO/g
s/nop/NOP/g
s/opq/OPQ/g
s/pqr/PQR/g
s/qrs/QRS/g
s/rst/RST/g
s/stu/STU/g
s/tuv/TUV/g
s/uvw/UVW/g
s/vwx/VWX/g
s/wxy/WXY/g
s/xyz/XYZ/g
Not a thing of beauty but it gets the job done.

Using your most recent ruby solution ...
Code:
echo; echo "Method of LQ Member firstfire, using SED."
sed -rn 's/$/\nabcdefghijklmnopqrstuvwxyz/;
 :a; s/(...)(.*\n.*\1.*)/\U\1\E\2/; ta;
   s/\n.*//p' $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of File" 

echo; echo "Method of LQ Guru grail, using RUBY."
ruby -ape '$F.each{|x| a="";x.each_char{|c|
   if ! a.empty? && c == a[-1,1].next; a<<c;
   else a = c;end;
   if a.size == 3; $_.sub!(x,x.sub(a,a.upcase)); break; end}}'  \
 $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of File" 

echo; echo "Method of LQ member danielbmartin, using SED."
sed -f $CmdFile $InFile >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of File"

echo; echo "Normal end of job.";echo; exit
... produced this result ...
Code:
daniel@daniel-desktop:~$ bash /home/daniel/Desktop/LQfiles/dbm1249.bin

Method of LQ Member firstfire, using SED.
OutFile ...
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
ABCDEF
End Of File

Method of LQ Guru grail, using RUBY.
OutFile ...
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
ABCdef
End Of File

Method of LQ Member danielbmartin, using SED.
OutFile ...
fiRSTfire is a skillful programmer
the truck was STUck in the mud
GHIbli is a desert wind
excessive heat buRST my wuRST
now is the time
ABCDEF
End Of File

Normal end of job.
There is one remaining nitpick: abcdef should be transformed to ABCDEF.

Daniel B. Martin
 
Old 09-28-2014, 11:28 AM   #30
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Well that is an additional requirement which I did previously mention was not part of this solution

I'll see what needs to be changed for correction. I would add that your patfile solution is rather limited as if the requirement changed top say 5 consecutive letters
you would have to completely re-write the file, whereas the ruby and first sed solution need a minor change.
 
1 members found this post helpful.
  


Reply

Tags
awk, gsub, 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
[SOLVED] Finding doubled letters with grep danielbmartin Programming 13 09-24-2014 06:08 AM
[SOLVED] Bash: Checking for lower case and upper case letters in a string fatalerror0x00 Programming 1 12-09-2012 02:17 AM
Question about creating files, and upper and lower case letters clifford227 Linux - Newbie 10 08-23-2012 05:02 AM
File extension and upper and lower case in .text files thepowerofone Linux - Newbie 9 11-25-2011 11:47 AM
upper case letters in kde 3.5through tight vnc on debian sunpascal Linux - Software 0 03-28-2006 05:37 PM

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

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