Sponsored Content
Top Forums Shell Programming and Scripting Print the row element till the next row element appear in a column Post 302728985 by Priyanka Chopra on Thursday 8th of November 2012 10:25:25 PM
Old 11-08-2012
Print the row element till the next row element appear in a column

Hi all

I have file with columns

Code:
F3       pathway      CPS
F2
H2
H4
H5
H6       no pathway    CMP
H7
H8
H9
H10

My expected output is

Code:
F3       pathway      CPS
F2        pathway      CPS
H2        pathway      CPS
H4       pathway      CPS
H5       pathway      CPS
H6       no pathway    CMP
H7       no pathway    CMP
H8       no pathway    CMP
H9       no pathway    CMP
H10      no pathway    CMP

Kindly guide me
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies

2. Shell Programming and Scripting

Print row if value in column 1 is the first occurence

Hi All, I would like to have a script which is able to perform the below. Print the whole row if column1 which is "0001" for the below example is the first occurrence. Subsequent "0001" occurrence will not be printed out and so on. Can any expert help ? Input: 0001 k= 40 0001 k= 2... (7 Replies)
Discussion started by: Raynon
7 Replies

3. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

4. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

5. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

6. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

8. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

9. Shell Programming and Scripting

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
3 Replies

10. UNIX for Beginners Questions & Answers

Print a row with the max number in a column

Hello, I have this table: chr1_16857_17742 - chr1 17369 17436 "ENST00000619216.1"; "MIR6859-1"; - 67 chr1_16857_17742 - chr1 14404 29570 "ENST00000488147.1"; "WASH7P"; - 885 chr1_16857_18061 - chr1 ... (5 Replies)
Discussion started by: coppuca
5 Replies
CPS(3pm)						User Contributed Perl Documentation						  CPS(3pm)

NAME
"CPS" - manage flow of control in Continuation-Passing Style OVERVIEW
The functions in this module implement or assist the writing of programs, or parts of them, in Continuation Passing Style (CPS). Briefly, CPS is a style of writing code where the normal call/return mechanism is replaced by explicit "continuations", values passed in to functions which they should invoke, to implement return behaviour. For more detail on CPS, see the SEE ALSO section. What this module implements is not in fact true CPS, as Perl does not natively support the idea of a real continuation (such as is created by a co-routine). Furthermore, for CPS to be efficient in languages that natively support it, their runtimes typically implement a lot of optimisation of CPS code, which the Perl interpreter would be unable to perform. Instead, CODE references are passed around to stand in their place. While not particularly useful for most regular cases, this becomes very useful whenever some form of asynchronous or event- based programming is being used. Continuations passed in to the body function of a control structure can be stored in the event handlers of the asynchronous or event-driven framework, so that when they are invoked later, the code continues, eventually arriving at its final answer at some point in the future. In order for these examples to make sense, a fictional and simple asynchronisation framework has been invented. The exact details of operation should not be important, as it simply stands to illustrate the point. I hope its general intention should be obvious. :) read_stdin_line( &on_line ); # wait on a line from STDIN, then pass it # to the handler function This module itself provides functions that manage the flow of control through a continuation passing program. They do not directly facilitate the flow of data through a program. That can be managed by lexical variables captured by the closures passed around. See the EXAMPLES section. For CPS versions of data-flow functionals, such as "map" and "grep", see also CPS::Functional. For backward compatibility with previous versions, the functions in "CPS::Functional" are also exported here; and "kunfold" is renamed to "kgenerate". This may be removed in a later version. SYNOPSIS
use CPS qw( kloop ); kloop( sub { my ( $knext, $klast ) = @_; print "Enter a number, or q to quit: "; read_stdin_line( sub { my ( $first ) = @_; chomp $first; return $klast->() if $first eq "q"; print "Enter a second number: "; read_stdin_line( sub { my ( $second ) = @_; print "The sum is " . ( $first + $second ) . " "; $knext->(); } ); } ); }, sub { exit } ); FUNCTIONS
In all of the following functions, the "&body" function can provide results by invoking its continuation / one of its continuations, either synchronously or asynchronously at some point later (via some event handling or other mechanism); the next invocation of "&body" will not take place until the previous one exits if it is done synchronously. They all take the prefix "k" before the name of the regular perl keyword or function they aim to replace. It is common in CPS code in other languages, such as Scheme or Haskell, to store a continuation in a variable called "k". This convention is followed here. kloop( &body, $k ) CPS version of perl's "while(true)" loop. Repeatedly calls the "body" code until it indicates the end of the loop, then invoke $k. $body->( $knext, $klast ) $knext->() $klast->() $k->() If $knext is invoked, the body will be called again. If $klast is invoked, the continuation $k is invoked. kwhile( &body, $k ) Compatibility synonym for "kloop"; it was renamed after version 0.10. New code should use "kloop" instead. kforeach( @items, &body, $k ) CPS version of perl's "foreach" loop. Calls the "body" code once for each element in @items, until either the items are exhausted or the "body" invokes its $klast continuation, then invoke $k. $body->( $item, $knext, $klast ) $knext->() $klast->() $k->() kdescendd( $root, &body, $k ) CPS version of recursive descent on a tree-like structure, defined by a function, "body", which when given a node in the tree, yields a list of child nodes. $body->( $node, $kmore ) $kmore->( @child_nodes ) $k->() The first value to be passed into "body" is $root. At each iteration, a node is given to the "body" function, and it is expected to pass a list of child nodes into its $kmore continuation. These will then be iterated over, in the order given. The tree-like structure is visited depth-first, descending fully into one subtree of a node before moving on to the next. This function does not provide a way for the body to accumulate a resultant data structure to pass into its own continuation. The body is executed simply for its side-effects and its continuation is invoked with no arguments. A variable of some sort should be shared between the body and the continuation if this is required. kdescendb( $root, &body, $k ) A breadth-first variation of "kdescendd". This function visits each child node of the parent, before iterating over all of these nodes's children, recursively until the bottom of the tree. kpar( @bodies, $k ) This CPS function takes a list of function bodies and calls them all immediately. Each is given its own continuation. Once every body has invoked its continuation, the main continuation $k is invoked. $body->( $kdone ) $kdone->() $k->() This allows running multiple operations in parallel, and waiting for them all to complete before continuing. It provides in a CPS form functionality similar to that provided in a more object-oriented fashion by modules such as Async::MergePoint or Event::Join. kpareach( @items, &body, $k ) This CPS function takes a list of items and a function body, and calls the body immediately once for each item in the list. Each invocation is given its own continuation. Once every body has invoked its continuation, the main continuation $k is invoked. $body->( $item, $kdone ) $kdone->() $k->() This is similar to "kforeach", except that the body is started concurrently for all items in the list list, rather than each item waiting for the previous to finish. kseq( @bodies, $k ) This CPS function takes a list of function bodies and calls them each, one at a time in sequence. Each is given a continuation to invoke, which will cause the next body to be invoked. When the last body has invoked its continuation, the main continuation $k is invoked. $body->( $kdone ) $kdone->() $k->() A benefit of this is that it allows a long operation that uses many continuation "pauses", to be written without code indenting further and further to the right. Another is that it allows easy skipping of conditional parts of a computation, which would otherwise be tricky to write in a CPS form. See the EXAMPLES section. GOVERNORS
All of the above functions are implemented using a loop which repeatedly calls the body function until some terminating condition. By controlling the way this loop re-invokes itself, a program can control the behaviour of the functions. For every one of the above functions, there also exists a variant which takes a CPS::Governor object as its first argument. These functions use the governor object to control their iteration. kloop( &body, $k ) gkloop( $gov, &body, $k ) kforeach( @items, &body, $k ) gkforeach( $gov, @items, &body, $k ) etc... In this way, other governor objects can be constructed which have different running properties; such as interleaving iterations of their loop with other IO activity in an event-driven framework, or giving rate-limitation control on the speed of iteration of the loop. CPS UTILITIES
These function names do not begin with "k" because they are not themselves CPS primatives, but may be useful in CPS-oriented code. $kfunc = liftk { BLOCK } $kfunc = liftk( &func ) Returns a new CODE reference to a CPS-wrapped version of the code block or passed CODE reference. When $kfunc is invoked, the function &func is called in list context, being passed all the arguments given to $kfunc apart from the last, expected to be its continuation. When &func returns, the result is passed into the continuation. $kfunc->( @func_args, $k ) $k->( @func_ret ) The following are equivalent print func( 1, 2, 3 ); my $kfunc = liftk( &func ); $kfunc->( 1, 2, 3, sub { print @_ } ); Note that the returned wrapper function only has one continuation slot in its arguments. It therefore cannot be used as the body for "kloop()", "kforeach()" or "kgenerate()", because these pass two continuations. There does not exist a "natural" way to lift a normal call/return function into a CPS function which requires more than one continuation, because there is no way to distinguish the different named returns. $func = dropk { BLOCK } $kfunc $func = dropk $waitfunc, $kfunc Returns a new CODE reference to a plain call/return version of the passed CPS-style CODE reference. When the returned ("dropped") function is called, it invokes the passed CPS function, then waits for it to invoke its continuation. When it does, the list that was passed to the continuation is returned by the dropped function. If called in scalar context, only the first value in the list is returned. $kfunc->( @func_args, $k ) $k->( @func_ret ) $waitfunc->() @func_ret = $func->( @func_args ) Given the following trivial CPS function: $kadd = sub { $_[2]->( $_[0] + $_[1] ) }; The following are equivalent $kadd->( 10, 20, sub { print "The total is $_[0] " } ); $add = dropk { } $kadd; print "The total is ".$add->( 10, 20 )." "; In the general case the CPS function hasn't yet invoked its continuation by the time it returns (such as would be the case when using any sort of asynchronisation or event-driven framework). For "dropk" to actually work in this situation, it requires a way to run the event framework, to cause it to process events until the continuation has been invoked. This is provided by the block, or the first passed CODE reference. When the returned function is invoked, it repeatedly calls the block or wait function, until the CPS function has invoked its continuation. EXAMPLES
Returning Data From Functions No facilities are provided directly to return data from CPS body functions in "kloop", "kpar" and "kseq". Instead, normal lexical variable capture may be used here. my $bat; my $ball; kpar( sub { my ( $k ) = @_; get_bat( on_bat => sub { $bat = shift; goto &$k } ); }, sub { my ( $k ) = @_; serve_ball( on_ball => sub { $ball = shift; goto &$k } ); }, sub { $bat->hit( $ball ); }, ); The body function can set the value of a variable that it and its final continuation both capture. Using "kseq" For Conditionals Consider the call/return style of code A(); if( $maybe ) { B(); } C(); We cannot easily write this in CPS form without naming C twice kA( sub { $maybe ? kB( sub { kC() } ) : kC(); } ); While not so problematic here, it could get awkward if C were in fact a large code block, or if more than a single conditional were employed in the logic; a likely scenario. A further issue is that the logical structure becomes much harder to read. Using "kseq" allows us to name the continuation so each arm of "kmaybe" can invoke it indirectly. kseq( &kA, sub { my $k = shift; $maybe ? kB( $k ) : goto &$k; }, &kC ); SEE ALSO
o CPS::Functional - functional utilities in Continuation-Passing Style o http://en.wikipedia.org/wiki/Continuation-passing_style <http://en.wikipedia.org/wiki/Continuation-passing_style> on wikipedia o Coro - co-routines in Perl ACKNOWLEDGEMENTS
Matt S. Trout (mst) <mst@shadowcat.co.uk> - for the inspiration of "kpareach" and with apologies to for naming of the said. ;) AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-06-27 CPS(3pm)
All times are GMT -4. The time now is 09:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy