Sed and regex help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed and regex help needed
# 1  
Old 08-08-2008
Sed and regex help needed

Hi all,

I'm writing a script that replaces a value in a file. The file is formatted as follows:

[param_one] [value]
[param_two] [value]

So, for this example, I'd like to replace the value for param_two. The value for param_two can be a one, or two-digit number. It replaces the value in file.cfg, and directs the output to new_file.cfg. $p2 is a variable passed as an arg on the command line. Here's what I have:

Code:
sed -e "/^\[param_two/s/[0-9]/$p2/" file.cfg > new_file.cfg

This code works like a charm if the value for param_two is a one-digit number and $p2 is a one or two-digit number. If the value is a two-digit number, however, my code only replaces only the first digit; which is not what I want. I want to be able to replace the value with whatever is passed for $p2. Any ideas? Thanks in advance.
# 2  
Old 08-08-2008
MySQL

can explain little bit more so that i can try myself..

Thanks
Sha
# 3  
Old 08-08-2008
Try this
Code:
sed -e "/^\[param_two/s/[0-9]\{1,2\}/$p2/" file.cfg > new_file.cfg


Last edited by Ikon; 08-08-2008 at 02:34 PM..
# 4  
Old 08-08-2008
or
Code:
 sed -e "/^\[param_two/s/[0-9]\+/$p2/" file.cfg > new_file.cfg

that will do more than 2 digits

Last edited by Ikon; 08-08-2008 at 02:34 PM..
# 5  
Old 08-08-2008
Quote:
Originally Posted by Shahul
can explain little bit more so that i can try myself..
Sure. You can create file.cfg with these contents:

[param_one] [/usr/local/foo]
[param_two] [23]

Then make a script with this code:

Code:
sed -e '/^\[param_two/s/[0-9]/6/' file.cfg > new_file.cfg

What I'm hoping for here is to replace the value [23] with [6] (Note: I removed the command line arg/variable from earlier, as I'm just trying to make this as simple an example as possible). Thanks a lot.
# 6  
Old 08-08-2008
I changed my code I left out the origional file to read...
Code:
 sed -e "/^\[param_two/s/[0-9]\+/$p2/" file.cfg > new_file.cfg

# 7  
Old 08-08-2008
Quote:
Originally Posted by Ikon
Try this
Code:
sed -e "/^\[param_two/s/[0-9]\{1,2\}/$p2/" file.cfg > new_file.cfg

Thanks a lot! This works exactly like I need it to. For my own edification, what does the new \{1,2\} portion of the regex mean in english? Smilie I really appreciate the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex with sed

hi i would like to say "DATABASENAME=" to "TABLESNAME=" remove "," and press enter myconfig file thanks (1 Reply)
Discussion started by: mnnn
1 Replies

2. Shell Programming and Scripting

Help with sed substitution / regex

Hi all, please can anyone show me how to use sed and regular expressions to achieve the following. If a line contains a capital A followed by exactly 5 or 6 characters followed by an angled bracket then insert an asterix before the angled bracket. So: XCONFIGA12345<X Becomes: ... (5 Replies)
Discussion started by: Jedimark
5 Replies

3. UNIX for Dummies Questions & Answers

RegEX help needed

Hi, Have to filter out string before the last underscore in the following input: UNIX_Solaris_59_KSH output: UNIX_Solaris_59 dummy one but :mad: Thanks & Regards, Sourabh Singh Khichi (4 Replies)
Discussion started by: skhichi
4 Replies

4. UNIX for Dummies Questions & Answers

Regex Needed:(

I am looking for the proper regex to match the hostname "areagc11" of this log.... Any help would be awsome:) Oct 25 11:08:18 areagc11 961: Oct 25 18:08:17.536 GMT: %SYS-5-CONFIG_I: Configured from console by someone onvty1 (10.156.72.97) (6 Replies)
Discussion started by: jlaigo2
6 Replies

5. Shell Programming and Scripting

Help needed in regex

Hi, Could you please help me in writing a regex for the following requirement? Let following be the string format: abc.cdef.ghij.lm I need to check between dots, there is atleast one character{a-z,A-Z,*}. Eg: abc1.gt2.345j is valid, but not 123.abc.vff.gth because 123 should not be... (2 Replies)
Discussion started by: lorzinian
2 Replies

6. Shell Programming and Scripting

perl regex help needed

Hi, I want to validate strings in perl, the string may contains characters from a-zA-Z0-9 and symbols +-_.:/\ To validate such a string I computed a regex if ($string =~ m/^/) { print "valid"; } else { print "invalid"; } but this regex also validates strings that contain... (8 Replies)
Discussion started by: zing_foru
8 Replies

7. Shell Programming and Scripting

Regex help needed

Hello, I'd like to write a regex that transforms a German base form of a noun into one of its inflected forms, namely I want to translate "Haus" to "Häuser" This is what I've got: /^(.+)$/_Umlaut( $1 )_er/ where _Umlaut( x )_ is a function operating on the noun stem captured by $1 The... (1 Reply)
Discussion started by: Bloomy
1 Replies

8. 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

9. Shell Programming and Scripting

sed - using regex and | need help

From my understanding when using regex1|regex2 the matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. When im trying to extract the name from those examples: A) name.can.be.different.20.03.2009.boom B)... (2 Replies)
Discussion started by: TehOne
2 Replies

10. UNIX for Dummies Questions & Answers

sed regex

I would like to do this: replace the word "prod" with the word "special" but it may occur through the file naturally without a command, I only want it to happen when it has a specific command in front of it. The command will always look like this &lt;IMG,###,###,##,&gt;prod/directory/IMG/file ... (4 Replies)
Discussion started by: Shakey21
4 Replies
Login or Register to Ask a Question