mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2013-05-03, 14:00   #1
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

510210 Posts
Default Bashing Strings Parsimoniously in Linux

Any BASH gurus looking for a frustrated "amateur" programmer to help out?

I'm trying to do what I thought would be a simple task, but it seems to be more trouble than it's worth...

the crux:

I have a string:
Code:
[May 02 2013, 23:52:50] Cofactor 127881022016992373630150068641535329304189051054726878835804414980494663758554596515035599713886561221873 (105 digits)
I want only the inner number.

I have successfully trimmed the front:
Code:
echo $templine
echo
cpos=`expr index "$templine" C`
let cpos=$cpos+8
templine=${templine:$cpos}
echo $templine
result:
Code:
[May 02 2013, 23:52:50] Cofactor 127881022016992373630150068641535329304189051054726878835804414980494663758554596515035599713886561221873 (105 digits)

127881022016992373630150068641535329304189051054726878835804414980494663758554596515035599713886561221873 (105 digits)
But, I'm having difficulty trimming the end off. I can't find any way to search for a space or open parenthesis. I'll skip telling all the things I've tried, since they didn't work.

Any assistance is gratefully appreciated. Thanks...
EdH is online now   Reply With Quote
Old 2013-05-03, 14:13   #2
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2×5×1,087 Posts
Default

Quote:
Originally Posted by EdH View Post
Any BASH gurus looking for a frustrated "amateur" programmer to help out?
Code:
[chalsall@burrow edh]$ cat input.txt 
[May 02 2013, 23:52:50] Cofactor 127881022016992373630150068641535329304189051054726878835804414980494663758554596515035599713886561221873 (105 digits)

[chalsall@burrow edh]$ sed 's/.* Cofactor \([0-9]*\).*/\1/' input.txt
127881022016992373630150068641535329304189051054726878835804414980494663758554596515035599713886561221873
chalsall is offline   Reply With Quote
Old 2013-05-03, 14:20   #3
Gimarel
 
Apr 2010

22·61 Posts
Default

To remove the prefix and the suffix just with bash you can do something like:
Code:
withoutsuffix=${templine% \(*}
number=${withoutsuffix##* }
Gimarel is offline   Reply With Quote
Old 2013-05-03, 15:22   #4
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

10011111011102 Posts
Default

Excellent! Thanks for the help. I've implemented Gimarel's code into my script at this point, but will be studying Chalsall's further as well.

Could someone point me toward a good study reference?

I would like to specifically find out how to parse a string that includes several double quote characters, as well as understand fully the previous posts.

Thanks much!!
EdH is online now   Reply With Quote
Old 2013-05-03, 15:28   #5
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2×5×1,087 Posts
Default

Quote:
Originally Posted by EdH View Post
Could someone point me toward a good study reference?
For sed (Stream EDitor), this is pretty good.

If you haven't wrapped your head around Regular Expressions (regex) yet, just Google for things like "regular expression cheat sheet", "regular expression examples", etc. Regex can seem a little intimidating initially, but they're well worth the effort to learn.
chalsall is offline   Reply With Quote
Old 2013-05-03, 15:54   #6
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

2·2,551 Posts
Default

Quote:
Originally Posted by chalsall View Post
For sed (Stream EDitor), this is pretty good.

If you haven't wrapped your head around Regular Expressions (regex) yet, just Google for things like "regular expression cheat sheet", "regular expression examples", etc. Regex can seem a little intimidating initially, but they're well worth the effort to learn.
Thanks! I was using the following for reference:

Advanced Bash Shell Scripting Guide
Advanced Bash Shell Scripting Guide - A Brief Introduction to Regular Expressions

But, I couldn't find a way to parse the space or ( in my previous posts.

I had briefly looked at sed and awk as referenced somewhere, but thought I'd better spend some time with BASH before branching off too much. It also seemed like sed is for working within a file, which didn't seem to be what I was doing, and awk was yet another scripting tool (or, so it seemed).

Anyway, thanks for all the help.

Off to study...
EdH is online now   Reply With Quote
Old 2013-05-03, 16:05   #7
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2×5×1,087 Posts
Default

Quote:
Originally Posted by EdH View Post
I had briefly looked at sed and awk as referenced somewhere, but thought I'd better spend some time with BASH before branching off too much. It also seemed like sed is for working within a file, which didn't seem to be what I was doing, and awk was yet another scripting tool (or, so it seemed).
To be perfectly honest, I've never taught myself much BASH scripting. My position is if I'm going to script something, I'll write it in Perl which is much more capable. Plus, the code then can be moved into CGI scripts, will run under Windows, etc.

But, no matter what tool you are using, understanding Regex is key.

Quote:
Originally Posted by EdH View Post
Anyway, thanks for all the help.
You are most welcome.
chalsall is offline   Reply With Quote
Old 2013-05-04, 09:46   #8
Mr. P-1
 
Mr. P-1's Avatar
 
Jun 2003

100100100012 Posts
Default

Quote:
Originally Posted by EdH View Post
I have a string:
Code:
[May 02 2013, 23:52:50] Cofactor 127881022016992373630150068641535329304189051054726878835804414980494663758554596515035599713886561221873 (105 digits)
I want only the inner number.
Code:
cut -d' ' -f6
Mr. P-1 is offline   Reply With Quote
Old 2013-05-04, 19:26   #9
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

10011111011102 Posts
Default

Quote:
Originally Posted by Mr. P-1 View Post
Code:
cut -d' ' -f6
I "think I" can see what this does, but not sure how to implement it, yet. "The implementation is left as an exercise for the reader." Off to exercise...
EdH is online now   Reply With Quote
Old 2013-05-04, 22:30   #10
Nick
 
Nick's Avatar
 
Dec 2012
The Netherlands

5·353 Posts
Default

Quote:
Originally Posted by chalsall View Post
But, no matter what tool you are using, understanding Regex is key.
Absolutely. And understanding the way that regular languages correspond with finite automata is the beginning of real computer science.
Nick is offline   Reply With Quote
Old 2013-05-04, 22:54   #11
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

1087010 Posts
Default

Quote:
Originally Posted by Nick View Post
Absolutely. And understanding the way that regular languages correspond with finite automata is the beginning of real computer science.
Possibly...

What is called Computer Science currently is really just Engineering.

IMHO, when we finally get to Computer Science (and we're close), it will be non-deterministic....
chalsall is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Digit strings containing primes davar55 Puzzles 13 2018-03-15 14:46
Primes from powers of 2 strings. Flatlander Puzzles 40 2011-02-10 09:42
ubasic question - strings Andi47 Programming 5 2008-12-28 05:52
Strings of Digits davar55 Puzzles 5 2008-11-02 00:08
Linux Drooling_Sheep Software 8 2003-11-22 05:50

All times are UTC. The time now is 13:33.


Mon Dec 5 13:33:45 UTC 2022 up 109 days, 11:02, 0 users, load averages: 0.58, 0.72, 0.80

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.

≠ ± ∓ ÷ × · − √ ‰ ⊗ ⊕ ⊖ ⊘ ⊙ ≤ ≥ ≦ ≧ ≨ ≩ ≺ ≻ ≼ ≽ ⊏ ⊐ ⊑ ⊒ ² ³ °
∠ ∟ ° ≅ ~ ‖ ⟂ ⫛
≡ ≜ ≈ ∝ ∞ ≪ ≫ ⌊⌋ ⌈⌉ ∘ ∏ ∐ ∑ ∧ ∨ ∩ ∪ ⨀ ⊕ ⊗ 𝖕 𝖖 𝖗 ⊲ ⊳
∅ ∖ ∁ ↦ ↣ ∩ ∪ ⊆ ⊂ ⊄ ⊊ ⊇ ⊃ ⊅ ⊋ ⊖ ∈ ∉ ∋ ∌ ℕ ℤ ℚ ℝ ℂ ℵ ℶ ℷ ℸ 𝓟
¬ ∨ ∧ ⊕ → ← ⇒ ⇐ ⇔ ∀ ∃ ∄ ∴ ∵ ⊤ ⊥ ⊢ ⊨ ⫤ ⊣ … ⋯ ⋮ ⋰ ⋱
∫ ∬ ∭ ∮ ∯ ∰ ∇ ∆ δ ∂ ℱ ℒ ℓ
𝛢𝛼 𝛣𝛽 𝛤𝛾 𝛥𝛿 𝛦𝜀𝜖 𝛧𝜁 𝛨𝜂 𝛩𝜃𝜗 𝛪𝜄 𝛫𝜅 𝛬𝜆 𝛭𝜇 𝛮𝜈 𝛯𝜉 𝛰𝜊 𝛱𝜋 𝛲𝜌 𝛴𝜎𝜍 𝛵𝜏 𝛶𝜐 𝛷𝜙𝜑 𝛸𝜒 𝛹𝜓 𝛺𝜔