sed/awk/perl: How to replace all spaces each with an underscore thatoccur before a specific string ?

B

bolega

sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

I really prefer a sed one liner.

Example
Input : This is my book. It is too thick to read. The author gets
little royalty but the publisher makes a lot.
Output: This_is_my_book._It_is_too__thick_to read. The author gets
little royalty but the publisher makes a lot.

We replaced all the spaces with underscores before the first occurence
of the string "to ".

Thanks
Gnuist
 
W

w_a_x_man

sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

I really prefer a sed one liner.

Example
Input :  This is my book. It is too  thick to read. The author gets
little royalty but the publisher makes a lot.
Output: This_is_my_book._It_is_too__thick_to read. The author gets
little royalty but the publisher makes a lot.

We replaced all the spaces with underscores before the first occurence
of the string "to ".

Thanks
Gnuist

awk 'BEGIN{FS=OFS="to "}{gsub(/ /,"_",$1);print}' myfile
 
T

Tim Chase

bolega said:
sed/awk/perl:

Better to post in the "sed" or "perl" mailing lists rather than a
Python list. I saw an awk solution flew by.
How to replace all spaces each with an underscore that occur
before a specific string ?

I really prefer a sed one liner.

Here's a one-liner sed solution:

sed '/to /{s//\n&/;h;s/.*\n//;x;s/\n.*//;s/ /_/g;G;s/\n//}'

There's a reason I prefer Python for these sorts of things:
readability! You win 5 free internets (as a stand-in for the
"real life" you clearly don't have) if you can decipher that in
under 20 seconds ;-)

expecting-to-see-NO CARRIER-after-typing-that'ly yers,

-tkc
+++ATH0
 
J

Jan Kaliszewski

22-08-2009 o 20:11:32 bolega said:
sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

$ rm -rf /home/bolega ; python -c 'for i in xrange(1000): print "I will
never crosspost senselessly."'

;~]
 
E

Ed Morton

sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

I really prefer a sed one liner.
Why?

Example
Input :  This is my book. It is too  thick to read. The author gets
little royalty but the publisher makes a lot.
Output: This_is_my_book._It_is_too__thick_to read. The author gets
little royalty but the publisher makes a lot.

We replaced all the spaces with underscores before the first occurence
of the string "to ".

No, you replaced all ... the string "to " (note the space).

awk '{idx=index($0,"to "); tgt=substr($0,1,idx-1); gsub(/ /,"_",tgt);
print tgt substr($0,idx)}' file

Ed.
 
J

John W. Krahn

bolega said:
sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

I really prefer a sed one liner.

Example
Input : This is my book. It is too thick to read. The author gets
little royalty but the publisher makes a lot.
Output: This_is_my_book._It_is_too__thick_to read. The author gets
little royalty but the publisher makes a lot.

We replaced all the spaces with underscores before the first occurence
of the string "to ".

$ perl -le'
$x = "This is my book. It is too thick to read. The author gets little
royalty but the publisher makes a lot.";
print $x;
$x =~ /to / && substr( $x, 0, $-[0] ) =~ tr/ /_/;
print $x;
'
This is my book. It is too thick to read. The author gets little
royalty but the publisher makes a lot.
This_is_my_book._It_is_too__thick_to read. The author gets little
royalty but the publisher makes a lot.




John
 
M

MRAB

John said:
bolega said:
sed/awk/perl:

How to replace all spaces each with an underscore that occur before a
specific string ?

I really prefer a sed one liner.

Example
Input : This is my book. It is too thick to read. The author gets
little royalty but the publisher makes a lot.
Output: This_is_my_book._It_is_too__thick_to read. The author gets
little royalty but the publisher makes a lot.

We replaced all the spaces with underscores before the first occurence
of the string "to ".

$ perl -le'
$x = "This is my book. It is too thick to read. The author gets little
royalty but the publisher makes a lot.";
print $x;
$x =~ /to / && substr( $x, 0, $-[0] ) =~ tr/ /_/;
print $x;
'
This is my book. It is too thick to read. The author gets little
royalty but the publisher makes a lot.
This_is_my_book._It_is_too__thick_to read. The author gets little
royalty but the publisher makes a lot.
If you're interested in a Python regex solution:

s = "This is my book. It is too thick to read. The author gets little
royalty but the publisher makes a lot."
s = re.sub(".*?(?=to )", lambda m: m.group().replace(" ", "_"), s)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top