A very simple question on SED or AWK for a GURU, and an enjoyable problem 
Author Message
 A very simple question on SED or AWK for a GURU, and an enjoyable problem

Here is the type of lines I have in a file:

junk  label="junk1/junk2/junk3/.../junkn/" more junk

I want to find every line that has

label="..."

pattern

and then I want to replace every / by _ inside the
quotes.

For the purposes of continuity, I want the script to look like this:

cat file |
sed commands |
awk commands |

etc.

I do not care if it all sed or awk or in what order.

Note that the junk is usually alphanumeric with dots etc but no slashes.
So it can be represented by [^/]* if / is considered non-special otherwise
escape it. There may be other /'s on the line outside the pattern
the double quotes starting with label= and they must not be changed.

This problem can be described as making changes to a pattern matching a
regexp. It is not the problem of making changes to a pattern in the whole
line contaning a regexp. That is what is making it difficult for me.
The other reason is that I do not have a definite number of slashes in
the pattern in the single quote otherwise I would use the tagged expression.

I hope you to enjoy this problem. I put on the net only after wrestling it
with some.

gnuist.



Sat, 02 Jul 2005 06:59:25 GMT  
 A very simple question on SED or AWK for a GURU, and an enjoyable problem

Quote:

> Here is the type of lines I have in a file:

> junk  label="junk1/junk2/junk3/.../junkn/" more junk

> I want to find every line that has

> label="..."

> pattern

> and then I want to replace every / by _ inside the
> quotes.

Looks with amazement at the list of newsgroups this one was posted to:
comp.unix.shell,
comp.lang.awk,
comp.unix.programmer,
comp.editors,
alt.comp.editors.batch

WTH is alt.comp.editors.batch?

Follow up to comp.unix.shell since it should fit the best this question.

Bye, Dragan

P.S. If I understand you correctly, grep and tr are your friends.

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole      No it isn't.  L. E. J. Brouwer



Sat, 02 Jul 2005 07:04:56 GMT  
 A very simple question on SED or AWK for a GURU, and an enjoyable problem
The main group for followup to this is: comp.unix.shell.
I hope that I have the relevant groups for cross-posting.
----

Even though the problem posed in this thread is still

*** UNSOLVED ***

let me extend my gratitude to Mr Dragan and Mr Yuan
for their kind attempts to answer this. I hope that
someone is able to put more helpful replies tonight.

Mr Dragan's reply of grep+tr is actually misleading
as concurred by Mr Yuan.

Dr Yuan's reply uses perl which is not familar to me
and I would appreciate a solution that is based on
bash, sed and awk since that is what I can take and
modify at this time.

Note, I CAN write a wrapper loop in bash and I just need
proof of concept for one file processing in this fashion

cat file |
sed or awk |
sed or awk |

and so on.

However, since we are making a replacement within a
pattern, I do not think only grep+tr would help. I would work
to understand a sed solution even if involves shuttling
back and forth data from the pattern space to the
holding space.

Thanks a lot and do not forget to have fun working on
this problem. It is a little out of the way.

Gnuist



Sat, 02 Jul 2005 16:19:52 GMT  
 A very simple question on SED or AWK for a GURU, and an enjoyable problem

Quote:

> Here is the type of lines I have in a file:

> junk  label="junk1/junk2/junk3/.../junkn/" more junk

> I want to find every line that has

> label="..."

> pattern

> and then I want to replace every / by _ inside the
> quotes.

> For the purposes of continuity, I want the script to look like this:

> cat file |
> sed commands |
> awk commands |

> etc.

> I do not care if it all sed or awk or in what order.

> Note that the junk is usually alphanumeric with dots etc but no slashes.
> So it can be represented by [^/]* if / is considered non-special otherwise
> escape it. There may be other /'s on the line outside the pattern
> the double quotes starting with label= and they must not be changed.

There seems to be a contradiction here. First you say that junk will not
contain / but then you say there can be a / outside the pattern.

Here is a sed script.

/label=\".*\"/ {
:loopstartshere
        s:.*\(label=\"[^\"/]*\)/\([^\"]*\):\1_\2:
        t loopstartshere

Quote:
}

First it picks out lines that include label="whatever"
Then, to each of those lines, it substitutes _ for the first / after label="
Then, the t command repeats the process *if* the above substitution occurred
(so the process will stop when there are no more / inside the label="...").

John.



Sat, 02 Jul 2005 17:18:14 GMT  
 A very simple question on SED or AWK for a GURU, and an enjoyable problem

Quote:


> > Here is the type of lines I have in a file:

> > junk  label="junk1/junk2/junk3/.../junkn/" more junk

> > I want to find every line that has

> > label="..."

> > pattern

> > and then I want to replace every / by _ inside the
> > quotes.

> Here is a sed script.

> /label=\".*\"/ {
> :loopstartshere
>         s:.*\(label=\"[^\"/]*\)/\([^\"]*\):\1_\2:
>         t loopstartshere
> }

Note that if you wanted to retain the junk before the label="..." then
the substitution line needs the initial .* inside the tagged regular expression:
s:\(.*label=\"[^\"/]*\)/\([^\"]*\):\1_\2:

John.



Sat, 02 Jul 2005 17:20:25 GMT  
 A very simple question on SED or AWK for a GURU, and an enjoyable problem

Quote:
> junk  label="junk1/junk2/junk3/.../junkn/" more junk
> I want to find every line that has
> label="..."
> pattern
> and then I want to replace every / by _ inside the
> quotes.

Depending on how your 'junk' looks like outside 'label="...."'
this might work for you.

        bash$ cat data
        junk/line
        label="junk/line"
        junk/line

        bash$ sed '/label=/y/\//_/' data
        junk/line
        label="junk_line"
        junk/line
        bash$

//Mats

--
My code (if any) in this message are Copyright (C) 2003 Mats Blomstrand
and licensed under GNU GPL, http://www.gnu.org/licenses/gpl.html



Sat, 02 Jul 2005 23:36:17 GMT  
 A very simple question on SED or AWK for a GURU, and an enjoyable problem

Quote:

> Here is the type of lines I have in a file:

> junk  label="junk1/junk2/junk3/.../junkn/" more junk

> I want to find every line that has

> label="..."

> pattern

> and then I want to replace every / by _ inside the
> quotes.

> For the purposes of continuity, I want the script to look like this:

> cat file |
> sed commands |
> awk commands |

perl -pe's#label="([^"]+)#($a=$1)=~tr!/!_!;$a#eg' file

John
--
use Perl;
program
fulfillment



Mon, 04 Jul 2005 09:12:15 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Sed, Awk Gurus - Your advice required!

2. A very simple question on SED or AWK for a GURU, possibly a lisp script or emacs batch processing of many files

3. Newbie awk (sed??) question, regular expressions

4. Awk/Sed Filehandler question

5. awk or sed: basic? question

6. 2 questions from book sed and awk programming

7. Question: How to remove END OF LINE using AWK or SED

8. awk vs. sed question

9. Sed/awk question

10. Sed/awk question Answered!

11. Looking for a smalltalk Guru to answer some simple questions

12. Two quick and simple Graphics questions for the Assembly Gurus out there (yes from a newbie)

 

 
Powered by phpBB® Forum Software