Parsing record into multiple records in Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing record into multiple records in Shell Script
# 1  
Old 11-13-2009
Parsing record into multiple records in Shell Script

Hi, I am trying to parse a very long record in a text file into multiple records by checking ADD, DELETE, or MODIFY field value in a shell script.

Input
# File name xyz.txt
ADD|N000|8015662|DELETE|N001|9915662|MODIFY|N999|85678

Output
ADD|N000|8015662|
DELETE|N001|9915662|
MODIFY|N999|85678

I tried different techniques using awk, sed etc. But could not get the desired out. I would appreciate your help. Thanks
# 2  
Old 11-13-2009
Code:
awk 'BEGIN {RS=ORS="|"}
  (NR > 1) && /ADD|MODIFY|DELETE/ { ORS="\n"; print ""; ORS="|" }
  1
' file1

ADD|N000|8015662|
DELETE|N001|9915662|
MODIFY|N999|85678

# 3  
Old 11-13-2009
Thanks a lot. I works. If you do not mind can you please explain the code.
# 4  
Old 11-13-2009
Code:
# Set the input (RS) and output record (ORS) separators to | (normally a newline)
awk 'BEGIN {RS=ORS="|"}
# If the record is ADD, MODIFY or DELETE print a newline (we didn't print the ADD, etc. yet)
# (the ORS hss to be changed temporarily, otherwise print "" will print a | now)
# and skip the first record (NR > 1) as you don't want a blank line at the start
   (NR > 1) && /ADD|MODIFY|DELETE/ { ORS="\n"; print ""; ORS="|" }
# 1 is shorthand for {print} (to print the record (ADD, etc.) (the detault action is to print when a codition is true.  1 always evaluates to true))
   1
' file1

# 5  
Old 11-13-2009
Thanks for your time and effort to get the things straight
# 6  
Old 11-15-2009
Code:
awk 'BEGIN {RS=ORS="|"} {print /ADD|MODIFY|DELETE/?"\n"$0:$0}' xyz.txt

# 7  
Old 11-16-2009
perl:

Code:
my $str="ADD|N000|8015662|DELETE|N001|9915662|MODIFY|N999|85678";
my @tmp = split(/[|](?=(?:DELETE)|(?:MODIFY))/,$str);
print join "\n", @tmp;

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 split one record to multiple records?

Hi, I have one tab delimited file which is having multiple store_ids in first column seprated by pipe.I want to split the file on the basis of store_id(separating 1st record in to 2 records ). I tried some more options like below with using split,awk etc ,But not able to get proper output. can... (1 Reply)
Discussion started by: jaggy
1 Replies

2. Shell Programming and Scripting

Shell script for insert multiple records into a Database

I have a table in an Informix DB into which I want to insert multiple records at a time. Data for one of the column should be unique & other column data may be the same for all the records I insert Typical Insert Statement I use to insert one row : insert into employee(empid, country, state)... (5 Replies)
Discussion started by: nisav
5 Replies

3. Shell Programming and Scripting

Multiple Records from 1 Record

I need to make one record to multiple records based on occurence column in the record and change the date.For example below first record has 5 ,so need to create 5 records from one and change the date to 5 months.Occurence can be any number. I am unable to come with a script.Can some one help ... (5 Replies)
Discussion started by: traininfa
5 Replies

4. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

5. UNIX for Dummies Questions & Answers

Split single record to multiple records

Hi Friends, source .... col1,col2,col3 a,b,1;2;3 here colom delimeter is comma(,). here we dont know what is the max length of col3 means now we have 1;2;3 next time i will receive 1;2;3;4;5;etc... required output .............. col1,col2,col3 a,b,1 a,b,2 a,b,3 please give me... (5 Replies)
Discussion started by: bab.galary
5 Replies

6. Shell Programming and Scripting

Split a single record to multiple records & add folder name to each line

Hi Gurus, I need to cut single record in the file(asdf) to multile records based on the number of bytes..(44 characters). So every record will have 44 characters. All the records should be in the same file..to each of these lines I need to add the folder(<date>) name. I have a dir. in which... (20 Replies)
Discussion started by: ram2581
20 Replies

7. Shell Programming and Scripting

Multiple records based on ';' in the record

Hi All, I have a *.csv files in a die /pro/lif/dow, (pipe delimiter file), these files are having 8 columns and 6 column(CDR_LOGIC) records are populated as below, I need to incorporate the below logic in all the *.csv files. 11||:ColumnA||:ColumnB 123||:ColumnA IIF(:ColumnA = :ColumnC then... (6 Replies)
Discussion started by: shruthidwh
6 Replies

8. Shell Programming and Scripting

Script to display record spanning over multiple lines

Following are the lines from /etc/sudoers.conf bob SPARC = (OP) ALL : SGI = (OP) ALL fred ALL = (DB) NOPASSWD: ALL ALL CDROM = NOPASSWD: /sbin/umount /CDROM,\ /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM Could you please help me with shell/perl script to display the records with... (2 Replies)
Discussion started by: Ujan
2 Replies

9. UNIX for Advanced & Expert Users

Parsing records from one record

Hi, I got a file which is one huge record. I know each record should be 550 bytes long. How do I parse out the records from the one huge record. (1 Reply)
Discussion started by: bwrynz1
1 Replies

10. UNIX for Dummies Questions & Answers

Parsing out records from one huge record

Hi, I have one huge record and know that each record in the file is 550 bytes long. How do I parse out individual records from the single huge record. Thanks, (4 Replies)
Discussion started by: bwrynz1
4 Replies
Login or Register to Ask a Question