sed substitution problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed substitution problem
# 1  
Old 02-12-2009
sed substitution problem

Can anyone please help me on this.

i have a file with lines say

X X3200 X
X X
X2400 X X4100

I want to use sed to put the numbers in braces.
the output should be like,

X X(3200) X
X X
X(2400) X X(4100)
# 2  
Old 02-12-2009
Code:
sed -e 's/\([0-9]\+\)/(\1)/g'

# 3  
Old 02-12-2009
Quote:
Originally Posted by pludi
Code:
sed -e 's/\([0-9]\+\)/(\1)/g'

Can you pls explain ur code???? especially the usage of +\ and (\1)

TIA
# 4  
Old 02-13-2009
Quote:
Originally Posted by pludi
Code:
sed -e 's/\([0-9]\+\)/(\1)/g'

this code is not working. anyways thanks for the reply
# 5  
Old 02-13-2009
What exactly do you mean with "It's not working"? Does sed display an error, or does it not do what you want? If the latter, what should it do? I'm getting (for your sample input)
Code:
$ cat test.txt
X X3200 X
X X
X2400 X X4100
$ sed -e 's/\([0-9]\+\)/(\1)/g' test.txt > test2.txt
$ cat test2.txt
X X(3200) X
X X
X(2400) X X(4100)
$

Explanation: it's a regex. the '\(\)' tells sed to start/end a capturing group (without the backslash it would treat them as literals), capturing all digits that occur one or more times consecutively (\+, same rule as for the parentheses). In the substitution, \1 refers to the contents of the first capturing group, which is to be enclosed by (). The 'g' at the end means "global", so sed will check the entire line instead of stopping after the first occurrence.
# 6  
Old 02-13-2009
Quote:
Originally Posted by pludi
What exactly do you mean with "It's not working"? Does sed display an error, or does it not do what you want? If the latter, what should it do? I'm getting (for your sample input)
Code:
$ cat test.txt
X X3200 X
X X
X2400 X X4100
$ sed -e 's/\([0-9]\+\)/(\1)/g' test.txt > test2.txt
$ cat test2.txt
X X(3200) X
X X
X(2400) X X(4100)
$

Explanation: it's a regex. the '\(\)' tells sed to start/end a capturing group (without the backslash it would treat them as literals), capturing all digits that occur one or more times consecutively (\+, same rule as for the parentheses). In the substitution, \1 refers to the contents of the first capturing group, which is to be enclosed by (). The 'g' at the end means "global", so sed will check the entire line instead of stopping after the first occurrence.

I tried this command in my system and its not recognising the \+\ .
the output file remains unaltered.its not capturing one or more digits.
if i use just [0-9] its working on a single digit. ex
X X(3)200 X

can u plz solve this problem.i dunno wats wrong with this.
# 7  
Old 02-13-2009
Quote:
Originally Posted by diliphp
[...]its not recognising the \+\ .
Ok, then this will probably not be solvable with sed, except when your numbers have a fixed length (eg. always 4 numbers, as in your example). In this case you'll only have to repeat [0-9] as often as there are numbers.

For a more flexible approach, try this
Code:
$ perl -i.bak -p -e 's/(\d+)/($1)/g' infile

This will make a backup copy named infile.bak and change your file in-place.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with substitution in sed??

Hi alll I have a file with this content ale,AAM,id2_1_2_1_1,23,2013-01-30,1,1 ale,BAND,id2_1_1_1_1,31,2013-01-28,1,1 ale,CAI,id2_1_1_1_1,23,2013-01-28,1,1 ale,CAI,id2_1_1_1_4,23,2013-01-28,1,1 ale,CAI,id2_1_2_1_1,23,2013-01-30,2,1 ale,NAST,id2_1_1_1_1,23,2013-01-29,1,1... (1 Reply)
Discussion started by: nikhil jain
1 Replies

2. Shell Programming and Scripting

sed substitution problem

I'm writing my first script and I have all the other commands I'm using working properly, but I'm having one final issue with a sed. I've looked on the forums for some answers, which have been close but sed is quite confusing to me. My problem is that I have a file called CylinderAtom1.txt... (3 Replies)
Discussion started by: 1Aegis1
3 Replies

3. Shell Programming and Scripting

Substitution with sed

I have a file with some numbers having single quotes around them which I want to remove. i.e. '923930' -> 23930 If it can be done without using sed thats fine. I have tried with sed but can't think how to replace this pattern on only the numbers (13 Replies)
Discussion started by: user_invalid
13 Replies

4. UNIX for Dummies Questions & Answers

Help with sed substitution

I'm a noob to unix, and I have a line of data like the following: title=Boston|tcolor=green|desc=Large city in New England|url=www.boston.com Is there a way to change a field value with sed substitution? (i.e. change tcolor=green to tcolor=blue) I figured out: sed... (19 Replies)
Discussion started by: stabby
19 Replies

5. Shell Programming and Scripting

sed substitution

Using sed I'm trying to replace 'string' with ']' while retaining case and ignoring words with 'string' in it along with additional characters like 'strings' and those which already contain the ] wrapper. I'm hoping to do it with sed and the right expression, if possible. Example: Apple... (2 Replies)
Discussion started by: tom.lee
2 Replies

6. Shell Programming and Scripting

sed variable substitution problem

Hi, I am facing a strange problem. I have a script that used the following to search and replace text: sed 's/'"${find_var_parm}"'/'"${find_var_filter}"'/g' $ParmFile > $TempFile The values of $find_var_parm and $find_var_filter are set based on search criteria. The above seems to be working... (2 Replies)
Discussion started by: arsh
2 Replies

7. Shell Programming and Scripting

sed substitution

Hi I am trying to do a text insertion in a text file at a particular line number in a shell script. However its not working. sed '122i\ > for j in \`echo $MyList\` ; do perl -pi -e\'s#01\/01\/2009#01\/01\/2011#\' $j ; done' $HOME/MyScript.ksh The Actual line to be inserted at line 122... (5 Replies)
Discussion started by: som.nitk
5 Replies

8. Shell Programming and Scripting

Help with sed/substitution!

I have file.txt 1 4 7 9 3 I want to replace the tabs with a space, but my code doesn't work. cat file.txt | sed 's/"\t"/ /g' > t.txt But file is still the same. Numbers seperated by tabs instead of spaces. Help? (2 Replies)
Discussion started by: Bandit390
2 Replies

9. Shell Programming and Scripting

Problem with sed string substitution

Hi, heres my problem: echo "aaaa(aaaa(aaa" | sed 's/a.*(//g' gives aaa but it should give aaaa(aaa .*( should find any string to the appearance of (, but it finds any string to the last appearance, any idea why, and how to do this? and what if the string ist... (2 Replies)
Discussion started by: funksen
2 Replies

10. UNIX for Dummies Questions & Answers

Substitution using sed

I know we can substitute a string using sed but how? For example: sed 's/(old variable)/(new variable)/ details.dat Am I suppose to put $old variable or whatever? Because I tried many times, it didnt work by putting $old variable. Am I suppose to enclose it with "" or ''? Please help (3 Replies)
Discussion started by: Ohji
3 Replies
Login or Register to Ask a Question