AWK Command parse a file based on string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Command parse a file based on string.
# 1  
Old 12-09-2011
AWK Command parse a file based on string.

AWK Command parse a file based on string.
I am trying to write a shell script to parse a file based on a string and move the content of the file to another file.

Here is scenario.
File content below

Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_1041_211815584.1323469617525"
X-Edel-MessageId: E76F5EEF-AC2D-482B-902C-D3A7A72D
X-Edel-MessageSent: fbd435be-8007-4a32-ac18-758ddea10b5a
------=_Part_1041_211815584.1323469617525


I wrote a awk command to break the file based on the below string.
X-Edel-MessageSent
awk -v fname=$filename '/X-Edel-MessageSent:/{i++}{print > i+1"-"fname}' filename.txt

Issue i am facing:- When I used the above command file is getting split into two files.
File1:-
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_1041_211815584.1323469617525"
X-Edel-MessageId: E76F5EEF-AC2D-482B-902C-D3A7A72D

File2:-
X-Edel-MessageSent: fbd435be-8007-4a32-ac18-758ddea10b5a
------=_Part_1041_211815584.1323469617525


But I need the files in the below way:-

File1:-
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_1041_211815584.1323469617525"
X-Edel-MessageId: E76F5EEF-AC2D-482B-902C-D3A7A72D
X-Edel-MessageSent: fbd435be-8007-4a32-ac18-758ddea10b5a


File2:-
------=_Part_1041_211815584.1323469617525

Your help is greatly appreciated.

Smilie
# 2  
Old 12-09-2011
Very very close. Print first, then increment.

Code:
awk -v fname=$filename '{print > i+1"-"fname};  /X-Edel-MessageSent:/{i++}' filename.txt

# 3  
Old 12-09-2011
Thanks a bunch. It worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the - and last id in it, indicated by the digit after the -, I am trying to use awk to print the missing line or lines in file following the pattern of the previous line. For example, in the file below the next... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to parse file and display result based on text

I am trying using awk to open an input file and check a column 2/field $2 and if there is a warning then that is displayed (variantchecker): G not found at position 459, found A instead. The attached Sample1.txt is that file. If in that column/field there is a black space, then the text after... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

awk Parse And Create Multiple Files Based on Field Value

Hello: I am working parsing a large input file which will be broken down into multiples based on the second field in the file, in this case: STORE. The idea is to create each file with the corresponding store number, for example: Report_$STORENUM_$DATETIMESTAMP , and obtaining the... (7 Replies)
Discussion started by: ec012
7 Replies

4. Shell Programming and Scripting

A command to split a file into two based on a string

Hello What command can i use to split a tab delimited txt file into two files base on the occurrence of a string my file name is EDIT.txt The content of file is below XX 1234 PROCEDURES XY 1634 PROCEDURES XM 1245 CODES XZ 1256 CODES It has more than a million record If there is... (16 Replies)
Discussion started by: madrazzii
16 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

Use awk or sed to parse delimited string

Hi I am trying to figure out the best way to search a long log file and print out certain information. For example if I had a line in a log file delimited by ampersand first_name=mike&last_name=smith&zip_code=55555&phone=555-5555&state=ma&city=boston and I only wanted to search for and... (3 Replies)
Discussion started by: mstefaniak
3 Replies

7. Shell Programming and Scripting

Parse a string as a command

I've a problem parsing a string as a command: Consider script stefano.sh as following: #!/usr/bin/sh txtshell="./parser.sh /ews/MyEventHandler/data/handler/StopAndMail.php eventid=StopAndMail.MVIN.6300 lot_number=1122FXB facility=EWSF3 'mailto=prova.prova@nohost.com, prova.test@nohost.com'... (2 Replies)
Discussion started by: buonstefano
2 Replies

8. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

9. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

10. UNIX for Dummies Questions & Answers

parse string with awk

Hi Guys, I spend half a day getting this to work with no luck, perhaps you guys can help.. I have a string from a file looking like this: module::name=test::type=generic_data::exec=snmpget.......::desc=A Little Test::interval=300 what I would like to split it, so I get a value for each... (3 Replies)
Discussion started by: hyber
3 Replies
Login or Register to Ask a Question