Cutting text from one location to another

The following text appears multiple times in a script and it resides in many scripts.
I would like to make the following changes to the text:

Text:
/usr/local/bin/gmail -t abc@yahoo.com -m "this is a test" -f xyz@yahoo.com

I need to cut the text that comes after -m which is "this is a test" and place it in front of the line with echo statment.
I think Awk will not work as -m will not be in the same column everytime.

final result:

echo "this is a test" | /usr/local/bin/gmail -t abc@yahoo.com -m -f xyz@yahoo.com

Any suggestions?

Thank you.

Here's a possible awk solution that seems to work, but makes assumptions about the input data:

Input:
this is a line foo
/usr/local/bin/gmail -t abc@yahoo.com -m "this is a test" -f xyz@yahoo.com
this is a line bar
this is a line baz
awk '/^\/usr\/local\/bin\/gmail/ { 
	match($0,/\"..*\"/)
	print "echo", substr($0,RSTART,RLENGTH), "|", $1, $2, $3, $4, $(NF-1), $NF
	next
     }
     {
	print
     }' inputfile.txt
Output:
this is a line foo
echo "this is a test" | /usr/local/bin/gmail -t abc@yahoo.com -m -f xyz@yahoo.com
this is a line bar
this is a line baz

Thanks

but, this just prompts the result on the screen. I need to replace it in the file that has the input text string.

I was trying to put your solution in a variable and replace it with changed text, but not successful.

Plus as mentioned earlier it is not a fixed length text, -m could be in different column.

:confused:

Just use i/o redirection and a temp file:

awk '/^\/usr\/local\/bin\/gmail/ { 
	match($0,/\"..*\"/)
	print "echo", substr($0,RSTART,RLENGTH), "|", $1, $2, $3, $4, $(NF-1), $NF
	next
     }
     {
	print
     }' inputfile.txt > inputfile.txt.tmp
mv inputfile.txt.tmp inputfile.txt

I don't see how the variability of the position of "-m" matters. Awk is just performing the translation on any line beginning with "/usr/local/bin/gmail".

Sorry, I think was not clear enough.

When I mean "-m" being variable in position is by:

/usr/local/bin/gmail -t abc@yahoo.com -m "this is a test" -f xyz@yahoo.com

/usr/local/bin/gmail -t abc@yahoo.com -m "this is a test" -f xyz@yahoo.com -a /home/users/note1.txt

/usr/local/bin/gmail -t abc@yahoo.com pqr@yahoo.com -m "this is a test" -f xyz@yahoo.com

/usr/local/bin/gmail -t abc@yahoo.com -a /home/users/note1.txt -m "this is a test" -f xyz@yahoo.com

Now when we print,
print "echo", substr($0,RSTART,RLENGTH), "|", $1, $2, $3, $4, $(NF-1), $NF

we are only printing first 4 columns and then second last and last column of the original text.

If I use the above input strings, the print statement will not give what is expected.

echo /usr/local/bin/gmail -t abc@yahoo.com -m "this is a test" -f xyz@yahoo.com | sed 's/\(.*-m \)\(.*\)\( -f.*\)/echo \"\2\" | \1\3/'

echo "/usr/local/bin/gmail -t abc@yahoo.com -m \"this is a test\" -f xyz@yaahoo.com" | awk -F"\"" '{print "echo " $2" " $1 $3}

thans self

Thank you Glenn, Matrix and Aju for the help.

Seems like I am moving forward. Below is Input files, Code and Output on screen.
Now, how would I replace the current line with the edited line (temp1 in my code)?

Thanks again everyone for the input. I'm learning new things here.
--------------------------

Input file 1:

this is line 1 of test file
this is line 2 of test file
/usr/local/bin/gmail -s "Subject line" abc@yahoo.com lmn@yahoo.com -m " Message for file 1 " -f xyz@yahoo.com -a /home/kcc1hxj/testfile1.txt
this is line 4 of test file

Input file 2:

this is line 1 of test file
this is line 2 of test file
/usr/local/bin/gmail -s "Subject line" -a /home/kcc1hxj/testfile2.txt abc@yahoo.com lmn@yahoo.com -m " Message for file 1 " -f xyz@yahoo.com
this is line 4 of test file

Code:

#!/bin/ksh
for file in /home/kcc1hxj/projects/removemail/scripts/*
do
temp1=`grep ^./usr/local/bin/gmail $file | sed 's/\(.-m \)\("."\)\( .\)/echo \"\2\" | \1\3/' | sed 's/""/"/g'`
echo $temp1
done

Output on screen:

echo " Message for file 1 " | /usr/local/bin/gmail -s "Subject line" abc@yahoo.com lmn@yahoo.com -m -f xyz@yahoo.com -a /home/kcc1hxj/testfile1.txt
echo " Message for file 1 " | /usr/local/bin/gmail -s "Subject line" -a /home/kcc1hxj/testfile2.txt abc@yahoo.com lmn@yahoo.com -m -f xyz@yahoo.com

:slight_smile: