swap words in a line with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting swap words in a line with sed
# 1  
Old 08-29-2006
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
# 2  
Old 08-29-2006
why not awk

Have you checked awk..It should be easy with awk..Check awk manual..
# 3  
Old 08-29-2006
Why sed? try this inside your shell script
echo "Enter your string : \c"
read var1 var2 var3 var4
echo "You entred; $var3 $var2 $var1 $var4"
# 4  
Old 09-05-2006
sed "s/^\(.*\) \(.*\) \(.*\)/\3 \2 \1/" file
# 5  
Old 09-05-2006
Quote:
Originally Posted by anbu23
sed "s/^\(.*\) \(.*\) \(.*\)/\3 \2 \1/" file
this won't work if the input has more than 3 words.
# 6  
Old 09-05-2006
Quote:
Originally Posted by atticus
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
if you want it simple, here's a python one

Code:
userinput = raw_input("Enter words: ") #assume eg "word1 word2 word3 word4"
splitted_words = userinput.split()
splitted_words[0], splitted_words[2] = splitted_words[2], splitted_words[0]
print splitted_words

Output:
Code:
bash-2.03$ ./test.py
Enter words: I have a dog
a have I dog

# 7  
Old 09-08-2006
oops i made a mistake. sed "s/^\(.*\) \(.*\) \(.*\)/\3 \2 \1/" file

sed "s/^\([^ ]*\) \([^ ]*\) \([^ ]*\)/\3 \2 \1/"
this will work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

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

3. Shell Programming and Scripting

USING sed to remove multiple strings/words from a line

Hi I use sed comnand to remove occurance of one workd from a line. However I need to removed occurance of dufferent words in ne line. Original-1 Hi this is the END of my begining Comand sed s/"END"/"start"/g Output-1 Hi this is the start of my beginig But I have more... (9 Replies)
Discussion started by: mnassiri
9 Replies

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

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

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

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

8. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

9. Shell Programming and Scripting

deleting blank line and row containing certain words in single sed command

Hi Is it possible to do the following in a single command /usr/xpg4/bin/sed -e '/rows selected/d' /aemu/CALLAUTO/callauto.txt > /aemu/CALLAUTO/callautonew.txt /usr/xpg4/bin/sed -e '/^$/d' /aemu/CALLAUTO/callautonew.txt > /aemu/CALLAUTO/callauto_new.txt exit (1 Reply)
Discussion started by: aemunathan
1 Replies

10. Shell Programming and Scripting

swaping of first 2 words in every line using sed

Please any one can help me how to swap first 2 words in every line in a file using sed Thankk you (4 Replies)
Discussion started by: web123
4 Replies
Login or Register to Ask a Question