Reverse words with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reverse words with sed
# 1  
Old 04-29-2014
Reverse words with sed

Hi guys,

I'm wondering what the best way would be to reverse words in a string based on certain characters.

For example:
Code:
echo Word1Word2Word3Word4 | sed '
       /\n/ !G
       s/\(Word.\)\(.*\n\)/&\2\1/
       //D
     '

This returns:
Code:
Word4Word3Word2Word1

I'm no sed expert but this is what I made and does the job, I just wonder if there's an other/better way to achieve the same

Last edited by Subbeh; 04-29-2014 at 08:36 AM.. Reason: mixed up 'word' and 'line' in the output
# 2  
Old 04-29-2014
This is fine - the only issue might be for maintenance, someone unfamiliar with sed would have issues learning what the code really does.

In the case where you need to reverse the words in a sentence, awk might be clearer--
ex:
Code:
echo 'This is a sentence'  | awk '{for(k=NF; k; k--) {printf("%s ", $(k)) }; print ""}'

The fact is you can do most text manipulations several ways in UNIX. And someone could make this awk more concise - but harder to interpret.

Linux (GNU coreutils) has special commands for text "reversal": tac and rev come to mind.

PS: your definition of reversal in your example is not what some developers probably have, and the same goes for "words" which are normally white space delimited, e.g., English.

Last edited by jim mcnamara; 04-29-2014 at 09:12 AM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-29-2014
Thanks Jim, although it's not exactly what I was looking for.

I know the awk, tac and rev solutions but was especially interested in getting a good sed example for educational purposes as it was harder to create than I thought it would be.
Also note that there is no delimiter between the words in the string.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Swap words using sed

Hi. I have to swap the first and the third word in all lines of a txt file using sed. Separators between words are: any charachter, except intervall. I hope, you'll understand what I want to do. my english is not so good, sorry for that:) (10 Replies)
Discussion started by: T720
10 Replies

2. Shell Programming and Scripting

SED - delete words between two possible words

Hi all, I want to make an script using sed that removes everything between 'begin' (including the line that has it) and 'end1' or 'end2', not removing this line. Let me paste an 2 examples: anything before any string begin few lines of content end1 anything after anything before any... (4 Replies)
Discussion started by: meuser
4 Replies

3. Shell Programming and Scripting

Help with sed/awk for reverse search and print

I have a file which is DFDG START DSFDS DSDS XXX END (VIO) AADD START SDSD FGFG END and I have to print the lines between START and END (VIO). In the files there are multiple places where START would be followed by END with few lines in between but I need to print only if START is... (18 Replies)
Discussion started by: pgbuddy
18 Replies

4. Shell Programming and Scripting

sed append words

Hi all, I have a file like one two three for five six seven eight ..... Actually i need to append a label to the words that belong to the 2 column and get: one two_label three for five six_label seven eight .... I was trying with sed inside vim but I can't figure out... (9 Replies)
Discussion started by: Dedalus
9 Replies

5. Shell Programming and Scripting

output words between sentences SED

Hi, i need to delete every thing ecept sentences between known prases lets say i have Thu Dec 4 08:28:57 2008 : Auth: Login OK: (from client LINKSYS3 port 12 cli 001644fc4838) i need information between Login OK: and ] (from what is vyce6220. between client and port between cli... (15 Replies)
Discussion started by: wrwe
15 Replies

6. Shell Programming and Scripting

swap words in a line with sed

Hello. There is something I can not manage : I want to swap the first word with the third one in every line. No file is given the input is read from the keyboard. I know I have to use sed, but it seems this is too complicated for me. Could you help me please ? Thanks, atticus (9 Replies)
Discussion started by: atticus
9 Replies

7. Shell Programming and Scripting

search two words in sed

I've following sed command working fine - sed '/search_pattern1/ !s/pattern1/pattern2/" file Now, I want to search two patterns - search_pattern1 and search_pattern2 . How can put these into above sed statement ? Thanks in advance. (12 Replies)
Discussion started by: ajitkumar2
12 Replies

8. Shell Programming and Scripting

reverse tokens with sed

I currently use this bash for loop below to reverse a set of tokens, example "abc def ghi" to "ghi def abc" but in looking at various sed one liner postings I notice two methods to reverse lines of text from a file (emulating tac) and reversing letters in a string (emulating rev) so I've spent some... (1 Reply)
Discussion started by: markc
1 Replies

9. UNIX for Dummies Questions & Answers

sed [delete everything between two words]

Hi, I have the following codes below that aims to delete every words between two pattern word. Say I have the files To delete every word between WISH_LIST=" and " I used the below codes (but its not working): #!/bin/sh sed ' /WISH_LIST=\"/ { N /\n.*\"/ {... (3 Replies)
Discussion started by: Orbix
3 Replies

10. UNIX for Dummies Questions & Answers

using sed and regex to reverse order???

so i have been trying to learn how to manipulate text on my own and have gotten stumped... let's say i have a text file that says (highly simplified): people ordinary How would swap the order of the words.. I know i need to use sed and some kind of back reference but cannot make it... (2 Replies)
Discussion started by: urtherhoda
2 Replies
Login or Register to Ask a Question