Cutting text from one location to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting text from one location to another
# 1  
Old 11-28-2006
Bug 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.
# 2  
Old 11-28-2006
Here's a possible awk solution that seems to work, but makes assumptions about the input data:
Code:
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

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

Code:
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

# 3  
Old 11-28-2006
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.

Smilie

Last edited by hemangjani; 11-28-2006 at 02:58 PM..
# 4  
Old 11-28-2006
Just use i/o redirection and a temp file:
Code:
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".
# 5  
Old 11-28-2006
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.
# 6  
Old 11-29-2006
Code:
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/'

# 7  
Old 11-29-2006
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}
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 find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

2. Shell Programming and Scripting

Insert text line to specific location CSV

In Perl. ***edited question below*** Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file: age,name,locationIs it possible to INSERT data into the CSV into the correct age order. For example, if I had... (1 Reply)
Discussion started by: whyte_rhyno
1 Replies

3. Shell Programming and Scripting

cutting long text by special char around 100 byte and newline

Regard, How can i cut the text by special char(|) around 100 byte and write the other of the text at newline using Perl. ... (3 Replies)
Discussion started by: Shawn, Lee
3 Replies

4. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

5. Shell Programming and Scripting

Cutting out text from specific portion on filename

Hi, how do I go about cutting out the first numeric characters after the word "access"? access1005101228.merged-00.15.17.86.d8.b8.log.gz (16 Replies)
Discussion started by: GermanJulian
16 Replies

6. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

7. Shell Programming and Scripting

How to cut first line only from a text near a specific column without cutting a word

First I have to say thank you to this community and this forum. You helped me very much builing several useful scripts. Now, I can't get a solution the following problem, I'm stuck somehow. Maybe someone has an idea. In short, I dump a site via lynx and pipe the output in a file. I need to... (7 Replies)
Discussion started by: lowmaster
7 Replies

8. UNIX for Advanced & Expert Users

copy files from one location to similar location

I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure, say location 1, /home/rick/tmp_files/1-12/00-25/ here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
Discussion started by: pharos467
1 Replies

9. HP-UX

replacing text in specific location

i have a file that looks like this: 000000112/01/2008 D99999 000000 12/01/2008 D99999 000000 12/01/2008 1D99999 i need to replace the blanks into 1 for column 7,18-19 how can this be achieved using awk? Thanks. (1 Reply)
Discussion started by: zeontman
1 Replies

10. Shell Programming and Scripting

cutting unwanted text

Hello, I have a log file which looks like this : I want to cut out some text and get printed only : "HTTP GET" and "Time duration: 1013 mls. (1 sec.)" I tried searching the forum for such solution, but I was unable to find it. I also tried awk '/duration/ { print $0 }' mylogfile.log,... (2 Replies)
Discussion started by: sysgate
2 Replies
Login or Register to Ask a Question