Sponsored Content
Full Discussion: Split records based on '-'
Top Forums UNIX for Advanced & Expert Users Split records based on '-' Post 302519604 by mora on Wednesday 4th of May 2011 11:15:22 AM
Old 05-04-2011
Split records based on '-'

HI,

I have a pipe delimiter file , I have to search for second field pattern, if the second field does not contain a '-' , I need to start capturing the record from this line till I find another second field with '-' value.

Below is the sample data


Code:
SOURCE DATA
ABC|ABC_702148-PARAM
ABC|CDE|BVD|VDGDT|DBDHDYT|DHDHJD|DJDGJGD
123|345|4556|5757|2323|5567786|454353|464645
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK
A123|123_702148
ABC|CDE|BVD|VDGDT|DBDHDYT|DHDHJD|DJDGJGD
123|345|4556|5757|2323|5567786|454353|464645
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK
DEF|XYZ_123_1358-PARAM
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK
ABC|CDE|BVD|VDGDT|DBDHDYT|DHDHJD|DJDGJGD
123|XYZ_123
ABC|CDE|BVD|VDGDT|DBDHDYT|DHDHJD|DJDGJGD
123|345|4556|5757|2323|5567786|454353|464645
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK
ABC|ABC_702148-PARAM
ABC|CDE|BVD|VDGDT|DBDHDYT|DHDHJD|DJDGJGD
123|345|4556|5757|2323|5567786|454353|464645
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK



Code:
Target Data

A123|123_702148
ABC|CDE|BVD|VDGDT|DBDHDYT|DHDHJD|DJDGJGD
123|345|4556|5757|2323|5567786|454353|464645
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK
123|XYZ_123
ABC|CDE|BVD|VDGDT|DBDHDYT|DHDHJD|DJDGJGD
123|345|4556|5757|2323|5567786|454353|464645
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK
ABDD|123|SASASA|1224|JJAKSJ|565464|DSDSD|LKLK


I was planning to do this in awk, do we have anything like

Code:
pseudo code
awk -F| '{$2== (can I search here for the '-') , then from this line to all the lines till I find another second erild with '-'

I was thinking of writing but as I don't know much about awk..please give me some pseudo code.

--MORA

Last edited by joeyg; 05-04-2011 at 12:18 PM.. Reason: corrected title
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

dynamically split the records

Hi, I was wandering would it be possible to split the record dynamically based on the certain values, for an instance i have a file with record with predefined split value i.e 10 col1 col2 col3 col4 ------------------------ aaaa bbbb 2 44aaaabbbb55cccddd1110 mmn xnmn 3... (6 Replies)
Discussion started by: braindrain
6 Replies

2. Shell Programming and Scripting

Sort & Split records in a file

Hi, I am new to scripting. I need a script to sort and the records in a file and then split them into different files. For example, the file is: H1...................... H2...................... D2.................... D2.................... H1........................... (15 Replies)
Discussion started by: Sunitha_edi82
15 Replies

3. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

4. Shell Programming and Scripting

split records into different files

Hi All, I want my file to be split based on value of 'N' (passed as argument). If value of 'N' is '2' then 4 new files will be generated from the below source file and the o/p file shoud look like File_$num , where num will be incremental. Source file: 1 2 3 4 5 O/p Files: ... (6 Replies)
Discussion started by: HemaV
6 Replies

5. Shell Programming and Scripting

Split records into multiple records

Hi All, I am trying to split a record into multiple records based on a value. Input.txt "A",1,0,10 "B",2,0,10,15,20 "C",3,11,14,16,19,21,23 "D",1,0,5 My desired output is: "A",1,0,10 "B",2,0,10 "B",2,15,20 "C",3,11,14 "C",3,16,19 "C",3,21,23 (4 Replies)
Discussion started by: kmsekhar
4 Replies

6. Shell Programming and Scripting

Split the Master and Child Records

Hi, I receive a file that has Master record followed by one/more Child Records as shown below & also as attached in the file. Now , The key for the child record is from pos 4 to position 80 in the parent record, now the requirement is to create two files 1. Parent file --> has all the parent... (1 Reply)
Discussion started by: KNaveen
1 Replies

7. Shell Programming and Scripting

Split Records

I have a flat file with 2 columns Id,loc 1,nj:ny:pa 2,pa 3,ca:tx:fl:nj Second colum data is seperated by semi colon and can i have many locations for one id Output i need is 1,nj 1,ny 1,pa 1,pa 3,ca 3,tx 3,fl (1 Reply)
Discussion started by: traininfa
1 Replies

8. Shell Programming and Scripting

Split file based on records

I have to split a file based on number of lines and the below command works fine: split -l 2 Inputfile -d OutputfileMy input file contains header, detail and trailor info as below: H D D D D TMy split files for the above command contains: First File: H DSecond File: ... (11 Replies)
Discussion started by: Ajay Venkatesan
11 Replies

9. Shell Programming and Scripting

Split records

Hi I have a file $cat test a,1;2;3 b,4;5;6;7 c,8;9 I want to split each record to multiple based on semicolon in 2nd field. i.e a,1 a,2 a,3 b,4 b,5 (3 Replies)
Discussion started by: Shivdatta
3 Replies

10. 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
Template::Plugin::Datafile(3)				User Contributed Perl Documentation			     Template::Plugin::Datafile(3)

NAME
Template::Plugin::Datafile - Plugin to construct records from a simple data file SYNOPSIS
[% USE mydata = datafile('/path/to/datafile') %] [% USE mydata = datafile('/path/to/datafile', delim = '|') %] [% FOREACH record = mydata %] [% record.this %] [% record.that %] [% END %] DESCRIPTION
This plugin provides a simple facility to construct a list of hash references, each of which represents a data record of known structure, from a data file. [% USE datafile(filename) %] A absolute filename must be specified (for this initial implementation at least - in a future version it might also use the "INCLUDE_PATH"). An optional "delim" parameter may also be provided to specify an alternate delimiter character. [% USE userlist = datafile('/path/to/file/users') %] [% USE things = datafile('items', delim = '|') %] The format of the file is intentionally simple. The first line defines the field names, delimited by colons with optional surrounding whitespace. Subsequent lines then defines records containing data items, also delimited by colons. e.g. id : name : email : tel abw : Andy Wardley : abw@tt2.org : 555-1234 sam : Simon Matthews : sam@tt2.org : 555-9876 Each line is read, split into composite fields, and then used to initialise a hash array containing the field names as relevant keys. The plugin returns a blessed list reference containing the hash references in the order as defined in the file. [% FOREACH user = userlist %] [% user.id %]: [% user.name %] [% END %] The first line of the file must contain the field definitions. After the first line, blank lines will be ignored, along with comment line which start with a '"#"'. BUGS
Should handle file names relative to "INCLUDE_PATH". Doesn't permit use of '":"' in a field. Some escaping mechanism is required. AUTHOR
Andy Wardley <abw@wardley.org> <http://wardley.org/> COPYRIGHT
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
Template::Plugin perl v5.12.1 2008-11-13 Template::Plugin::Datafile(3)
All times are GMT -4. The time now is 09:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy