Sponsored Content
Full Discussion: Split column into rows
Top Forums UNIX for Beginners Questions & Answers Split column into rows Post 302991731 by saravananravim on Wednesday 15th of February 2017 10:25:39 PM
Old 02-15-2017
Split column into rows

Hi,

I have input dataset as below:

Code:
Cl.jenn,1051,ABCD
JEN.HEA,9740|1517|8119|2145,ZZZZ,REPEAT
Rich.Sm,
Ann.Car,3972|4051|1064|4323|4122|2394|2574|4507
Sta.for,7777,ABCD,UUUU
Sm.Ric,
Ch.LRD, 
Eh.ab,
Gr.sh,

Expected output:
-------------------
Code:
Cl.jenn,1051,ABCD
JEN.HEA,9740,ZZZZ,REPEAT
JEN.HEA,1517,ZZZZ,REPEAT
JEN.HEA,8119,ZZZZ,REPEAT
JEN.HEA,2145,ZZZZ,REPEAT
Rich.Sm,
Ann.Car,3972
Ann.Car,4051
Ann.Car,1064
Ann.Car,4323
Ann.Car,4122
Ann.Car,2394
Ann.Car,2574
Ann.Car,4507
Sta.for,7777,ABCD,UUUU
Sm.Ric,
Ch.LRD,
Eh.ab,
Gr.sh,

I am using below command to achieve this:
--------------------------------------------------
Code:
awk -F',' -v OFS=',' '{n=split($2,s,"|"); for(i=1;i<=n;i++) {$2=s[i];print}}' test.txt

Actual output is :
-------------------
Code:
Cl.jenn,1051,ABCD
JEN.HEA,9740,ZZZZ,REPEAT
JEN.HEA,1517,ZZZZ,REPEAT
JEN.HEA,8119,ZZZZ,REPEAT
JEN.HEA,2145,ZZZZ,REPEAT
Ann.Car,3972
Ann.Car,4051
Ann.Car,1064
Ann.Car,4323
Ann.Car,4122
Ann.Car,2394
Ann.Car,2574
Ann.Car,4507
Sta.for,7777,ABCD,UUUU
Ch.LRD,

Some records are filtered to display. Any suggestions pls???

Last edited by Don Cragun; 02-16-2017 at 12:01 AM.. Reason: Add CODE tags.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

split rows

Hi I wanted to split rows based on the number of 1's present in 21st field(21st field is 40 length field) so I wrote the below awk code. However, the tool that I am using to invoke the command is not recognising the command. So, could you please help me to translate this command to sed? awk... (5 Replies)
Discussion started by: ahmedwaseem2000
5 Replies

2. Shell Programming and Scripting

Split rows

Hi all, I need a simple bin/sh script FILE1: ab1 gegege swgdeyedg ac2 jxjjxjxjxxjxjx ad3 ae4 xjxjxj zhzhzh ahahs af5 sjsjsjs ssjsjsjsj sjsjsj ag6 shshshshs sjjssj shhshshs myScript.sh has to return: ROW ab1 ROW ac2 ROW ad3 ROW ae4 In other words: "ROW " + the first world... (3 Replies)
Discussion started by: ric79
3 Replies

3. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

4. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

6. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

7. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

8. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies

9. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

10. UNIX for Beginners Questions & Answers

How to split one long column into multiple rows with 3 each ?

I have a large csv dataset like this : A value1 A value2 A value3 B value1 B value2 B value3 C value1 C value2 C value3 what I expected output is :A value1 value2 value3 B value1 value2 value3 C value1 value2 value3 I'm thinking of use like awk, columns , but haven't find a proper... (4 Replies)
Discussion started by: nengcheng
4 Replies
Tangram::Relational::Mappings(3pm)			User Contributed Perl Documentation			Tangram::Relational::Mappings(3pm)

NAME
Tangram::Relational::Mappings - Mapping inheritance DESCRIPTION
There are many ways of representing inheritance relationships in a relational database. This document describes three popular ways and how Tangram supports them. STRATEGIES FOR MAPPING INHERITANCE
Inheritance is a concept that has no equivalent in the relational world. However, it is possible to implement it by using strict disci- plines and a combination of relational features like tables and foreign keys. One of the paramount issues about mapping inheritance is how well the mapping supports polymorphism. Any Object-Oriented persistence facility that deserves its name needs to allow the retrieval of all the Fruits, and return a heterogeneous collection of Apples, Oranges and Bananas. Also, it must perform this operation in an efficient manner. In particular, polymorphic retrieval should not cost one SELECT per retrieved object. A secondary - yet important - issue is how well the mapping plays by the rules of orthogonal orthodoxy. Another issue we'll examine is how well the mapping supports 'complex' queries, that is, queries that involve several objects. Three strategies are in common use, that go by the name Vertical, Horizontal and Filtered mapping. They all have advantages and disadvan- tages. The following sections describe the three strategies in details. They make use of a simple object model to illustrate the mappings. +---------------------+ | Person | | {abstract} | +---------<------- 1 +---------------------+ | | name: string | | +---------------------+ | | | ^ | | | +------------------+---------------------+ | | | | +---------------+ +-----------------+ V | NaturalPerson | | LegalPerson | | +---------------+ +-----------------+ | | age: integer | | form: string | | +---------------+ +-----------------+ | | | | +---------------------+ +-------->-------- * | Vehicle | | {abstract} | +---------------------+ | make: string | +---------------------+ | ^ | +------------------+-------------------+ | | +---------------+ +-----------------+ | Car | | Plane | +---------------+ +-----------------+ | plate: string | | ident: string | +---------------+ +-----------------+ Horizontal Mapping description Each concrete class is mapped onto a single table. Each row in the table describes the persistent state of one object. The attributes are mapped onto columns, usually one column per attribute but not necessarily. For example, collections may be stored else- where (for example on a link table) and thus require no column on the class' table. In effect, the database looks like this: +---------------+ | NaturalPerson | +------+--------+-------+------+ | id | name | age | ================================ | 17 | Bill Gates | 46 | +------+----------------+------+ | 23 | Georges Bush | 50 | +------+----------------+------+ +-------------+ | LegalPerson | +------+------+---------+------+ | id | name | form | ================================ | 36 | Microsoft | Inc | +------+----------------+------+ +------+ | Car | +------+-------+----------------+--------+ | id | owner | make | plate | ========================================== | 12 | 17 | Saab | BILL-1 | +------+-------+----------------+--------+ | 50 | 36 | Miata | MS-001 | +------+-------+----------------+--------+ | 51 | 36 | Miata | MS-002 | +------+-------+----------------+--------+ +-------+ | Plane | +------++-----+----------------+--------+ | id | owner| make | ident | ========================================= | 29 | 23 | Boeing | AF-001 | +------+------+----------------+--------+ advantages Polymorphic retrieval costs one SELECT per concrete conforming class; retrieving all the Persons costs two SELECTs. These SELECTs, however, don't use joins - an expensive operation. In our example, retrieving all the Persons requires the following two SELECTs: SELECT id, name, age FROM NaturalPerson SELECT id, name, form FROM LegalPerson disadvantages This mapping is reasonable with regard to relational orthodoxy, but not perfect: the 'name' column is present on two different tables, with the same semantic. The biggest drawback, however, happens when you try to perfrom complex queries. Suppose oyu want to retrieve all the Persons (Natural- or Legal-) that own a Vehicle of make 'Saab' (be it a Car or a Plane). Sticking with equijoins, the cost of the operation is four SELECTs: SELECT NaturalPerson.id, NaturalPerson.name, NaturalPerson.age FROM NaturalPerson, Car WHERE Car.owner = NaturalPerson.id SELECT NaturalPerson.id, NaturalPerson.name, NaturalPerson.age FROM NaturalPerson, Plane WHERE Plane.owner = NaturalPerson.id SELECT LegalPerson.id, LegalPerson.name, LegalPerson.form FROM LegalPerson, Car WHERE Car.owner = LegalPerson.id SELECT LegalPerson.id, LegalPerson.name, LegalPerson.form FROM LegalPerson, Plane WHERE Plane.owner = LegalPerson.id When the depth of the hierarchies increase, the combinatory explosion makes complex queries prohibitive. Vertical Mapping description Each class has its corresponding table, which contains only the class' direct fields. In other words, the table doesn't store the inherited fields. Both concrete and abstract classes get a table. The state of an object is thus scattered over several tables. For example: +--------+ | Person | +------+-+------+-------+ | id | name | ========================= | 17 | Bill Gates | +------+----------------+ | 23 | Georges Bush | +------+----------------+ | 36 | Microsoft | +------+----------------+ +---------------+ +-------------+ | NaturalPerson | | LegalPerson | +------+--------+ +-------+-----++ | id | age | | id | form | ================= ================ | 17 | 46 | | 36 | Inc | +------+--------+ +-------+------+ | 23 | 50 | +------+--------+ +---------+ | Vehicle | +------+--+----+----------------+ | id | owner | make | ================================= | 12 | 17 | Saab | +------+-------+----------------+ | 29 | 23 | AF-001 | +------+-------+----------------+ | 50 | 36 | Miata | +------+-------+----------------+ | 51 | 36 | Miata | +------+-------+----------------+ +------+ +-------+ | Car | | Plane | +------++--------+ +-------+--------+ | id | plate | | id | ident | ================== ================== | 12 | BILL-1 | | 29 | AF-001 | +-------+--------+ +-------+--------+ | 50 | MS-001 | +-------+--------+ | 51 | MS-002 | +-------+--------+ Polymorphic retrieval is achieved by issuing one SELECT per concrete conforming class; retrieving In our example, retrieving all the Per- sons requires the following two SELECTs: SELECT Person.id, Person.name, NaturalPerson.age FROM Person, NaturalPerson WHERE Person.id = NaturalPerson.id SELECT Person.id, Person.name, LegalPerson.form FROM Person, LegalPerson WHERE Person.id = LegalPerson.id This mapping sometimes needs an extra column that carries a type identifier. In our example, we take the very resonable assumption that Person is an abstract class. Had we decided to allow 'pure' Persons, we would have been faced with the following problem: the Person table would contain rows that describe pure Persons, but also rows that describe the Person part of Natural- and LegalPersons. We would need to filter those incomplete objects out when retrieving the pure Persons. Thus the Person table would look like this: +--------+ | Person | +-----+--+---+----------------+ | id | type | name | =============================== | 13 | 1 | Pure Person | +-----+------+----------------+ | 17 | 2 | Bill Gates | +-----+------+----------------+ | 23 | 2 | Georges Bush | +-----+------+----------------+ | 36 | 3 | Microsoft | +-----+------+----------------+ In this case, we need an extra SELECT for retrieving pure Persons: SELECT Person.id, Person.name FROM Person WHERE Person.type IN(1) advantages From the relational point of view, this mapping is excellent: the resulting database is in third normal form. This mapping also supports complex queries very well. Take the Saab owners example again: we don't need to involve the Car nor Plane tables in the query. As a result, two SELECTs suffice: SELECT Person.id, Person.name, NaturalPerson.age FROM Person, NaturalPerson, Vehicle WHERE Person.id = NaturalPerson.id AND Vehicle.owner = Person.id SELECT Person.id, Person.name, LegalPerson.form FROM Person, LegalPerson, Vehicle WHERE Person.id = LegalPerson.id AND Vehicle.owner = Person.id disadvantages The mapping potentially has the highest performance cost: it requires multiple SELECTs like the horizontal mapping, but in addition, these SELECTs use joins. Filtered Mapping description Entire hierarchies are mapped onto a single table. Two rows may describe objects of different types, maybe completely unrelated. The set of columns is the uperset of all the columns needed by all the attributes of any of the classes involved in the mapping. A special 'type' column contains an value that uniquely identifies the concrete class of the object described by the row. All the columns related to attributes that don't occur in all the classes must be declared as NULLABLE. Indeed, the table may contain mostly NULL values. In our example, the database may look either like this: +---------+ | Persons | +-----+---+--+----------------+------+------+ | id | type | name | age | form | ============================================= | 17 | 1 | Bill Gates | 46 | NULL | +-----+------+----------------+------+------+ | 23 | 1 | Georges Bush | 50 | NULL | +-----+------+----------------+------+------+ | 36 | 2 | Microsoft | NULL | Inc | +-----+------+----------------+------+------+ +---------+ | Persons | +-----+---+--+----------------+------+------+ | id | type | name | age | form | ============================================= | 17 | 1 | Bill Gates | 46 | NULL | +-----+------+----------------+------+------+ | 23 | 1 | Georges Bush | 50 | NULL | +-----+------+----------------+------+------+ | 36 | 2 | Microsoft | NULL | Inc | +-----+------+----------------+------+------+ | 36 | 2 | Microsoft | NULL | Inc | +-----+------+----------------+------+------+ +----------+ | Vehicles | +-----+----+-+-------+----------------+--------+--------+ | id | type | owner | make | plate | ident | ========================================================= | 12 | 3 | 17 | Saab | BILL-1 | NULL | +-----+------+-------+----------------+--------+--------+ | 29 | 4 | 23 | Boeing | NULL | AF-001 | +-----+------+-------+----------------+--------+--------+ | 50 | 3 | 36 | Miata | MS-001 | NULL | +-----+------+-------+----------------+--------+--------+ | 51 | 3 | 36 | Miata | MS-002 | NULL | +-----+------+-------+----------------+--------+--------+ Retrieving all the Persons requires only one SELECT: SELECT id, name, age, form FROM Persons When retrieving NaturalPersons we must take care to filter out the rows that belog to LegalPersons: SELECT id, name, age FROM Persons WHERE type = 1 We may even decide to place unrelated hierarchies on the same table: +---------+ | Objects | +-----+---+--+---------------+------+------+--------+--------+--------+ | id | type | name | age | form | make | plate | ident | ======================================================================= | 17 | 1 | Bill Gates | 46 | NULL | NULL | NULL | NULL | +-----+------+---------------+------+------+--------+--------+--------+ | 23 | 1 | Georges Bush | 50 | NULL | NULL | NULL | NULL | +-----+------+---------------+------+------+--------+--------+--------+ | 36 | 2 | Microsoft | NULL | Inc | NULL | NULL | NULL | +-----+------+---------------+------+------+--------+--------+--------+ | 12 | 3 | NULL | NULL | NULL | Saab | BILL-1 | NULL | +-----+------+---------------+------+------+--------+--------+--------+ | 29 | 4 | NULL | NULL | NULL | Boeing | NULL | AF-001 | +-----+------+---------------+------+------+--------+--------+--------+ | 50 | 3 | NULL | NULL | NULL | Miata | MS-001 | NULL | +-----+------+---------------+------+------+--------+--------+--------+ | 51 | 3 | NULL | NULL | NULL | Miata | MS-002 | NULL | +-----+------+---------------+------+------+--------+--------+--------+ advantages Polymorphic retrieval costs exactly one SELECT, regardless of the number of conforming types. Thus this mapping potentially is the most efficient. disadvantages This mapping is very questionable according to relational orthodoxy. Even if one decides to forgo these rules, using such a mapping takes away many of the interesting features offered by modern RDBM systems. Because nearly all the columns must allow NULL values, we cannot take advantage of features like referential integrity constraints, domain constraints, indexes, etc. Also, as the table becomes cluttered with NULL values, the relative number of significant columns in any given row tends towards zero: we may end up retrieving rows consisting of a little information swimming in a sea of NULLs. In effect, this mapping may end up hindering performance instead of improving it in presence of deep hierarchies with many attributes. MAPPINGS SUPPORTED BY TANGRAM
Tangram supports both vertical mapping and filtered mapping, and any hybrid of the two. The 'table' attribute in the class description in the Schema can be used to put the state of several classes on the same table. The table name defaults to the class name, resulting in a vertical mapping. For example, the following schema: Tangram::Relational->schema( { classes => [ Person => { table => 'Persons', fields => { string => [ qw( name ) ] } }, NaturalPerson => { table => 'Persons', fields => { int => [ qw( age ) ] } }, LegalPerson => { table => 'Persons', fields => { string => [ qw( form ) ] } } ] } ); ...specifies a pure filtered mapping for the Person hierarchy: CREATE TABLE Persons ( id INTEGER NOT NULL, PRIMARY KEY( id ), type INTEGER NOT NULL, form VARCHAR(255) NULL, age INT NULL, name VARCHAR(255) NULL ); The following schema: Tangram::Relational->schema( { classes => [ Person => { table => 'Person', fields => { string => [ qw( name ) ] } }, NaturalPerson => { table => 'NaturalPerson', fields => { int => [ qw( age ) ] } }, LegalPerson => { table => 'Person', fields => { string => [ qw( form ) ] } } ] } ); ...gives NaturalPerson its own table, but LegalPerson shares the Person table: CREATE TABLE Person ( id INTEGER NOT NULL, PRIMARY KEY( id ), type INTEGER NOT NULL, form VARCHAR(255) NULL, name VARCHAR(255) NULL ); CREATE TABLE NaturalPerson ( id INTEGER NOT NULL, PRIMARY KEY( id ), type INTEGER NOT NULL, age INT NULL ); perl v5.8.8 2006-03-29 Tangram::Relational::Mappings(3pm)
All times are GMT -4. The time now is 09:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy