Swap words using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swap words using sed
# 1  
Old 04-15-2013
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 [a-zA-Z] intervall.
I hope, you'll understand what I want to do. my english is not so good, sorry for thatSmilie
# 2  
Old 04-15-2013
Can you post your sample data?
# 3  
Old 04-15-2013
example:
Code:
egy dfdsf ketto sada

-->i need to get the line:
Code:
ketto dfdsf egy sada

Code:
alma:a korte*szilva narancs

->i need to get the line:
Code:
szilva:a korte*alma narancs


Last edited by Scrutinizer; 04-15-2013 at 02:46 PM.. Reason: code tags
# 4  
Old 04-15-2013
I guess the first one is easier. Are there any other separators other than <space> ?

Like in your second line, * : are also considered like that
# 5  
Old 04-15-2013
... couldn't align the words properly...

Last edited by durden_tyler; 04-15-2013 at 12:13 PM.. Reason: The forum software messes up the vertical line positions and also the word spacing...
# 6  
Old 04-15-2013
durden_tyler, your example is correct. I have to get something like that

---------- Post updated at 06:35 PM ---------- Previous update was at 06:20 PM ----------

Quote:
Originally Posted by PikK45
I guess the first one is easier. Are there any other separators other than <space> ?

Like in your second line, * : are also considered like that
* ,: are separators

---------- Post updated at 06:49 PM ---------- Previous update was at 06:35 PM ----------

Quote:
Originally Posted by T720
durden_tyler, your example is correct. I have to get something like that

---------- Post updated at 06:35 PM ---------- Previous update was at 06:20 PM ----------



* ,: are separators
any charachter is considered separator, except charachters which are between [a-z] or [A-Z]
# 7  
Old 04-15-2013
Quote:
Originally Posted by T720
durden_tyler, your example is correct. I have to get something like that

---------- Post updated at 06:35 PM ---------- Previous update was at 06:20 PM ----------



* ,: are separators

---------- Post updated at 06:49 PM ---------- Previous update was at 06:35 PM ----------



any charachter is considered separator, except charachters which are between [a-z] or [A-Z]
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:
Quote:
alma:a korte*szilva narancs->i need to get the line: szilva:a korte*alma narancs
, 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

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