LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2009, 07:06 AM   #1
Willard
LQ Newbie
 
Registered: Nov 2009
Posts: 17

Rep: Reputation: 0
Question Reversing a string using only commands from the SUS (Single UNIX Specification)


Greetings.

I would like to reverse a string using only the tools which are part of the Single UNIX Specification (POSIX). The list of UNIX utilities in that specification can be seen on Wikipedia:

http://en.wikipedia.org/wiki/List_of_Unix_utilities

In Linux and BSD I can just use the command

Code:
rev
and feed input to it through standard input. However, 'rev' is not part of SOS.

So far, my best proposal is the following.

Code:
awk '{for(i=length($0);i>0;i--){printf("%c",substr($0,i,1))}}'
This program reverses the text provided to it on standard input, although it removes linebreaks if present.

In my specific scenario, I have a single word consisting of English characters only, with no spaces, punctuations, or hyphens.

I think the code looks hideous, and I have not found a more elegant way in my brief search. Any ideas?

Thanks in advance
Willard.
 
Old 11-17-2009, 07:15 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
gawk
Code:
$ echo "abcdefghij" | awk -vFS= '{for(i=NF;i>0;i--)printf $i}'
jihgfedcba
 
Old 11-17-2009, 08:05 AM   #3
Willard
LQ Newbie
 
Registered: Nov 2009
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
gawk
Code:
$ echo "abcdefghij" | awk -vFS= '{for(i=NF;i>0;i--)printf $i}'
jihgfedcba
Ah, much better. Neat trick

Both this script, and my script, will reverse lines at a time, and remove line breaks. For completeness and elegance, it would be nice if the script would just consider the entire input stream as a single token, and reverse it, instead of treating linebreaks in a special manner.

If I remember correctly, it should be possible to re-define how a stream is tokenized. I cannot remember how, though.

A challenge: Write the shortest SUS-compliant program which reverses a string.
 
Old 11-17-2009, 08:21 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ more file
foo
bar
foo1
bar1

$ awk -vFS= -vRS= '{for(i=NF;i>0;i--)printf $i}' file
1rab
1oof
rab
oof
 
Old 11-17-2009, 10:03 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by Willard View Post
If I remember correctly, it should be possible to re-define how a stream is tokenized. I cannot remember how, though.
it's IFS but it won't split words into characters.

here's a neat trick:

Code:
(IFS=:;ls -1 $PATH)
 
Old 11-17-2009, 10:26 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Well, C99 is part of the SUS!

I think using awk is cheating

Can't cope with newlines but...

Code:
$ uname  -inps                                           
FreeBSD foghorn amd64 GENERIC

$ uname  -inps | fold -1 | nl | sort -rn | cut -f2 | tr -d  '\n'
CIRENEG 46dma nrohgof DSBeerF$
wow 10+ years and I never heard of fold or nl before.

cheers OP.
 
1 members found this post helpful.
Old 11-17-2009, 11:30 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
This is longer, but uses pure shell:
Code:
FILE_LINE_COUNT=0
LINE=
IFS=
while read LINE ; do
 # add the curent line to the line counter
 (( FILE_LINE_COUNT++ ))
 OUTPUT_STRING=
 STRING=$LINE
 while [[ "$STRING" != "" ]] ; do
  OUTPUT_STRING=${STRING:0:1}$OUTPUT_STRING
  STRING=${STRING:1}
 done
echo $OUTPUT_STRING
done <"$1"
Actually this uses bash, but if that particular construct doesn't work i think it can still be done with just the shell using another construct.
That code is part of a fuller file which constitutes a pretty complete implementaion of rev using pure bash.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C: recommended string handling commands? Completely Clueless Programming 9 08-21-2009 03:48 AM
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 05:57 PM
minicom - send string or single ascii character neutron001 Linux - Software 1 04-10-2008 07:08 PM
Compare first characters of a string in UNIX shell loopoo Linux - Newbie 8 12-11-2006 11:35 AM
Perl : Changing a single char in a string richhill Programming 2 09-17-2003 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration