Help with sed replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with sed replace
# 1  
Old 04-07-2011
Help with sed replace

Hello,

I have a comman separated file lets day data.txt in following format
Code:
,:000002 CH XIN9I.INDX, 34.7534909645,:000002 CH,:Index XIN9I.INDX
,:000063 CH XIN9I.INDX, 6.3062924781,:000063 CH,:Index XIN9I.INDX
,:000776 CH XIN9I.INDX, 2.7001954832,:000776 CH,:Index XIN9I.INDX

I would like to replace all occurences of [0-9][0-9][0-9][0-9][0-9][0-9] CH with [0-9][0-9][0-9][0-9][0-9][0-9].CH

i.e. following
Code:
:000002.CH XIN9I.INDX, 34.7534909645,:000002.CH,:Index XIN9I.INDX
,:000063.CH XIN9I.INDX,  6.3062924781,:000063.CH,:Index XIN9I.INDX
,:000776.CH XIN9I.INDX,  2.7001954832,:000776.CH,:Index XIN9I.INDX

I tried
Code:
cat data.txt | sed "s/[0-9][0-9][0-9][0-9][0-9][0-9] CH/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].CH/g" > out.txt

and it did not work as expected.

I would really appreciate any help.

Last edited by Franklin52; 04-07-2011 at 02:57 PM.. Reason: Please use code tags
# 2  
Old 04-07-2011
try this,
Code:
sed 's/\(......\) \+CH/\1\.CH/g'  infile

Code:
sed 's/\([0-9]\{6\}\) \+CH/\1\.CH/g' infile

OR
Code:
 perl -pe 's/(.+?)\s+CH/$1.CH/g' infile

# 3  
Old 04-07-2011
Another one:
Code:
sed 's/\([0-9]\{6\}\) /\1./g' file

# 4  
Old 04-07-2011
I tried it, the out put is unchanged, please advise, I would really appreciate it
# 5  
Old 04-07-2011
Or just ... (if apply .... depending on the rest of the file)

Code:
sed 's/ CH/\.CH/g' infile

?
# 6  
Old 04-07-2011
Hi ctsgnb,

Thank you so much it worked, m I reading it correctly that you ares suggesting to replace space CH with dot CH, correct?

Thanks

---------- Post updated at 01:15 PM ---------- Previous update was at 01:13 PM ----------

Now when I think of it, I believe we would want it to be stricter than that

There will be exactly 6-digits then space and CH and requirement is to replace it with 6-digits then dot CH

Please advise, I would appreciate it
# 7  
Old 04-07-2011
Quote:
Originally Posted by srattani
I tried it, the out put is unchanged, please advise, I would really appreciate it
If you're referring to pravin27's sed suggestions, it could be because they use a gnu extension (\+) and you are not using gnu sed.

Regards,
Alister

---------- Post updated at 02:19 PM ---------- Previous update was at 02:17 PM ----------

Quote:
Originally Posted by srattani
Now when I think of it, I believe we would want it to be stricter than that

There will be exactly 6-digits then space and CH and requirement is to replace it with 6-digits then dot CH

Please advise, I would appreciate it
Try:
Code:
sed 's/\([[:digit:]]\{6\}\) CH/\1.CH/g'

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Replace

I have a file whose output words are always like this: aaaa bbbb cccc dddd. Trying to arrange the data so that there are 2 columns such that the 1st word become the 1st column like this: aaaa aaaa aaaa bbbb aaaa cccc aaaa dddd Trying to use awk... (8 Replies)
Discussion started by: jimmyf
8 Replies

2. Shell Programming and Scripting

Sed/replace help

How can we empty or replace with null, following block of code (within the php quotes including the quotes) from inside a file. *** some other data above this code <? #317008# ... (5 Replies)
Discussion started by: fed.linuxgossip
5 Replies

3. Shell Programming and Scripting

sed replace

Hi, i have a file as give below >cat sample_file param1 val1 2012-06-19 ##there can be one or more space after 2012-06-19 in the above file i want to replace val1 with a with value passed through a variable... below is the command i tried >parval='param1 val2' >par1=param1 >sed... (3 Replies)
Discussion started by: midhun19
3 Replies

4. Shell Programming and Scripting

Replace with sed

Hi, I have values in a file at Place $1 as AmericanDollar, AustralianDollar, Singapore1Dollar and so on on various rows. I want to replace Dollar with Pound for all those records where no numeric character is present in the string. Means in above mentioned values 'Singapore1Dollar' should not be... (11 Replies)
Discussion started by: DannyMirashi
11 Replies

5. UNIX for Dummies Questions & Answers

SED Replace

I'm trying to change a "." in a file name with a "_" I have tried; sed -e 's/./_/g' However this then replaces the entire filename with a load of "_" For example; ls /usr/local/feed/service/customers/test1/configs/test1.httpsend | awk -F/ '{print $9}' | tr "" "" | sed -e "s/./_/g" ... (2 Replies)
Discussion started by: JayC89
2 Replies

6. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

7. Shell Programming and Scripting

using sed to replace help

Hi, i am following content in file cat file Install Installation-path variable Now i need to replace Installation-path with some text to be provided as argument in csh script invocation My question is , can i replace this by only using path eg. sed "s/path/$1" file but it... (1 Reply)
Discussion started by: sarbjit
1 Replies

8. UNIX for Dummies Questions & Answers

sed - replace $

my script: amount1=`tail /tmp/file1.txt` amount2=`tail /tmp/file2.txt` sed -e 's/'${amount2}'/'${amount1}'/g' filename1 > filename2 what did i do wrong ? i just want to replace amount1 with amount2 value. (2 Replies)
Discussion started by: tjmannonline
2 Replies

9. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

How to replace using SED?

Hi, I want to change a particular string in a file with another string. This is part of a larger script file. I m using SED for this purpose: sed -e 's/hostname.domainname/${HOST}.${DOMAIN}/g' $sed_file>$tmp_file Where the occurance hostname.domainname has to be replaced with the... (4 Replies)
Discussion started by: mahatma
4 Replies
Login or Register to Ask a Question