Sponsored Content
Top Forums Shell Programming and Scripting counting lines containing two column field values with awk Post 302532792 by origamisven on Wednesday 22nd of June 2011 05:52:22 AM
Old 06-22-2011
counting lines containing two column field values with awk

Hello everybody,
I'm trying to count the number of consecutive lines in a text file which have two distinctive column field values. These lines may appear in several line blocks within the file, but I only want a single block to be counted.

This was my first approach to tackle the problem (I'm a beginner, so be gentle Smilie) ...

# find line in which pattern INT appears for the first time in file topology
Code:
NR_1st_int=$(awk -v var="$INT" '$0~var {print NR}' topology | sed '1 !d')

# print molecule number of field 5 in that line into MOL
Code:
MOL_int=$(awk -v line_int="$NR_1st_int" 'NR==line_int {print $5}' topology)

# count number of lines that INT and MOL appear IN THE FILE
Code:
NINT=$(awk -v var1="$INT" -v var2="$MOL_int" 'BEGIN { count=0 } { if (( $4 == var1 ) && ( $5 == var2 )) count++} END{print count}' topology)

The main problem is that this (bad) code does not restrict the number NINT to a single block of occurring lines, which I need.

I've tried working with some loops but I messed it up every time. Can you help me out?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with switching field/column values

Hi all, I need some help on switching field/column values. For example I have a file name data.txt which contains: a b a b a b and I want to switch a and b and save it to the same file. the file data.txt then will have: b a b a b a The problem is, well, I know how to... (7 Replies)
Discussion started by: sonyd8
7 Replies

2. UNIX for Dummies Questions & Answers

Awk counting lines with field match

Hi, Im trying to create a script that reads throught every line in a file and then counts how many lines there with a certain field that matches a input, and also ausing another awk it has to do the same as the above but to then use sort anduniq to get rid of all the unique lines with another... (8 Replies)
Discussion started by: fredted40x
8 Replies

3. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

4. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies

5. UNIX for Dummies Questions & Answers

awk to sum column field from duplicate row/lines

Hello, I am new to Linux environment , I working on Linux script which should send auto email based on the specific condition from log file. Below is the sample log file Name m/c usage abc xxx 10 abc xxx 20 abc xxx 5 xyz ... (6 Replies)
Discussion started by: asjaiswal
6 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

[Solved] Counting The Number of Lines Between Values with Multiple Variables

Hey everyone, I have a bunch of lines with values in field 4 that I am interested in. If these values are between 1 and 3 I want it to count all these values to all be counted together and then have the computer print out LOW and the number of lines with those values in between 1 and 3,... (2 Replies)
Discussion started by: VagabondGold
2 Replies

8. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

9. Shell Programming and Scripting

awk to filter out lines containing unique values in a specified column

Hi, I have multiple files that each contain four columns of strings: File1: Code: 123 abc gfh 273 456 ddff jfh 837 789 ghi u4u 395 File2: Code: 123 abc dd fu 456 def 457 nd 891 384 djh 783 I want to compare the strings in Column 1 of File 1 with each other file and Print in... (3 Replies)
Discussion started by: owwow14
3 Replies

10. Shell Programming and Scripting

Sum column values matching other field

this is part of a KT i am going thru. i am writing a script in bash shell, linux where i have 2 columns where 1st signifies the nth hour like 00, 01, 02...23 and 2nd the file size. sample data attached. Desired output is 3 columns which will give the nth hour, number of entries in nth hour and... (3 Replies)
Discussion started by: alpha_1
3 Replies
Create and Destroy Topologies(3)			     Hardware Locality (hwloc)				  Create and Destroy Topologies(3)

NAME
Create and Destroy Topologies - Functions int hwloc_topology_init (hwloc_topology_t *topologyp) int hwloc_topology_load (hwloc_topology_t topology) void hwloc_topology_destroy (hwloc_topology_t topology) void hwloc_topology_check (hwloc_topology_t topology) Function Documentation void hwloc_topology_check (hwloc_topology_ttopology) Run internal checks on a topology structure. The program aborts if an inconsistency is detected in the given topology. Parameters: topology is the topology to be checked Note: This routine is only useful to developers. The input topology should have been previously loaded with hwloc_topology_load(). void hwloc_topology_destroy (hwloc_topology_ttopology) Terminate and free a topology context. Parameters: topology is the topology to be freed int hwloc_topology_init (hwloc_topology_t *topologyp) Allocate a topology context. Parameters: topologyp is assigned a pointer to the new allocated context. Returns: 0 on success, -1 on error. int hwloc_topology_load (hwloc_topology_ttopology) Build the actual topology. Build the actual topology once initialized with hwloc_topology_init() and tuned with Configure Topology Detection routines. No other routine may be called earlier using this topology context. Parameters: topology is the topology to be loaded with objects. Returns: 0 on success, -1 on error. See also: Configure Topology Detection Author Generated automatically by Doxygen for Hardware Locality (hwloc) from the source code. Version 1.4.1 Mon Feb 27 2012 Create and Destroy Topologies(3)
All times are GMT -4. The time now is 10:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy