How to copy a string to a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to copy a string to a text file
# 1  
Old 08-02-2008
Java How to copy a string to a text file

I am using the following command to email a tex file as an attachment-

cat mailtext.txt | elm -s "Subject" emailAddr
where content of mailtext.txt is -
"Body of email"
[include foo.txt text/plain base64]

This will attach foo.txt with the email.

My problem is that the file foo.txt is ceated dynamically everytime with a timestamp. e.g. foo_<timestamp>.txt

So if this filename is collected in a variable-
sFileName = foo_<timestamp>.txt

How do I include this filename dynamically in the mailtext.txt file?

Any help would be appreciated.

Thanks.
# 2  
Old 08-03-2008
Use a HERE document and interpolate the value of a variable containing the file name.

Code:
elm -s "Subject" emailAddr <<HERE
"Body of email"
[include $sFileName text/plain base64]
HERE

With this solution, you don't need the external file at all. (Of course, if you still need an external file for other reasons, you can create it from a HERE document and then simply keep the sending part like you already had it, perhaps modulo the Useless Use of Cat.)
# 3  
Old 08-03-2008
Thanks for the solution ...

I am providing this command in a C program.

sprintf( cmd, "elm -s SUBJECT %s <<HERE \n %s \n [include %s text/plain base64] \n HERE ", emailAddr, sMailBody, sFileName );

This is sending the [include %s text/plain base64] part also in the email body along with the actual body text. And there is no file attached in the email.

Could you please help me with this ???
# 4  
Old 08-03-2008
The <<HERE is a feature of the shell; you need to spawn a shell to get it working.

Code:
sprintf (cmd, "sh -c 'elm -s SUBJECT %s <<HERE\n"
  "%s\n[include %s text/plain base64]\nHERE\n'"l, emailAddr, sMailBody, sFileName);

You are not allowed any leading whitespace on the line containing the terminating HERE (at least not in many shells).

Couldn't you just open a pipe to "elm -s SUBJECT %s" with popen() and pipe in the content yourself, though? That's pretty much equivalent to using a HERE document. (And you avoild the pesky security considerations you always bump into when spawning a shell with user-supplied input.)

If the file truly is text/plain, what's the benefit of encoding it in base64, by the way?
# 5  
Old 08-03-2008
Could you please explain me how to use popen() ?

It could be a pretty simple thing, but I m a novice in unix. It would be good if you could show me the command.
# 6  
Old 08-04-2008
Googling for "popen examples" gets me e.g. popen
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy a string to another file

OS version: RHEL 6.7 Shell : Bash I have a file like below. It has 500K lines. I want to extract TAG_IDs shown in single quote at the end to copied to another file. As if I had copied the TAG_IDs using block select (Column Select) in modern text editor $ cat file.txt UPDATE TAGREF SET... (9 Replies)
Discussion started by: John K
9 Replies

2. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

How to copy one file into another except some text

I have file input like- abc,"123",""123","123","123","123","123","123","123","123","123","123","123","123", abc,"123","123","123","123","123","123","123","123","123""123",abc etc Input file consists of 1000 records and i want to copy into final file except abc abc is at fixed position... (6 Replies)
Discussion started by: AhmedLakadkutta
6 Replies

6. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2 thanks in advance! (4 Replies)
Discussion started by: engr.jay
4 Replies

7. Shell Programming and Scripting

How to copy text with ed to new file ?

Alright, so in ed i do a subsititution: ed file << 'HERE' s/.*h2.*>\(.*\)<.*/\1/p HERE This gives me my desired line: Errors found while checking this document as XHTML 1.0 Transitional! But how would i put this line in a temporary file ? (4 Replies)
Discussion started by: Bertieboy7
4 Replies

8. Shell Programming and Scripting

Copy string from files into new file

I'm trying to copy a string (myame@yahoo.com) from multiple files and save them to a new file. This is what's I've gathered so far: sed 's/string/g' file.txt > output.txt Not sure how to run this on multiple files and extract just the email address found in each file. Any help would be... (2 Replies)
Discussion started by: rdell
2 Replies

9. UNIX for Dummies Questions & Answers

Copy or Grep all text below a string

Hello, I am trying to copy all the text from a file below a search string... For example i want to grep all text below the word sure: UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! ... (2 Replies)
Discussion started by: aliaa2a
2 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question