LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-27-2013, 06:07 AM   #1
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Rep: Reputation: Disabled
sed reverse output


Code:
[ssh@Nick ~]$ echo "123456789abcdefg" | sed ':a; s/\(\w\{1\}\)\(\w\{1\}\)/\2 \1/;ta'
2 3 4 5 6 7 8 9 a b c d e f g 1
[ssh@Nick ~]$ echo "123456789abcdefg" | sed ':a; s/\(\w\{1,\}\)\(\w\{1\}\)/\2 \1/;ta'
g f e d c b a 9 8 7 6 5 4 3 2 1
Hi,I want to reverse the content of echo,but I am wonder why it cannot work when use {1} ? and how can I remove the space between each characters as it turn out to standby while I use "\2\1" instead of "\2 \1"
 
Old 03-27-2013, 06:12 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Have you considered
Code:
echo "123456789abcdefg" | rev
? No point in reinventing the wheel.

If you look at what the first one does, you get:

Code:
123456
2 13456
2 3 1456
...
2 3 4 5 6 1
because at each stage you're swapping the first two characters and inserting a space.

The second one, however, does:

Code:
123456
23456 1
3456 2 1
...
6 5 4 3 2 1
Finally, if you don't have the space, it just repeatedly swaps the first two characters and never terminates:

Code:
123456
213456
123456
213456
...
Hope this helps,

Last edited by Snark1994; 03-27-2013 at 06:17 AM. Reason: Added explanation of what was wrong with the sed commands
 
1 members found this post helpful.
Old 03-27-2013, 06:25 AM   #3
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
Have you considered
Code:
echo "123456789abcdefg" | rev
? No point in reinventing the wheel.

If you look at what the first one does, you get:

Code:
123456
2 13456
2 3 1456
...
2 3 4 5 6 1
because at each stage you're swapping the first two characters and inserting a space.

The second one, however, does:

Code:
123456
23456 1
3456 2 1
...
6 5 4 3 2 1
Finally, if you don't have the space, it just repeatedly swaps the first two characters and never terminates:

Code:
123456
213456
123456
213456
...
Hope this helps,
so great,you have solved my question,thank you very much
 
Old 03-28-2013, 12:10 AM   #4
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
Have you considered
Code:
echo "123456789abcdefg" | rev
? No point in reinventing the wheel.

If you look at what the first one does, you get:

Code:
123456
2 13456
2 3 1456
...
2 3 4 5 6 1
because at each stage you're swapping the first two characters and inserting a space.

The second one, however, does:

Code:
123456
23456 1
3456 2 1
...
6 5 4 3 2 1
Finally, if you don't have the space, it just repeatedly swaps the first two characters and never terminates:

Code:
123456
213456
123456
213456
...
Hope this helps,
hello,can I do anything to remove the space between each output ? I dont want to pipe out and tr -d
 
Old 03-28-2013, 05:24 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
What about

Code:
echo "123456789abcdefg" | rev
?
 
Old 03-28-2013, 11:21 PM   #6
freebsd_Rules_All_OSes
LQ Newbie
 
Registered: Mar 2013
Posts: 19

Rep: Reputation: Disabled
Quote:
Originally Posted by nicksu View Post
hello,can I do anything to remove the space between each output ? I dont want to pipe out and tr -d
If you want to use your original code and remove any spaces between characters and not use pipe and the tr command, do this

Code:
echo "123456789abcdefg" | sed ':a; s/\(\w\{1,\}\)\(\w\{1\}\)/\2 \1/;ta;s/ //g'
Just add ;s/ //g' in bold as in above and the output becomes

gfedcba987654321

Last edited by freebsd_Rules_All_OSes; 03-28-2013 at 11:23 PM.
 
Old 03-29-2013, 12:09 AM   #7
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by freebsd_Rules_All_OSes View Post
If you want to use your original code and remove any spaces between characters and not use pipe and the tr command, do this

Code:
echo "123456789abcdefg" | sed ':a; s/\(\w\{1,\}\)\(\w\{1\}\)/\2 \1/;ta;s/ //g'
Just add ;s/ //g' in bold as in above and the output becomes

gfedcba987654321
Thank you for your reply,but I am wonder is there any differ between " sed -e ':a; s/\(\w\{1,\}\)\(\w\{1\}\)/\2 \1/;ta' -e 's/ //g'" and the command you provide ?
 
Old 03-29-2013, 12:10 AM   #8
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
What about

Code:
echo "123456789abcdefg" | rev
?
yes,it work.but just something for sed learning.thank you
 
Old 03-29-2013, 12:40 AM   #9
freebsd_Rules_All_OSes
LQ Newbie
 
Registered: Mar 2013
Posts: 19

Rep: Reputation: Disabled
Code:
echo "123456789abcdefg" |  sed -e ':a; s/\(\w\{1,\}\)\(\w\{1\}\)/\2 \1/;ta' -e 's/ //g'
Code:
echo "123456789abcdefg" | sed ':a; s/\(\w\{1,\}\)\(\w\{1\}\)/\2 \1/;ta;s/ //g'

These two will produce the same output. No difference whether you use -e or not.

The ; is just a continuous of the sed command and it is also used to combine multiple commands in one line. For example

ls -l; cal

Linux is very versatile. There is more than one way to do the same tasks with less coding. It is based on your knowledge of linux and script writing and everything in between.

Hope this helps

Last edited by freebsd_Rules_All_OSes; 03-29-2013 at 12:47 AM.
 
Old 03-29-2013, 08:19 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
When it comes to sed (and some other programs), a lot of the flexibility you can do on Linux comes from extensions added by the gnu project. Implementations on other Unixes often don't have the same range of features and abilities. Some other versions of sed require using separate "-e" expressions in looping commands like this, for example.

http://sed.sourceforge.net/sedfaq6.html#s6.7.1

Be aware too that the ';' in sed's expression syntax is different from the shell's command separator, although it is analogous to it.
 
  


Reply


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
Reverse the order of lines using sed Karljoe2 Programming 4 12-26-2012 11:38 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 06:50 PM
[ask] ifconfig output reverse demhyt Linux - Networking 4 09-21-2008 08:06 PM
C# reverse output? itz2000 Programming 2 05-17-2006 11:31 AM
weird sed output schneidz Programming 3 05-19-2005 12:16 PM


All times are GMT -5. The time now is 04:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration