Picking out text from one file and writing it to another.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Picking out text from one file and writing it to another.
# 1  
Old 05-20-2005
Picking out text from one file and writing it to another.

Hello. I have one file that is a collection of discarded emails. Each email is it's own section with each section beginning with the same header (ie 'Another Email' ). I want to traverse through the file and every time I find the header ('Another Email') I then want to pick out the 'To:' line information (which is on it's own line in the file) and the 'Subject:' line information which is on another line. I want to take all the 'To:' lines and 'Subject:' lines and write them to one file. I have been trying to do this using sed, awk, and grep but can't seem to get it to work. Any help would be greatly appreciated.

Thanks,
Scott
# 2  
Old 05-20-2005
Post the script you tried so far.
# 3  
Old 05-20-2005
Scott
If you could give a sample of your input file (ie, the rejected mail file) it would be much helpful to those who want a try on this issue.
# 4  
Old 05-20-2005
Sorry, the info is confidential.

Due to the nature of the emails I can not post any of the info in the file but it essentially like this.
<> Another Email From Machine 1
Header and format info......
Date:......
Time:......
To: bob@yahoo.com
Subject: I need help with Unix scripting
HTML......
<> End section
<> Another Email From Machine 1
Header and format info......
Date:......
Time:......
To: tom@yahoo.com
Subject: Hi there.
HTML......
<> End section

I hope this helps.

Thanks,
Scott
# 5  
Old 05-20-2005
Try this...

grep "^To:" inputfile > f1; grep "^Subject:" inputfile > f2;paste f1 f2 > yourfile;rm f1 f2


and check the file yourfile
# 6  
Old 05-20-2005
Code:
#!/bin/ksh
infile="./scott33.txt"
tmpoutfile="./scott33.out"
lastfile="./scott33.done"
cat $infile|awk /"Another Email"/,/"Subject:"/ > $tmpoutfile
cat $tmpoutfile|egrep -e '(Subject:|To:)' > $lastfile
rm $tmpoutfile
cat $lastfile

This might do what you want.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

2. Shell Programming and Scripting

Picking values from a file.

Hi, I have a file which contains values in this format. abc cde fgh ijk lmn opq rst uvw The user will pass the required parameter from the command line. My requirement is that script should pick the values passed by the user and the next value in the next line. Like if the user... (10 Replies)
Discussion started by: arijitsaha
10 Replies

3. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

4. Homework & Coursework Questions

Efficient Text File Writing

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a template main.c file via shell script to make it easier for yourself later. The issue here isn't writing... (2 Replies)
Discussion started by: george3isme
2 Replies

5. UNIX for Dummies Questions & Answers

Writing text to file

Hi, I know the code to write a piece of text to the end of a given text file is echo $text >> filename.txt I would like to know how to write a piece of text to a file using shell, but the file name isn't given. I want it to write to whatever text file is currently open. Not to all text files... (2 Replies)
Discussion started by: anirudh215
2 Replies

6. Shell Programming and Scripting

picking file from a particular URL and the processing

body { margin: 0 0 0 0; padding:0 0 0 0 }td,div { font-family:Courier New;font-size:10pt;vertical-align:top }body { margin: 0 0 0 0; padding:0 0 0 0 }.transcript { background-color:#d2d2d2; }.messageBlock { margin-left:4px; margin-bottom:3px }.message { margin-left:100px; word-wrap:break-word;... (0 Replies)
Discussion started by: priyanka3006
0 Replies

7. Shell Programming and Scripting

Need help in writing a script to create a new text file with specific data from existing two files

Hi, I have two text files. Need to create a third text file extracting specific data from first two existing files.. Text File 1: Format contains: SQL*Loader: Release 10.2.0.1.0 - Production on Wed Aug 4 21:06:34 2010 some text ............so on...and somwhere text like: Record 1:... (1 Reply)
Discussion started by: shashi143ibm
1 Replies

8. Shell Programming and Scripting

writing data in a text file at particular line

I need to write value of variable $version at a particular line in a text file. Line number is determined by another variable &line......I don't know how to do it in shell script ... (2 Replies)
Discussion started by: punitpa
2 Replies

9. Shell Programming and Scripting

parsing data file picking out certain fields

I have a file that is large and is broken up by groups of data. I want to take certain fields and display them different to make it easier to read. Given input file below: 2008 fl01 LAC 2589 polk doal xx 2008q1 mx sect 25698541 Sales 08 Dept group lead1 ... (8 Replies)
Discussion started by: timj123
8 Replies

10. Shell Programming and Scripting

Picking the file based on Date..Requirement

Dear frnds My requirement is as follows -rw-r----- 1 f02 dd 109428250 May 18 14:02 Extracts_20070518104730.zip -rw-r----- 1 f02 dd 109493187 May 21 13:30 Extracts_20070521091700.zip -rw-r----- 1 f02 dd 109993058 May 23 14:14 Extracts_20070523085955.zip -rw-r----- 1... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies
Login or Register to Ask a Question