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
Have you checked awk..It should be easy with awk..Check awk manual..
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"
sed "s/^\(.\) \(.\) \(.*\)/\3 \2 \1/" file
this won't work if the input has more than 3 words.
if you want it simple, here's a python one
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:
bash-2.03$ ./test.py
Enter words: I have a dog
a have I dog
oops i made a mistake. sed "s/^\(.\) \(.\) \(.*\)/\3 \2 \1/" file
sed "s/^\([^ ]\) \([^ ]\) \([^ ]*\)/\3 \2 \1/"
this will work.
i think Andrek is right. Why to use SED and AWK when you simply swap 1 with 3.
awk ' { temp=$3;$3=$1;$1=temp; print $0 } ' file
Let sed be the file ...
vi sed
"sed" 2 lines, 72 characters
sunday is a holiday
monday is a holiday
Run this command:
sed 's/\([a-z]\) \([a-z]\) \([a-z]\) \([a-z]\)/\3 \2 \1 \4/' sed > sed1.txt
Output is :
vi sed1.txt
"sed1.txt" 4 lines, 72 characters
a is sunday holiday
a is monday holiday