Printf with backreference in ruby


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printf with backreference in ruby
# 1  
Old 08-31-2013
Printf with backreference in ruby

Hello everybody,

May you help with this, I don't know what is wrong.

I want trying to print the first 4 characters as decimal and remove the "k's" from the next 7 characters.

I'm trying with gsub and backrefence as shown below trying to remove the "k's" and then trying to assign to "x" the 0x0020
using the backreference of pattern1, but is not working.


What I've tried:
Code:
string = "0020aakbbjk30294748"

string=string.gsub!(/(\d{4})([a-z]{7})((\d){5})/,'k')

x="0x"+"#{$1}"

printf("%d ",$x) 
puts "#{$2} #{$3}"

desired output:
Code:
32 aabbj 30294

Thanks in advance for any help.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Segregate file content using sed backreference

I have some text like EU1BTDAT:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1BTDATEST:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1CLOSEDATES:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1DATED:ASSGNDD... (8 Replies)
Discussion started by: gotamp
8 Replies

2. Shell Programming and Scripting

Help with sed backreference please

Hi, I'm using /bin/sh I would appreciate if someone could help me with SED syntax for a "simple" line. Here is where I Got to: I have these strings that are returned by my(Examples) (naturally "FullPath" is always changing don't hardcode this lol) FullPath/AAA.framework... (3 Replies)
Discussion started by: Herrick
3 Replies

3. Shell Programming and Scripting

Regex and backreference to replace in binary file

Hello to all, I have this sed script that replaces hex strins within a binary file. As you can see, I want to replace all bytes 4X with 2X (where X could take values 0 to F). sed -e 's/\x40/\x20/g' -e 's/\x41/\x21/g' -e 's/\x42/\x22/g' -e 's/\x43/\x23/g' -e 's/\x44/\x24/g' -e... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

4. What is on Your Mind?

Ruby language

Hi All, Could you please suggest books for learning ruby and python language ? Thanks, Pravin (1 Reply)
Discussion started by: pravin27
1 Replies

5. Shell Programming and Scripting

sed regex backreference replacement

Hello, I want to rename multiple files and catch some points about backreference within sed and regex. Here is a part of my file list. Input: S92A.fa S92B.fa ... S96Z.fa S921.fa S922.fa ... S997.fa Note: The file names are not necessarily continuous from A~Z or 921 ~ 997, as some of the... (3 Replies)
Discussion started by: yifangt
3 Replies

6. Shell Programming and Scripting

What is Ruby?

Lately there have been a lot of one-liners posted in "ruby" on the apparent assumption that mainstream unix or Linux come with "ruby. They don't. What is "ruby"? What platforms and Operating System versions are supported? The syntax for "ruby" seems remarkably obscure compared with say "awk"... (13 Replies)
Discussion started by: methyl
13 Replies

7. UNIX for Dummies Questions & Answers

Help | Unix | grep | regular expression | backreference | Syntax/Logic

Hello, I'm working on learning regular expressions and what I can do with them. I'm using unix to and its programs to experiment and learn what my limitations are with them. I'm working on duplicating the regular expression: ^(.*)(\r?\n\1)+$ This is supposed to delete duplicate lines... (2 Replies)
Discussion started by: MykC
2 Replies

8. Shell Programming and Scripting

egrep vs. sed backreference

My egrep outputs this: $ cat html.out|sed -n '/bluetext/s/&nbsp;/ /gp'|egrep '{5}' <span class="bluetext"><b> Lexington Park, MD 20653</b></span> But my backreference \1 is empty. I dont understand why. Can someone clarify? $ cat html.out|sed -n '/bluetext/s/&nbsp;/ /gp'|sed -n... (1 Reply)
Discussion started by: r0sc0
1 Replies
Login or Register to Ask a Question
RUBY-SWITCH(1)															    RUBY-SWITCH(1)

NAME
ruby-switch - switch between different Ruby interpreters USAGE
ruby-switch --list ruby-switch --check ruby-switch --set RUBYVERSION ruby-switch --auto DESCRIPTION
ruby-switch can be used to easily switch to different Ruby interpreters as the default system-wide interpreter for your Debian system. When run with --list, all supported Ruby interpreters are listed. When --check is passed, ruby-switch will check which Ruby interpreter is currently being used. If the settings are inconsistent -- e.g. `ruby` is Ruby 1.8 and `gem` is using Ruby 1.9.1, ruby-switch will issue a big warning. When --set RUBYINTERPRETER is used ruby-switch will switch your system to the corresponding Ruby interpreter. This includes, for example, the default implementations for the following programs: ruby, gem, irb, erb, testrb, rdoc, ri. ruby-switch --set auto will make your system use the default Ruby interpreter currently suggested by Debian. OPTIONS
-h, --help Displays the help and exits. A NOTE ON RUBY 1.9.x Ruby uses two parallel versioning schemes: the `Ruby library compatibility version' (1.9.1 at the time of writing this), which is similar to a library SONAME, and the `Ruby version' (1.9.3 is about to be released at the time of writing). Ruby packages in Debian are named using the Ruby library compatibility version, which is sometimes confusing for users who do not follow Ruby development closely. ruby-switch also uses the Ruby library compatibility version, so specifying `ruby1.9.1' might give you Ruby with version 1.9.2, or with version 1.9.3, depending on the current Ruby version of the `ruby1.9.1' package. COPYRIGHT AND AUTHORS
Copyright (c) 2011, Antonio Terceiro <terceiro@debian.org> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. 2011-11-20 RUBY-SWITCH(1)