LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-31-2006, 09:12 AM   #1
Voetsjoeba
LQ Newbie
 
Registered: Sep 2006
Distribution: SuSE 9.2 Personal?
Posts: 9

Rep: Reputation: 0
Newlines in heredoc


Hi there,

I have to write a shell script ( Solaris 8 s28_38shwp2 SPARC machine ) that will return the arguments that were passed along to it, but in reversed order, eg:

~$ ./reverse abc def ghi
ghi def abc

But since this is for my studies and we technically haven't seen much editors beyond vi/ex and ed, I can only use those. We need to solve this one using ex. Since ex is not a stream editor but a file editor, I figured I'd have to use some temporary file, and use ex on it. So here's my idea:

#!/bin/sh
echo $@ > .reverse.tmp
ex - .reverse.tmp <<HERE
1,1s/ /^V^M/g
# invert the word order here: g/^/.m0
# strip newlines here
wq
HERE
cat .reverse.tmp
rm .reverse.tmp

But the problem here is that no matter what I try, it just won't grab that newline. The words just get concatenated without a newline inbetween each. I don't see how I could avoid using heredoc since it also needs to save the changes, not just perform them. For that I tried 1,1s/ /^V^M/g/w, which also didn't seem to work.

Any help is appreciated, I've been trying to get this working for the past 2 hours and it's getting on my nerves.

Last edited by Voetsjoeba; 10-31-2006 at 09:14 AM.
 
Old 11-01-2006, 07:01 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Does it have to be done in the shell? It's easy in perl...

Code:
#!/usr/bin/perl -w
map { print $_ . " "; } reverse @ARGV;
print "\n";
 
Old 11-01-2006, 11:08 AM   #3
Voetsjoeba
LQ Newbie
 
Registered: Sep 2006
Distribution: SuSE 9.2 Personal?
Posts: 9

Original Poster
Rep: Reputation: 0
Unfortunately, yes :/ It must be a shell script.
 
Old 11-01-2006, 11:27 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Is this something you have been set for homework?
 
Old 11-01-2006, 11:27 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Is this something you have been set for homework?
 
Old 11-01-2006, 12:26 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,310
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
Is this something you have been set for homework?
Apparently it is since he stated
Quote:
Originally Posted by Voetsjoeba
But since this is for my studies
in his OP. Anyway. There is an example in this forum, say within the past 6 months, where somebody had to do the same but without using "rev"...
 
Old 11-02-2006, 05:37 AM   #7
Voetsjoeba
LQ Newbie
 
Registered: Sep 2006
Distribution: SuSE 9.2 Personal?
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn
Is this something you have been set for homework?
Apparently it is since he stated in his OP. Anyway. There is an example in this forum, say within the past 6 months, where somebody had to do the same but without using "rev"...
Any keywords to search for ?
 
Old 11-04-2006, 11:36 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,310
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
Any keywords to search for ?
How about just plain "reverse"? Anyway. This is the thread you're looking for.
 
  


Reply

Tags
doc, here, newline, reverse


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
post edit removes newlines dogpatch LQ Suggestions & Feedback 14 02-05-2006 01:45 PM
Dos to unix newlines Rotwang Linux - General 3 08-22-2005 01:45 PM
Bash: Supress newlines with \c - Doesn't work stefanlasiewski Programming 5 07-26-2005 01:17 PM
Gaim newlines Napalm Llama Linux - Software 5 07-04-2005 02:09 PM
Split a string on newlines (bash) rose_bud4201 Programming 7 04-14-2005 01:58 PM


All times are GMT -5. The time now is 10:05 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration