Sponsored Content
Top Forums Shell Programming and Scripting Print values within groups of lines with awk Post 302958598 by Ophiuchus on Friday 23rd of October 2015 03:16:27 PM
Old 10-23-2015
Print values within groups of lines with awk

Hello to all,

I'm trying to print the value corresponding to the words A, B, C, D, E. These words could appear sometimes and sometimes not inside each group of lines. Each group of lines begins with "ZYX".

My issue with current code is that should print values for 3 groups and only is printing for 2 groups (group 1 and group 3) and I'm not sure why.

What is missing in my current code to fix this?

The current output is:
Code:
123|3|22|56|881
711||988||444

and desired ouput:
Code:
123|3|22|56|881
332|453|11||
711||988||444

My input file is:
Code:
ZYX
A = 123
B = 3
C = 22
D = 56
E = 881
ZYX
A = 332
B = 453
C = 11
ZYX
A = 711
C = 988
E = 444

My current code is:
Code:
awk '/ZYX/{a="";b="";c="";d="";e=""} 
         /A/ {a=$3}
         /B/ {b=$3}
         /C/ {c=$3} 
         /D/ {d=$3}
         /E/ {e=$3; print a"|"b"|"c"|"d"|"e}' file


Thaaks for any help.

Regards
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print a key with its all values using awk/others

input COL1 a1 b1 c1 d1 e1 f1 C1 10 10 10 100 100 1000 C2 20 20 200 200 200 2000 output C1 a1 10 1 C1 b1 10 1 C1 c1 10 1 C1 d1 100 2 C1 e1 100 2 C1 f1 1000 3 C2 ... (12 Replies)
Discussion started by: ruby_sgp
12 Replies

2. Shell Programming and Scripting

awk- looping through groups of lines

Hello, I'm working with a file that has three columns. The first one represents a certain channel and the third one a timestamp (second one is not important). Example input is as follows: 2513 12 10.771 2513 13 10.771 2513 14 10.771 2513 15 10.771 2644 8 10.771 ... (6 Replies)
Discussion started by: acsg
6 Replies

3. UNIX for Dummies Questions & Answers

Only print lines with 3 numeric values

Hey guys & gals, I am hoping for some advice on a sed or awk command that will allow to only print lines from a file that contain 3 numeric values. From previous searches here I saw that ygemici used the sed command to remove lines containing more than 3 numeric values ; however how... (3 Replies)
Discussion started by: TAPE
3 Replies

4. Shell Programming and Scripting

Print lines containing same values in a group

Hi, I have a table like this: Name A1 A2 A3 B1 B2 B3 111 abc abc abc cbc cbc cbc 222 acv acv n_n bbc bbc bbc 333 bvv bvv bvv cBx ccx ccx 444 ttk ttk ttk kke kke kke 555 mcr mcr mcr mcr mcr mcr The 1st column is just names... (3 Replies)
Discussion started by: polsum
3 Replies

5. Shell Programming and Scripting

How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts, I am trying to get the output from a matching pattern but unable to construct the awk command: file : aa bb cc 11 dd aa cc 33 cc 22 45 68 aa 33 44 44 dd aa cc 37 aa 33 44 67 I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and... (3 Replies)
Discussion started by: rveri
3 Replies

6. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

awk print values between consecutive lines

I have a file in below format: file01.txt TERM TERM TERM ABC 12315 68.53 12042013 165144 ABC 12315 62.12 12042013 165145 ABC 12315 122.36 12052013 165146 ABC 12315 582.18 12052013 165147 ABC 12316 2.36 12052013 165141 ABC 12316 ... (8 Replies)
Discussion started by: alex2005
8 Replies

8. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

9. Shell Programming and Scripting

How to print lines that have values in certain columns ?

Hi, everyone I have a dataset like this: A B C D A C C D E F G H F D K Y X A K K C Gsome of columns have no values in each line. I want to print all lines that have 1/2/3/4 values, export separately to four files. What I expected is like this: file1 Y file 2 A C X Afile 3... (3 Replies)
Discussion started by: nengcheng
3 Replies

10. UNIX for Beginners Questions & Answers

Print lines based upon unique values in Nth field

For some reason I am having difficulty performing what should be a fairly easy task. I would like to print lines of a file that have a unique value in the first field. For example, I have a large data-set with the following excerpt: PS003,001 MZMWR/ L-DWD// * PS003,001... (4 Replies)
Discussion started by: jvoot
4 Replies
ModPerl::Code(3)					User Contributed Perl Documentation					  ModPerl::Code(3)

NAME
$class::Const - Perl Interface for $class Constants SYNOPSIS
CONSTANTS
EOF my $groups = $data{$class}; for my $group (sort keys %$groups) { print $fh <<"EOF"; ":$group" use $class::Const -compile qw(:$group); The ":$group" group is for XXX constants. EOF for my $const (sort @{ $groups->{$group} }) { print $fh "=head3 C<$class::$const> "; } } print $fh "=cut "; } } sub generate_constants_lookup_doc { my ($data) = @_; foreach my $class (sort keys %$Apache2::ConstantsTable) { my $groups = $Apache2::ConstantsTable->{$class}; my $constants = [sort map { @$_ } values %$groups]; constants_lookup_code_doc($constants, $class, $data); } } sub generate_constants_group_lookup_doc { my ($data) = @_; foreach my $class (sort keys %$Apache2::ConstantsTable) { my $groups = $Apache2::ConstantsTable->{$class}; constants_group_lookup_code_doc($class, $groups, $data); } } sub constants_group_lookup_code_doc { my ($class, $groups, $data) = @_; my @tags; my @code; while (my ($group, $constants) = each %$groups) { $data->{$class}{$group} = [ map { my @ifdef = constants_ifdef($_); s/^($constant_prefixes)_?//o; $seen_const{$class}{$_}++; $_; } @$constants ]; } } sub constants_lookup_code_doc { my ($constants, $class, $data) = @_; my (%switch, %alias); %alias = %shortcuts; my $postfix = lc $class; my $package = $class . '::'; my $package_len = length $package; my $func = canon_func(qw(constants lookup), $postfix); for (@$constants) { if (s/^($constant_prefixes)(_)?//o) { $alias{$_} = join $2 || "", $1, $_; } else { $alias{$_} ||= $_; } next unless /^([A-Z])/; push @{ $switch{$1} }, $_; } for my $key (sort keys %switch) { my $names = $switch{$key}; for my $name (@$names) { my @ifdef = constants_ifdef($alias{$name}); push @{ $data->{$class}{other} }, $name unless $seen_const{$class}{$name} } } } sub generate_exports { my ($self, $c_fh) = @_; require ModPerl::WrapXS; ModPerl::WrapXS->generate_exports($c_fh); } # src/modules/perl/*.c files needed to build APR/APR::* outside # of mod_perl.so sub src_apr_ext { return map { "modperl_$_" } (qw(error bucket), map { "common_$_" } qw(util log)); } 1; __END__ NAME
ModPerl::Code - Generate mod_perl glue code SYNOPSIS
use ModPerl::Code (); my $code = ModPerl::Code->new; $code->generate; DESCRIPTION
This module provides functionality for generating mod_perl glue code. Reason this code is generated rather than written by hand include: consistency thin and clean glue code enable/disable features (without #ifdefs) adapt to changes in Apache experiment with different approaches to gluing AUTHOR
Doug MacEachern perl v5.18.2 2015-06-18 ModPerl::Code(3)
All times are GMT -4. The time now is 12:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy