sed - dynamic search and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - dynamic search and replace
# 1  
Old 12-06-2006
sed - dynamic search and replace

Hi all,

I have a data file formatted as in the following line:
Achadd 0:35 1:35 2:35 3:40 4:40 5:40
I need the minutes converted to seconds; I wrote a script, min2sec, to do so for one datapoint. I was hoping to use sed as in the following code to call this script and reformat the line:
cat datafile.txt | sed 's/\([0-9]*:[0-9]*\)/ $((min2sec \1))/g'
Unfortunately, sed is not calling min2sec. Does anyone have any pointers? I have little experience with sed.

Thanks!
# 2  
Old 12-07-2006
cat datafile.txt | sed "s/\([0-9]*:[0-9]*\)/ $(min2sec \1)/g"

Change the quotes and use `min2sec \1` or $(min2sec \1)
# 3  
Old 12-07-2006
Thanks for the reply. (Where's the "duh!" emoticon when I need one?)

There is still a problem, however.
cat datafile.txt | sed "s/\([0-9]*:[0-9]*\)/`min2sec \1`/g"
the shell does now execute `min2sec \1`, however it does so before sed substitutes "\1" (before sed is even called, I believe). IOW sed is called as in the following line:
cat datafile.txt | sed "s/\([0-9]*:[0-9]*\)/`60`/g"
Do you see any way arround this problem? Is there any way to tell sed to execute a shell command? Or do I need to look for a solution outside of sed?

Thanks!

Last edited by x-375HK-x; 12-07-2006 at 04:57 PM..
# 4  
Old 12-07-2006
I looked to awk to solve my problem instead. The following code converts lines 1-4 into lines 5-7, which was the application I need. I'll just post the code here in case it helps anyone.
1. Brd02_11_12_18_31
2. conc 10^{-24} 10^{-23} 10^{-22} 10^{-21} 10^{-20} 10^{-19} 10^{-18}
3. Achadd 1:00 1:45 3:00 4:00 5:15 6:15 7:15
4. Remove 2:45 3:45 4:45 6:00 7:00

5. conc = [ 10^{-24} 10^{-23} 10^{-22} 10^{-21} 10^{-20} 10^{-19} 10^{-18} ];
6. Achadd = [ 60 105 180 240 315 375 435 ];
7. Remove = [ 165 225 285 360 420 ];
awk -f fdataf.awk board=Brd02_11_12_18_31 < timedata.txt

where fdataf.awk is the following:
Code:
$1 == board, stop == 3 {
   if ($1 != board)
   {
   printf "%s = [ ", $1;
      for (i = 2; i <= NF; i++) {
        n = split($i,x,":");
        if ( n == 1 ) {
          printf "%s ", $i;
       }
       else
       {
         printf  "%d ", x[1]*60 + x[2];
       }
     }
     printf "];\n"
   }
   stop++ 
}

I'm still interested in knowing whether it's possible to call a shell script from within awk or sed, however the above work-arround solves my problem for now. Thanks for the help.

Last edited by reborg; 12-07-2006 at 07:50 PM..
# 5  
Old 12-08-2006
Quote:
Originally Posted by x-375HK-x
Thanks for the reply. (Where's the "duh!" emoticon when I need one?)

There is still a problem, however.
cat datafile.txt | sed "s/\([0-9]*:[0-9]*\)/`min2sec \1`/g"
the shell does now execute `min2sec \1`, however it does so before sed substitutes "\1" (before sed is even called, I believe). IOW sed is called as in the following line:
cat datafile.txt | sed "s/\([0-9]*:[0-9]*\)/`60`/g"
Do you see any way arround this problem? Is there any way to tell sed to execute a shell command? Or do I need to look for a solution outside of sed?

Thanks!
I think it is not possible to send matched pattern ( \1,\2,.. ) to shell script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic search and replace

Hi, I am trying to search and replace a value in a file. File contains data which is stated below ruby ./scripts/CreateUsage.rb Cloud_Computing_001 Cloud_Computing_001 is the 3rd column of the file. I want to replace this Cloud_Computing_001 with the runtime value. Next time i want to... (4 Replies)
Discussion started by: Chandresh Kumar
4 Replies

2. Shell Programming and Scripting

Need help with search and replace using SED

Hi guys, thanks for accepting me in your forum .. I am trying to clean some hacked PHP files using SSH .. I am using this command: find . -type f -print0 | xargs -0 sed -i '/god_mod/d' <?php ... (3 Replies)
Discussion started by: wisam74us
3 Replies

3. UNIX for Dummies Questions & Answers

search and replace with sed

Hi All I have a simple text file and I want to be able to replace any alpha character and comma combination with any other symbol of my choice here is the text file I want to replace: pear apple ban,ana grape ",g1234," te,st1 here is how it should look afterwards: pear... (4 Replies)
Discussion started by: greg_b
4 Replies

4. Shell Programming and Scripting

Sed - search and replace help.

Hi everyone, basically I am been cleaning data by using simple sed commands So what i have below has been working for me. sed 's/="//g' trade.csv > tradeb.csv sed 's/"//g' tradeb.csv > trade2.csv but now i don't want to remove all the quotes just the ones if i encounter this ... (1 Reply)
Discussion started by: raz0r
1 Replies

5. UNIX for Dummies Questions & Answers

How to use 'sed' to search and replace?

Hello - I have a very large file in which a certain numbers are repeated. I find that using vi to edit the entire file is useless. How should i use sed to find a replace such as this text: To replace: 145.D25.D558 With: 215.22.45.DW I tried this command: sed... (4 Replies)
Discussion started by: DallasT
4 Replies

6. Shell Programming and Scripting

sed search and replace

hi, im new for sed, anyone can help me to these in sed command my output file.txt "aaa",a1,bbb "ddd",a1,ccc "eee",a1,www need to change a1, to "a1"," output i need "aaa","a1","bbb "ddd","a1","ccc "eee","a1","www thanks in advance fsp (2 Replies)
Discussion started by: fspalero
2 Replies

7. UNIX for Dummies Questions & Answers

Search/Replace with Sed

Is there a way to use the sed command to 1) search a specified pattern 2) in the line where that pattern is found, replace from character N to character N+4 with a new 4-character string. Thks in advance! (5 Replies)
Discussion started by: mvalonso
5 Replies

8. UNIX for Dummies Questions & Answers

sed search and replace

Hello Folks, Anyone know how I can replace this line in file.xml <oacore_nprocs oa_var="s_oacore_nprocs">8</oacore_nprocs> with this line <oacore_nprocs oa_var="s_oacore_nprocs">1</oacore_nprocs> using sed or awk ? Thanks for your time. Cheers, Dave (7 Replies)
Discussion started by: d__browne
7 Replies

9. Shell Programming and Scripting

Search and replace sed or tr

Hi folks, I need to search and replace specific text in a file and replace it. I have a text file that does not have any newlines or carriage returns. All newlines have been removed. Here is what I need to do. Find the exact string “DH” (quotes included) and replace it with \n”DH” (basically... (6 Replies)
Discussion started by: bridgeje
6 Replies

10. Shell Programming and Scripting

search and replace dynamic data in a shell script

Hi, I have a file that looks something like this: ... 0,6,256,87,0,0,0,1187443420 0,6,438,37,0,0,0,1187443380 0,2,0,0,0,10,0,1197140320 0,3,0,0,0,10,0,1197140875 0,2,0,0,0,23,0,1197140332 0,3,0,0,0,23,0,1197140437 0,2,0,0,0,17,0,1197140447 0,3,0,0,0,17,0,1197140543... (8 Replies)
Discussion started by: csejl
8 Replies
Login or Register to Ask a Question