Sponsored Content
Top Forums UNIX for Advanced & Expert Users Parse key-value pair into separate rows Post 302738515 by ctsgnb on Saturday 1st of December 2012 05:42:29 PM
Old 12-01-2012
Code:
$ cat t1
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405717924,9700005405733788|unidentified,unidentified,unidentified
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified
$ awk -F"|" '{a=$1
m=split($(NF-1),V,",")
n=split($NF,T,",")
if(n!=m){print "ERROR-File[" FILENAME "] -line[" NR "] : Number of field inconsistent";exit}
else{i=0
while(++i<=m){print "row"(++d)":" RS a FS FS V[i] FS T[i]}
}}' t1
row1:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified
row2:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405717924|unidentified
row3:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405733788|unidentified
row4:
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405552747|unidentified
row5:
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405717924|unidentified
row6:
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405733788|unidentified
ERROR-File[t1] -line[3] : Number of field inconsistent
$

If you don't want to exit when encountering an inconsistency in the number of field you can just replace the "exit" with "next" to skip the erroneous line.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

automating RSA key pair generation

I want to automate the process of generating RSA keys. I want to remotley login to a linux machine from a windows maching without having to enter a password. For this I need to generate the RSA key pair. but I want to do this procedure on alot of linux machines. For which I was looking to automate... (2 Replies)
Discussion started by: lassimanji
2 Replies

2. UNIX for Dummies Questions & Answers

forming duplicate rows based on value of a key

if the key (A or B or ...others) has 4 in its 3rd column the 1st A row has to form 4 dupicates along with the all the values of A in 4th column (2.9, 3.8, 4.2) . Hope I explain the question clearly. Cheers Ruby input "A" 1 4 2.9 "A" 2 5 ... (7 Replies)
Discussion started by: ruby_sgp
7 Replies

3. Shell Programming and Scripting

Parse a File ColumnWise & Delete the Rows

Hi , I have a CSV file that has around 50K records in it. I have to delete all the duplicate rows from the file except one, depending upon data in column4. Lets say there are 3 rows for 'column4data' in the file. I have to retain only that line which has the smallest date value in column2.... (3 Replies)
Discussion started by: Sheel
3 Replies

4. UNIX for Dummies Questions & Answers

How to separate a single column file into files of the same size (i.e. number of rows)?

I have a text file with 1,000,000 rows (It is a single column text file of numbers). I would like to separate the text file into 100 files of equal size (i.e. number of rows). The first file will contain the first 10,000 rows, the second row will contain the second 10,000 rows (rows 10,001-20,000)... (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Bash script to create rsa key pair

Hello all, I am setting up a cluster of Mac Pro's which need to be able to talk to a master computer, traffic between the nodes and the master needs to take place without a ssh key. I need a script that will create a security key, save it to the default place, enter the password as no password.... (2 Replies)
Discussion started by: sdl27789
2 Replies

6. UNIX for Advanced & Expert Users

Parse (delimited string) key-value pairs in a column into separate lines

Hi experts, e.g. i/p data looks like 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified|| o/p data should like - row1: 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified ... (1 Reply)
Discussion started by: sumoka
1 Replies

7. Shell Programming and Scripting

Line up Print outs from Separate Rows

A text file has two logs of same event & both logs have to be correlated into a single line before same can be processed further RAW RunningLog Date Month Year Time Event 9 JUN 2013 1932 2 10 JAN 2014 1000 4 Duration Closed ... (10 Replies)
Discussion started by: newageBATMAN
10 Replies

8. Shell Programming and Scripting

How to parse parts of 1 column into two separate columns?

I have a shell script that is currently transferring a csv file from a server into a Teradata database table. One of the 30 or so columns is called "destination_url". In that URL there are parameters, and it is possible for those parameters to be repeated because of referring companies copying... (3 Replies)
Discussion started by: craigwg
3 Replies

9. Shell Programming and Scripting

How to separate rows of data into another column?

I have data such as below where the value in second field is the same as that in the row after. 123456,22222,John,0,xyz 234567,22222,John1,1,cde 43212,3333,Jean,3,pip 84324,3333,Abel,2,cat I'd like to rearrange the output like below to put such records beside each other and separated with... (5 Replies)
Discussion started by: james2009
5 Replies

10. Shell Programming and Scripting

Convert rows to columns based on key and count

Team, I am having requirement to convert rows to columns Input is: key ,count, id1, pulse1, id2, pulse2 ,id3, pulse3 12, 2 , 14 , 56 , 15, 65 13, 3, 12, 32, 14, 23, 18, 54 22, 1 , 32, 42 Expected Out put: key, id,pulse 12, 14, 56 12, 15, 65 13 ,12, 32 13, 14 ,23 13, 18 ,54 22 ,32,... (3 Replies)
Discussion started by: syam1406
3 Replies
transfer::connect(n)					     Data transfer facilities					      transfer::connect(n)

__________________________________________________________________________________________________________________________________________________

NAME
transfer::connect - Connection setup SYNOPSIS
package require Tcl 8.4 package require snit ?1.0? package require transfer::connect ?0.1? transfer::connect object ?options...? object destroy object connect command _________________________________________________________________ DESCRIPTION
This package provides objects holding enough information to enable them to either connect to a counterpart, or to be connected to by said counterpart. I.e. any object created by this packages is always in one of two complementary modes, called active (the object initiates the connection) and passive (the object receives the connection). Of the two objects in a connecting pair one has to be configured for active mode, and the other then has to be configured for passive mode. This establishes which of the two partners connects to whom (the active to the other), or, who is waiting on whom (the passive on the other). Note that this is completely independent of the direction of any data transmission using the connection after it has been estab- lished. An active node can, after establishing the connection, either transmit or receive data. Equivalently the passive node can do the same after the waiting for it partner has ended. API
transfer::connect object ?options...? This command creates and configures a new connection object. The fully qualified name of the object command is returned as the result of the command. The recognized options are listed below. -mode mode This option specifies the mode the object is in. It is optional and defaults to active mode. The two possible modes are: active In this mode the two options -host and -port are relevant and specify the host and TCP port the object has to connect to. The host is given by either name or IP address. passive In this mode the option -host has no relevance and is ignored should it be configured. The only option the object needs is -port, and it specifies the TCP port on which the listening socket is opened to await the connection from the partner. -host hostname-or-ipaddr This option specifies the host to connect to in active mode, either by name or ip-address. An object configured for passive mode ignores this option. -port int For active mode this option specifies the port the object is expected to connect to. For passive mode however it is the port where the object creates the listening socket waiting for a connection. It defaults to 0, which allows the OS to choose the actual port to listen on. -encoding encodingname -eofchar eofspec -translation transspec These options are the same as are recognized by the builtin command fconfigure. They provide the configuration to be set for the channel between the two partners after it has been established, but before the callback is invoked (See method connect). object destroy This method destroys the object. This is safe to do for an active object when a connection has been started, as the completion callback is synchronous. For a passive object currently waiting for its parter to establish the connection however this is not safe and will cause errors later on, when the connection setup completes and tries to access the now missing data structures of the destroyed object. object connect command This method starts the connection setup per the configuration of the object. When the connection is established the callback command will be invoked with one additional argument, the channel handle of the socket over which data can be transfered. The detailed behaviour of the method depends on the configured mode. For an active object the connection setup is done syn- chronously. I.e. the object will wait until the connection is established. In that mode the method returns the empty string as its result. A passive object however operates asynchronously. The method will return immediately after a listener has been set up and the con- nection will be established in the background. In that mode the method returns the port number of the listening socket, for use by the caller, like transfering this information to the counterpart so that it may know where to connect to. This is necessary as the object might have been configured for port 0, allowing the OS to choose the actual port it will listen on. The listening port is closed immediately when the connection was established by the partner, to keep the time interval small within which a third party can connect to the port too. Even so it is recommended to use additional measures in the protocol outside of the connect and transfer object to ensure that a connection is not used with an unidentified/unauthorized partner. BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category transfer of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. KEYWORDS
active, channel, connection, passive, transfer COPYRIGHT
Copyright (c) 2006 Andreas Kupries <andreas_kupries@users.sourceforge.net> transfer 0.1 transfer::connect(n)
All times are GMT -4. The time now is 02:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy