Sponsored Content
Top Forums UNIX for Beginners Questions & Answers awk with sed to combine lines and remove specific odd # pattern from line Post 303033775 by RavinderSingh13 on Thursday 11th of April 2019 01:53:32 PM
Old 04-11-2019
Hello cmccabe,

My previous solution works with reading Input_file 2 times, try following with reading Input_file single time only.
Code:
awk '!NF || (!/^xxxx_[0-9]+/ && !/^[0-9]+/){
  print
  next
}
!/^xxxx_[0-9]+/{
  line=$0
  next
}
{
  for(i=1;i<=NF;i++){
     val=$i
     sub(/.*_/,"",val)
     if(val%2==0){
         value=(value?value OFS:"")$i
     }
  }
  print value,line
  line=val=value=""
}
END{
  if(line){
     print line
  }
}'   Input_file

Output will be as follows.
Code:
xxxx_0002 00-0000-Lname-Fname-REPEAT
xxxx_0008 111111-yyyy
xxxx_0006 111111-yyyy-0
FileName_ID

FileName_ID

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

SImple HELP! how to combine two lines together using sed or awk..

hi..im new to UNIX... ok i have this information in the normal shell... there are 2 lines display like this: h@hotmail.com k@hotmail.com i want it to display like this with a space betweem them h@hotmail.com k@hotmail.com the information is stored in a text file.... anyone... (10 Replies)
Discussion started by: forevercalz
10 Replies

2. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies

3. Shell Programming and Scripting

NAWK to remove lines that matches a specific pattern

Hi, I have requirement that I need to split my input file into two files based on a search pattern "abc" For eg. my input file has below content abc defgh zyx I need file 1 with abc and file2 with defgh zyx I can use grep command to acheive this. But with grep I need... (8 Replies)
Discussion started by: sbhuvana20
8 Replies

4. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

5. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

6. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

7. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

8. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

9. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

10. Shell Programming and Scripting

sed to remove next 15 lines after pattern in Solaris

Team, I am trying to use sed to delete 15 lines, after pattern patch, which includes the pattern as well in Solaris. I used the below command, as we do it Linux, but it's not working as expected in Solaris. I am getting the error as "garbled".sed '/\/table/,+15d' status.html sed: command... (8 Replies)
Discussion started by: Nagaraj R
8 Replies
IO::Async::Process(3pm) 				User Contributed Perl Documentation				   IO::Async::Process(3pm)

NAME
"IO::Async::Process" - start and manage a child process SYNOPSIS
use IO::Async::Process; use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $process = IO::Async::Process->new( command => [ "tr", "a-z", "n-za-m" ], stdin => { from => "hello world ", }, stdout => { on_read => sub { my ( $stream, $buffref ) = @_; while( $$buffref =~ s/^(.*) // ) { print "Rot13 of 'hello world' is '$1' "; } return 0; }, }, on_finish => sub { $loop->stop; }, ); $loop->add( $process ); $loop->run; DESCRIPTION
This subclass of IO::Async::Notifier starts a child process, and invokes a callback when it exits. The child process can either execute a given block of code (via fork(2)), or a command. EVENTS
The following events are invoked, either using subclass methods or CODE references in parameters: on_finish $exitcode Invoked after the process has exited by normal means (i.e. an exit(2) syscall from a process, or "return"ing from the code block), and has closed all its file descriptors. on_exception $exception, $errno, $exitcode Invoked when the process exits by an exception from "code", or by failing to exec(2) the given command. $errno will be a dualvar, containing both number and string values. Note that this has a different name and a different argument order from "Loop->open_child"'s "on_error". If this is not provided and the process exits with an exception, then "on_finish" is invoked instead, being passed just the exit code. CONSTRUCTOR
$process = IO::Async::Process->new( %args ) Constructs a new "IO::Async::Process" object and returns it. Once constructed, the "Process" will need to be added to the "Loop" before the child process is started. PARAMETERS
The following named parameters may be passed to "new" or "configure": on_finish => CODE on_exception => CODE CODE reference for the event handlers. Once the "on_finish" continuation has been invoked, the "IO::Async::Process" object is removed from the containing "IO::Async::Loop" object. The following parameters may be passed to "new", or to "configure" before the process has been started (i.e. before it has been added to the "Loop"). Once the process is running these cannot be changed. command => ARRAY or STRING Either a reference to an array containing the command and its arguments, or a plain string containing the command. This value is passed into perl's exec(2) function. code => CODE A block of code to execute in the child process. It will be called in scalar context inside an "eval" block. setup => ARRAY Optional reference to an array to pass to the underlying "Loop" "spawn_child" method. fdn => HASH A hash describing how to set up file descriptor n. The hash may contain the following keys: via => STRING Configures how this file descriptor will be configured for the child process. Must be given one of the following mode names: pipe_read The child will be given the writing end of a pipe(2); the parent may read from the other. pipe_write The child will be given the reading end of a pipe(2); the parent may write to the other. Since an EOF condition of this kind of handle cannot reliably be detected, "on_finish" will not wait for this type of pipe to be closed. pipe_rdwr Only valid on the "stdio" filehandle. The child will be given the reading end of one pipe(2) on STDIN and the writing end of another on STDOUT. A single Stream object will be created in the parent configured for both filehandles. socketpair The child will be given one end of a socketpair(2); the parent will be given the other. The family of this socket may be given by the extra key called "family"; defaulting to "unix". The socktype of this socket may be given by the extra key called "socktype"; defaulting to "stream". If the type is not "SOCK_STREAM" then a IO::Async::Socket object will be constructed for the parent side of the handle, rather than "IO::Async::Stream". Once the filehandle is set up, the "fd" method (or its shortcuts of "stdin", "stdout" or "stderr") may be used to access the "IO::Async::Handle"-subclassed object wrapped around it. The value of this argument is implied by any of the following alternatives. on_read => CODE The child will be given the writing end of a pipe. The reading end will be wrapped by an "IO::Async::Stream" using this "on_read" callback function. into => SCALAR The child will be given the writing end of a pipe. The referenced scalar will be filled by data read from the child process. This data may not be available until the pipe has been closed by the child. from => STRING The child will be given the reading end of a pipe. The string given by the "from" parameter will be written to the child. When all of the data has been written the pipe will be closed. stdin => ... stdout => ... stderr => ... Shortcuts for "fd0", "fd1" and "fd2" respectively. stdio => ... Special filehandle to affect STDIN and STDOUT at the same time. This filehandle supports being configured for both reading and writing at the same time. METHODS
$pid = $process->pid Returns the process ID of the process, if it has been started, or "undef" if not. Its value is preserved after the process exits, so it may be inspected during the "on_finish" or "on_exception" events. $process->kill( $signal ) Sends a signal to the process $running = $process->is_running Returns true if the Process has been started, and has not yet finished. $exited = $process->is_exited Returns true if the Process has finished running, and finished due to normal exit(2). $status = $process->exitstatus If the process exited due to normal exit(2), returns the value that was passed to exit(2). Otherwise, returns "undef". $exception = $process->exception If the process exited due to an exception, returns the exception that was thrown. Otherwise, returns "undef". $errno = $process->errno If the process exited due to an exception, returns the numerical value of $! at the time the exception was thrown. Otherwise, returns "undef". $errstr = $process->errstr If the process exited due to an exception, returns the string value of $! at the time the exception was thrown. Otherwise, returns "undef". $stream = $process->fd( $fd ) Returns the IO::Async::Stream or IO::Async::Socket associated with the given FD number. This must have been set up by a "configure" argument prior to adding the "Process" object to the "Loop". The returned object have its read or write handle set to the other end of a pipe or socket connected to that FD number in the child process. Typically, this will be used to call the "write" method on, to write more data into the child, or to set an "on_read" handler to read data out of the child. The "on_closed" event for these streams must not be changed, or it will break the close detection used by the "Process" object and the "on_finish" event will not be invoked. $stream = $process->stdin $stream = $process->stdout $stream = $process->stderr $stream = $process->stdio Shortcuts for calling "fd" with 0, 1, 2 or "io" respectively, to obtain the IO::Async::Stream representing the standard input, output, error, or combined input/output streams of the child process. EXAMPLES
Capturing the STDOUT stream of a process By configuring the "stdout" filehandle of the process using the "into" key, data written by the process can be captured. my $stdout; my $process = IO::Async::Process->new( command => [ "writing-program", "arguments" ], stdout => { into => $stdout }, on_finish => sub { print "The process has finished, and wrote: "; print $stdout; } ); $loop->add( $process ); Note that until "on_finish" is invoked, no guarantees are made about how much of the data actually written by the process is yet in the $stdout scalar. See also the "run_child" method of IO::Async::Loop. To handle data more interactively as it arrives, the "on_read" key can instead be used, to provide a callback function to invoke whenever more data is available from the process. my $process = IO::Async::Process->new( command => [ "writing-program", "arguments" ], stdout => { on_read => sub { my ( $stream, $buffref ) = @_; while( $$buffref =~ s/^(.*) // ) { print "The process wrote a line: $1 "; } return 0; }, }, on_finish => sub { print "The process has finished "; } ); $loop->add( $process ); If the code to handle data read from the process isn't available yet when the object is constructed, it can be supplied later by using the "configure" method on the "stdout" filestream at some point before it gets added to the Loop. In this case, "stdin" should be configured using "pipe_read" in the "via" key. my $process = IO::Async::Process->new( command => [ "writing-program", "arguments" ], stdout => { via => "pipe_read" }, on_finish => sub { print "The process has finished "; } ); $process->stdout->configure( on_read => sub { my ( $stream, $buffref ) = @_; while( $$buffref =~ s/^(.*) // ) { print "The process wrote a line: $1 "; } return 0; }, ); $loop->add( $process ); Sending data to STDIN of a process By configuring the "stdin" filehandle of the process using the "from" key, data can be written into the "STDIN" stream of the process. my $process = IO::Async::Process->new( command => [ "reading-program", "arguments" ], stdin => { from => "Here is the data to send " }, on_finish => sub { print "The process has finished "; } ); $loop->add( $process ); The data in this scalar will be written until it is all consumed, then the handle will be closed. This may be useful if the program waits for EOF on "STDIN" before it exits. To have the ability to write more data into the process once it has started. the "write" method on the "stdin" stream can be used, when it is configured using the "pipe_write" value for "via": my $process = IO::Async::Process->new( command => [ "reading-program", "arguments" ], stdin => { via => "pipe_write" }, on_finish => sub { print "The process has finished "; } ); $loop->add( $process ); $process->stdin->write( "Here is some more data " ); AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-10-24 IO::Async::Process(3pm)
All times are GMT -4. The time now is 11:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy