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
haml(1) 						      General Commands Manual							   haml(1)

NAME
haml - Translates Haml markup into its HTML equivalent SYNOPSIS
haml [options] [INPUT] [OUTPUT] DESCRIPTION
Uses the Haml engine to parse the selected template and outputs the result to the specified file. OPTIONS
--rails RAILS_DIR Install Haml and Sass to a Rails project -c, --check Just check syntax, don't evaluate. -s, --stdin Read input from standard input instead of an input file --trace Show a full traceback on error -t, --style NAME Output style. Can be indented (default) or ugly. -f, --format NAME Output format. Can be xhtml (default), html4, or html5. -e, --escape-html Escape HTML characters (like ampersands and angle brackets) by default. -q, --double-quote-attributes Set attribute wrapper to double-quotes (default is single). -r, --require FILE Same as 'ruby -r'. -I, --load-path PATH Same as 'ruby -I'. --debug Print out the precompiled Ruby source. -?, -h, --help Show a usage summary -v, --version Print version SEE ALSO
This program is shipped as part of the libhaml-ruby1.8 library package, you can check its corresponding documentation can be found in the libhaml-ruby-doc package. AUTHOR
This manual page was written by Gunnar Wolf <gwolf@debian.org>, based on the command-line output of this program, for the Debian GNU/Linux system (but may be freely used by others). December 18, 2008 haml(1)