extract part of text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract part of text file
# 1  
Old 06-11-2010
extract part of text file

I need to extract the following lines from this text and put it in different files.

Code:
From xxxx@gmail.com  Thu Jun 10 21:15:46 2010
Return-Path: <xxxxx@gmail.com>
X-Original-To: xxx@localhost
Delivered-To:xxxx@localhost
Received: from ubuntu (localhost [127.0.0.1])
    by ubuntu (Postfix) with ESMTP id 53FDD2575A
    for <xxxxxx@localhost>; Thu, 10 Jun 2010 12:15:46 -0700 (PDT)
MIME-Version: 1.0
Received: from gmail-pop.l.google.com [xxxxx]
    by ubuntu with POP3 (fetchmail-6.3.9-rc2)
    for <xxxxxx@localhost> (single-drop); Thu, 10 Jun 2010 21:15:46 +0200 (CEST)
Received: by xxxxxx with HTTP; Thu, 10 Jun 2010 12:13:40 -0700 (PDT)
Date: Thu, 10 Jun 2010 21:13:40 +0200
Delivered-To: xxxxxxxr@gmail.com
Message-ID: <xxxxxxxxx@mail.gmail.com>
Subject: TOPIC
From: NAME <xxxxxxxx@gmail.com>
To: xxxxxxxxxxx@gmail.com
Content-Type: multipart/alternative; boundary=001485f1ea94fa4e4d0488b1d13c
X-Antivirus: avast! (VPS 100610-0, 10/06/2010), Inbound message
X-Antivirus-Status: Clean

--001485f1ea94fa4e4d0488b1d13c
Content-Type: text/plain; charset=ISO-8859-1

This is an exemple from text

--001485f1ea94fa4e4aaaa8b1d13c
Content-Type: text/html; charset=ISO-8859-1

This is an exemple from text

--001485f1ea94fa4e4aaaa8b1d13c--

From xxxx@gmail.com  Thu Jun 10 21:15:46 2010
Return-Path: <xxxxx@gmail.com>
X-Original-To: xxx@localhost
Delivered-To:xxxx@localhost
Received: from ubuntu (localhost [127.0.0.1])
    by ubuntu (Postfix) with ESMTP id 53FDD2575A
    for <xxxxxx@localhost>; Thu, 10 Jun 2010 12:15:46 -0700 (PDT)
MIME-Version: 1.0
Received: from gmail-pop.l.google.com [xxxxx]
    by ubuntu with POP3 (fetchmail-6.3.9-rc2)
    for <xxxxxx@localhost> (single-drop); Thu, 10 Jun 2010 21:15:46 +0200 (CEST)
Received: by xxxxxx with HTTP; Thu, 10 Jun 2010 12:13:40 -0700 (PDT)
Date: Thu, 10 Jun 2010 21:13:40 +0200
Delivered-To: xxxxxxxr@gmail.com
Message-ID: <xxxxxxxxx@mail.gmail.com>
Subject: TOPIC
From: NAME <xxxxxxxx@gmail.com>
To: xxxxxxxxxxx@gmail.com
Content-Type: multipart/alternative; boundary=001485f1ea94fa4e4d0488b1d13c
X-Antivirus: avast! (VPS 100610-0, 10/06/2010), Inbound message
X-Antivirus-Status: Clean

--001485f1ea94fa4e4d0488b1d13c
Content-Type: text/plain; charset=ISO-8859-1

this text can be
1 or more lines
like this

--001485f1ea94fa4e4d0asdfadgad3c
Content-Type: text/html; charset=ISO-8859-1

this text can be
1 or more lines
like this

--001485f1ea94fa4e4d0asdfadgad3c--

I need an output file like this
Code:
Subject: TOPIC
From: NAME <xxxxxxxx@gmail.com>
this text can be
1 or more lines
like this

thank you for helping

Moderator's Comments:
Mod Comment Use code tags also for data and logs etc., ty.

Last edited by waxo; 06-11-2010 at 09:14 AM.. Reason: code tags
# 2  
Old 06-11-2010
if there is only 1 file. then u can do the below code:-

Code:
egrep "Subject|From|Text" infile

but as far as ur text is considered i am sure it has more than one line. is their any specific pattern in ur text. (that u can check for.)
this is not the best solution but this will work.
for others wait for the masters of awk and sed Smilie

btw do u knw any yaxo ? ?
This User Gave Thanks to dazdseg For This Post:
# 3  
Old 06-11-2010
The only pattern I found is this.

search for this line "Content-Type: text" , and while not "--" print.

Content-Type: text/plain; charset=ISO-8859-1



--


but I have no idea from how to make it.

Last edited by waxo; 06-11-2010 at 09:16 AM..
# 4  
Old 06-11-2010
Code:
awk '/Content-Type: text\/plain; charset=ISO-8859-1/,/--/' infile

# 5  
Old 06-11-2010
Code:
Content-Type: text/plain; charset=ISO-8859-1

this will be the starting pattern but what will be the ending pattern?

is this ur ending pattern ??
Code:
--001485f1ea94fa4e4aaaa8b1d13c--

if this is true then u can use sed or awk. let me check wht i can do bcz i am not good with either sed or awk bt i try and give u an answer
This User Gave Thanks to dazdseg For This Post:
# 6  
Old 06-11-2010
the end pattern is "--" because the following numbers change every time.

Smilie

---------- Post updated at 07:34 AM ---------- Previous update was at 07:27 AM ----------

Quote:
Originally Posted by bartus11
Code:
awk '/Content-Type: text\/plain; charset=ISO-8859-1/,/--/' infile

Content-Type: text/plain; charset=ISO-8859-1

this text can be
1 or more lines
like this


--001485f1ea946fb0b20488b1e401

It works! but I need only the text and no the patterns.
# 7  
Old 06-11-2010
Code:
awk '/Content-Type: text\/plain; charset=ISO-8859-1/,/--/{if (!/(Content-Type: text\/plain; charset=ISO-8859-1)|(--)/){print}' infile

This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substr/Instr in shell script or extract part of text

Hi, I need to extract part of a text to two variables text is "PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1" I want "Test Error Message" in one variable and "1" in another variable.... (11 Replies)
Discussion started by: vedavrath
11 Replies

2. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

3. Programming

Extract part of an archive to a different file

I need to save part of a file to a different one, start and end offset bytes are provided by two counters in long format. If the difference is big, how should I do it to prevent buffer overflow in java? (7 Replies)
Discussion started by: Tribe
7 Replies

4. Shell Programming and Scripting

Extract the part of sequences from a file

I have a text file, input.fasta contains some protein sequences. input.fasta is shown below. >P02649 MKVLWAALLVTFLAGCQAKVEQAVETEPEPELRQQTEWQSGQRWELALGRFWDYLRWVQT LSEQVQEELLSSQVTQELRALMDETMKELKAYKSELEEQLTPVAEETRARLSKELQAAQA RLGADMEDVCGRLVQYRGEVQAMLGQSTEELRVRLASHLRKLRKRLLRDADDLQKRLAVY... (8 Replies)
Discussion started by: rahim42
8 Replies

5. Shell Programming and Scripting

Extract part of file

Hello All, I need to extract part of a file into a new file My file is Define schema xxxxxx Insert into table ( a ,b ,c ,d ) values ( 1, 2, 3, (15 Replies)
Discussion started by: nnani
15 Replies

6. Shell Programming and Scripting

extracting part of a text file

Hi guys So I have a very large log file where each event is logged along with the time that it occurred. So for e.g. The contents of the file look like: ... 12:00:07 event 0 happened. 12:01:01 event 1 happened. 12:01:05 event 2 happened. 12:01:30 event 3 happened. 12:02:01 event 4... (10 Replies)
Discussion started by: alinaqvi90
10 Replies

7. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

8. Shell Programming and Scripting

How to extract certain part of log file?

Hi there, I'm having some problem with UNIX scripting (ksh), perhaps somebody can help me out? For example: ------------ Sample content of my log file (text file): -------------------------------------- File1: .... info_1 ... info_2 ... info_3 ... File2: .... info_1 ... info_2 ...... (10 Replies)
Discussion started by: superHonda123
10 Replies

9. UNIX for Dummies Questions & Answers

Extract a part of file name

Hi, I want to extract a part of filename and pass it as a parameter to one of the scripts. Could someone help. File name:- NLL_NAM_XXXXX.XXXXXXX_1_1.txt. Here i have to extract only XXXXX.XXXXXXX and the position will be constant. that means that i have to extract some n characters from... (6 Replies)
Discussion started by: dnat
6 Replies

10. Shell Programming and Scripting

Moving part of Text in a file

Hi, I have this text in a file where I need to move part of the text.... <Relation1 OriginatingObjectID="Holding_1" RelatedObjectID="Party_1" id="Relation_1"> <OriginatingObjectType tc="4">Holding</OriginatingObjectType> <RelatedObjectType tc="6">Party</RelatedObjectType>... (4 Replies)
Discussion started by: mgirinath
4 Replies
Login or Register to Ask a Question