Need help with regex groups


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with regex groups
# 1  
Old 06-22-2011
Need help with regex groups

I have a requirement - replace specified positions in a string with a character. I found perl regex useful for this approach. however, I am facing the following issue.

The target file 'temp' contains -
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The goal is to convert position 3 and 4 with '55'. Following is the script-

Code:
#!/bin/ksh

position=2
value='55'

nxtpos=4
len_val=2
#head -2 test| tail -1 > temp
x=`perl -pi -e "s/(.{$position})(.*)/\1$value\2/" temp`
echo $x

however on running it, it interprets the \1$value as \155, and replaces nothing.

Could you please suggest what can be done?

Last edited by Franklin52; 06-23-2011 at 03:48 AM.. Reason: Please use code tags, thank you
# 2  
Old 06-22-2011
Code:
position=2
value=55
sed "s/../"$value"/"$position"" temp

# 3  
Old 06-22-2011
Quote:
Originally Posted by verdepollo
Code:
position=2
value=55
sed "s/../"$value"/"$position"" temp

Thanks for the response. it surely does do the job at hand. however this is a simplified scenario. Actually i need to replace positions 255 and above, which is not identified using sed. and this syntax doesn't work with perl. Is there something which can be done using perl?
# 4  
Old 06-22-2011
Code:
position=123
value=456
perl -pi -e "s/(.{"${position}"}).{"${#value}"}(.*)/\1\Q"${value}"\2/g" temp

This User Gave Thanks to verdepollo For This Post:
# 5  
Old 06-22-2011
Setting awk's field separator to "" makes it split a string into individual characters, letting you alter each index individually. awk also has an elegant way to set variables without having to jam them into the expression itself.

Code:
$ echo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
aaaaaaaaaaaaaaaaaa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pectedly find their browsers over a mile wide.

Some versions of awk may be limited to 2048 characters per line. But some versions of sed are too.
# 6  
Old 06-22-2011
Quote:
Originally Posted by verdepollo
Code:
position=123
value=456
perl -pi -e "s/(.{"${position}"}).{"${#value}"}(.*)/\1\Q"${value}"\2/g" temp

Thanks. This does the trick!!

---------- Post updated at 07:09 AM ---------- Previous update was at 01:32 AM ----------

Quote:
Originally Posted by verdepollo
Code:
position=123
value=456
perl -pi -e "s/(.{"${position}"}).{"${#value}"}(.*)/\1\Q"${value}"\2/g" temp

I had to make a small change - terminate the \Q with a \E before the \2 started.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Regex - Capturing groups

I am having trouble with regex capturing groups, For Ex : I am having a file with ABC CDLF SFSDFK PRIMARY INDEX(XYZ,DEF,GHI); XYZ FLJ SDFKLD; PRIMARY INDEX(ABC); BHI SDKFLFLSFD PRIMARY INDEX (QWE , RTY , LHJ); My output should be : ABC XYZ,DEF,GHI XYZ ABC BHI ... (10 Replies)
Discussion started by: ysvsr1
10 Replies

4. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Perl newbie - regex replace all groups issue

Hello, Although I have found similar questions, I could not find advice that could help with my problem. The issue: I am trying to replace all occurrences of a regex, but I cannot make the regex groups work together. This is a simple input test file: The Vedanta Philosophy... (3 Replies)
Discussion started by: samask
3 Replies

6. UNIX for Dummies Questions & Answers

Groups

Must I be in a group? I am using Ubuntu and am the only user on my PC. I know how to change groups but do not see a way to not be in a group. Any help would be appreciated. (2 Replies)
Discussion started by: nthepines
2 Replies

7. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

8. Shell Programming and Scripting

Grep regex matches, groups

Hello, I am searching all over the place for this, just not finding anything solid :( I want to do be able to access the groups that are matched with grep (either with extended regex, or perl compatible regex). For instance: echo "abcd" | egrep "a(b(c(d)))" Of course this returns... (1 Reply)
Discussion started by: Rhije
1 Replies

9. Solaris

groups

1 user in member of 4 groups find file permissions and default group (1 Reply)
Discussion started by: tirupathi
1 Replies

10. AIX

Where are my groups

Hello A couple of weeks ago, I added a user to an AIX 5.3 system. I go to add one today, and it appears that when creating a user in smit, I cannot see any groups. No primary groups No Group set No Admin Groups The /etc/group and etc/secuity/group files seem to be intact. I did... (4 Replies)
Discussion started by: mhenryj
4 Replies
Login or Register to Ask a Question