Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Sed and Awk (http://www.programmingforums.org/forum22.html)
-   -   sed challange (http://www.programmingforums.org/showthread.php?t=11094)

jim mcnamara Aug 16th, 2006 4:50 PM

sed challange
 
This forum is near death. Maybe some interesting activity will restore it to life.

Challenge - write a simple sed script to reverse a string or file:
:

prompt$> echo "hi there" | sed 'your script goes here'
ereht ih


ie. the behavior should match the rev command utility. I'll post an answer
on Friday....

Infinite Recursion Aug 16th, 2006 8:43 PM

I'd like to see more entries in this section also... maybe a weekly script challenge or something.

:

#!/usr/bin/sed -f

# reverse all chars of each line, keep line ordering

# ignore empty lines, i.e. nothing to reverse
/./!b

# escape ! by doubling it, place markers at beginning and end of line
# the markers are -!- which can never happen after the escaping of !
s/!/!!/g
s/^/-!-/
s/$/-!-/

# swaps first char after first maker, with first char before last marker
# and then advance the markers through the swapped chars
ta
:a
s/-!-\([^!]\|!!\)\(.*\)\([^!]\|!!\)-!-/\3-!-\2-!-\1/
ta

# delete markers, and then unescape the !s
s/-!-//g
s/!!/!/g


ir@grendel [~/code/sed]# echo "hi there" | ./s.sh
ereht ih

jim mcnamara Aug 17th, 2006 8:46 AM

I came up with:
:

sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'


All times are GMT -5. The time now is 9:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC