![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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.... |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
I came up with:
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Aug 2008
Posts: 3
Rep Power: 0
![]() |
Re: sed challange
Simple -
echo ereht ih next challAnge ... |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 137
Rep Power: 0
![]() |
Re: sed challange
Somehow 'echo' doesnt seem quite like a sed script. Although, I have been known to overlook the obvious before...
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Re: sed challange
You're overlooking the obvious and the subtle. Obvious: your script should reverse any input string or file. Subtle: the thread has been dead for 2 years.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Heres the challange | John07 | Visual Basic | 3 | Jan 21st, 2005 11:31 AM |