Swap words using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swap words using sed
# 8  
Old 04-15-2013
Don,

I had essentially pointed out the same thing as you, but the data in my example didn't show up properly aligned. After a few attempts, I removed it. The OP probably noticed the post while it was still there for a few minutes.

Code:
$
$
$ cat f79
egy dfdsf ketto sada
alma:a korte*szilva narancs
$
$ sed 's/^\([a-zA-Z]\+\)\([^a-zA-Z]\+\)\([a-zA-Z]\+\)\([^a-zA-Z]\+\)\([a-zA-Z]\+\)\(.*\)/\5\2\3\4\1\6/' f79
ketto dfdsf egy sada
korte:a alma*szilva narancs
$
$

Although, if the separators are known and the actual words will only consist of "a-zA-Z" (no numbers or underscores, for instance), then you could do something like this -

Code:
$
$ sed 's/^\([a-zA-Z]\+\)\([* ,:]\+\)\([a-zA-Z]\+\)\([* ,:]\+\)\([a-zA-Z]\+\)\(.*\)/\5\2\3\4\1\6/' f79
ketto dfdsf egy sada
korte:a alma*szilva narancs
$
$

This User Gave Thanks to durden_tyler For This Post:
# 9  
Old 04-15-2013
Quote:
Originally Posted by Don Cragun
I'm confused.

You say durden_tyler's example is correct, but there is no example in durden_tyler's posting in this thread.

You said that you want:
, but if all non-alphabetic characters are separators, why shouldn't:
Code:
alma:a korte*szilva narancs

be changed to:
Code:
korte:a alma*szilva narancs

instead of to:
Code:
szilva:a korte*alma narancs

durden_tyler posted an answer, but tis answer was deleted at the time...or I'm not sure about what happened with that.
And sorry, I'm verry tired Smilie I made a terrible mistake...your version is correct.
so finally : alma:a korte*szilva narancs need to be changed to: korte:a alma*szilva narancs

thanks for understanding, and again, apologies for my bad englishSmilie

---------- Post updated at 08:39 PM ---------- Previous update was at 08:07 PM ----------

Quote:
Originally Posted by durden_tyler
Don,

I had essentially pointed out the same thing as you, but the data in my example didn't show up properly aligned. After a few attempts, I removed it. The OP probably noticed the post while it was still there for a few minutes.

Code:
$
$
$ cat f79
egy dfdsf ketto sada
alma:a korte*szilva narancs
$
$ sed 's/^\([a-zA-Z]\+\)\([^a-zA-Z]\+\)\([a-zA-Z]\+\)\([^a-zA-Z]\+\)\([a-zA-Z]\+\)\(.*\)/\5\2\3\4\1\6/' f79
ketto dfdsf egy sada
korte:a alma*szilva narancs
$
$

Although, if the separators are known and the actual words will only consist of "a-zA-Z" (no numbers or underscores, for instance), then you could do something like this -

Code:
$
$ sed 's/^\([a-zA-Z]\+\)\([* ,:]\+\)\([a-zA-Z]\+\)\([* ,:]\+\)\([a-zA-Z]\+\)\(.*\)/\5\2\3\4\1\6/' f79
ketto dfdsf egy sada
korte:a alma*szilva narancs
$
$

Thank you durden_tyler! Your first example works perfectly!
# 10  
Old 04-15-2013
Applying the specification literally, I'd propoese
Code:
$ cat file
korte:a alma*szilva narancs
$ sed -r 's/([[:alpha:]]+)([^[:alpha:]]+)([[:alpha:]]+)([^[:alpha:]]+)([[:alpha:]]+)/\5\2\3\4\1/' file
alma:a korte*szilva narancs

Hoppla - should have read the entire thread - still I think this one will respect locale settings.

Last edited by RudiC; 04-15-2013 at 04:09 PM..
This User Gave Thanks to RudiC For This Post:
# 11  
Old 04-15-2013
T720,
Your specification is not clear as to whether empty strings of alphabetic characters constitute a field. (If the first character on a line is not alphabetic, is the 1st field empty? If there are two non-alphabetic characters together, is there an empty field between them?)

The script durden_tyler provided assumes that there are no empty fields and that all sequences of one or more non-alphabetic characters count as a single field separator. (It also assumes that + is a special character in a sed RE acting to specify one or more of the previous RE. Some versions of sed do that, but it is not portable and does not meet the requirements of the POSIX standards and the Single UNIX Specifications.)

The following sed script will do what you want if empty strings are allowed and each non-alphabetic character is to be treated as a separator:
Quote:
sed 's/^\([[:alpha:]]*\)\([^[:alpha:]][[:alpha:]]*[^[:alpha:]]\)\([[:alpha:]]*\)/\3\2\1/'
When given the input:
Code:
egy dfdsf ketto sada
alma:a korte*szilva narancs
*save3rd remainder
first$second|third\fourth
1x23no change

it produces the output:
Code:
ketto dfdsf egy sada
korte:a alma*szilva narancs
rd*save3 remainder
third$second|first\fourth
1x23no change

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to swap to words in a file?

Hi, I have a file with certain lines in it. i want to swap every occurance of word A with B and every occurrance of word B with A. Example file.txt Unix is a great OS. Linux support graphical user intrface. unix and linux are both same. Output file.txt Linux is a great OS. Unix... (5 Replies)
Discussion started by: Little
5 Replies

2. Shell Programming and Scripting

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: echo Word1Word2Word3Word4 | sed ' /\n/ !G s/\(Word.\)\(.*\n\)/&\2\1/ //D ' This returns: Word4Word3Word2Word1 I'm no sed expert but... (2 Replies)
Discussion started by: Subbeh
2 Replies

3. Shell Programming and Scripting

sed swap lines

Hi, I think it is possible with sed, but I'm not sure... I've a file that contains some text and filenames: gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 8175 | /home/floris/Muziek/Karaoke/1341838939/CB60471-05 - Gilbert, Brantley - Country Must Be... (2 Replies)
Discussion started by: jkfloris
2 Replies

4. Shell Programming and Scripting

Extract data b/w two words using sed

Hi, I want to extract data b/w two words with the help of sed.. Eg: In "Anna said that she would fetch the bucket", I want to display data b/w "Anna" and "would" O/P: said that she I have tried with below code, but unable to get desired o/p echo "Anna said that she would fetch the... (5 Replies)
Discussion started by: divya bandipotu
5 Replies

5. 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

6. UNIX for Advanced & Expert Users

Swap lines using sed

Hi, I have to swap two consecutive line using sed in a file. My text to swap is available in the file x.pl #Create & map a common work library if (!(-e "../work")) { system ("vlib work ../work"); system ("vmap work ../work"); } system ("vsimsa -do thiagu_dec.do"); In this i... (6 Replies)
Discussion started by: adharmalingam
6 Replies

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question