Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 16th, 2006, 4:50 PM   #1
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 3 jim mcnamara is on a distinguished road
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....
jim mcnamara is offline   Reply With Quote
Old Aug 16th, 2006, 8:43 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,436
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 17th, 2006, 8:46 AM   #3
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 3 jim mcnamara is on a distinguished road
I came up with:
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Heres the challange John07 Visual Basic 3 Jan 21st, 2005 11:31 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:18 PM.

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