Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Grep A Column Based On Column Name Post 302981283 by drl on Friday 9th of September 2016 09:34:09 AM
Old 09-09-2016
Hi.

A code to work with this kind of data and requests is csvtool. This can be found in repositories for Debian (as noted below), Fedora 23 (Workstation), etc.:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate manipulation of CSV data.
# csvtool, Debian repository, and:
# https://github.com/Chris00/ocaml-csv
#
# align: http://kinzler.com/me/align/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C csvtool align

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
sed 's/  */,/g' $FILE |
tee f1 |
csvtool namedcol Cat,Dog - |
align -s /,

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30
csvtool - ( /usr/bin/csvtool, 2014-08-06 )
align 1.7.0

-----
 Input data file data1:
Dog        Cat
fido       sneaky
dopey      poptart
ears       whisker
barky      herd

-----
 Results:
Cat     Dog
sneaky  fido
poptart dopey
whisker ears
herd    barky

The script could be modified to allow requests to come in from the command line as well.

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep all the columns based on a particular column

This is the data file that I have 21879, 0, 22, 58, 388 0, -1, 300, 1219172589765, 1708, 0, 200, 21891, 0, 0, 33, 309 0, -1, 300, 1219172591478, 1768, 0, 200, 22505, 0, 0, 33, 339 0, -1, 300, 1219172593251, 1738, 0, 200, 21888, 0, 1, 33, 308 0, -1, 300, 1219172594995, 633, 0, 200, 24878,... (2 Replies)
Discussion started by: pmallur
2 Replies

2. Shell Programming and Scripting

need to remove duplicates based on key in first column and pattern in last column

Given a file such as this I need to remove the duplicates. 00060011 PAUL BOWSTEIN ad_waq3_921_20100826_010517.txt 00060011 PAUL BOWSTEIN ad_waq3_921_20100827_010528.txt 0624-01 RUT CORPORATION ad_sade3_10_20100827_010528.txt 0624-01 RUT CORPORATION ... (13 Replies)
Discussion started by: script_op2a
13 Replies

3. Shell Programming and Scripting

to add special tag to a column based on column condition

Hi All, I have following html code <TR><TD>9</TD><TD>AR_TVR_TBS </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR> <TR><TD>10</TD><TD>ASCV_SMY_TBS </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR> <TR><TD>11</TD><TD>ARC_TBS ... (9 Replies)
Discussion started by: ckwan
9 Replies

4. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

5. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

6. Shell Programming and Scripting

Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has... (2 Replies)
Discussion started by: Ganesh L
2 Replies

7. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

8. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

9. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

10. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies
Widgets::ListBox::MultiColumn(3pm)			User Contributed Perl Documentation			Widgets::ListBox::MultiColumn(3pm)

NAME
Curses::Widgets::ListBox::MultiColumn - Multi-Column List Box Widgets MODULE VERSION
$Id: MultiColumn.pm,v 0.1 2002/11/14 01:28:49 corliss Exp corliss $ SYNOPSIS
use Curses::Widgets::ListBox::MultiColumn; $lb = Curses::Widgets::ListBox::MultiColumn->new({ COLUMNS => [0, 5, 10, 3, 3], LISTITEMS => [@list], }); $lb->draw($mwh, 1); See the Curses::Widgets pod for other methods. REQUIREMENTS
Curses Curses::Widgets Curses::Widgets::ListBox DESCRIPTION
Curses::Widgets::ListBox::MultiColumn is an extension of the standard Curses::Widgets::ListBox that allows a list of columns, with each column a specified width. METHODS
new (inherited from Curses::Widgets) $tm = Curses::Widgets::ListBox->new({ COLUMNS => [0, 5, 10, 3, 3], LISTITEMS => [@list], HEADERS => [@headers], HEADERCOLFG => 'white', HEADERCOLBG => 'green', BIGHEADER => 1, }); All of the same key values apply here as they do for the parent class Curses::Widgets::ListBox. In addition, the following new keys are defined: Key Default Description ============================================================ COLUMNS [] Column widths LISTITEMS [] List of list values HEADERS [] Column header labels HEADERFGCOL undef Header foreground colour HEADERBGCOL undef Header background colour BIGHEADER 0 Use more graphics for the header KEYINDX 0 Index of key column If headers are defined but one or both of the header colours are not, then they will default to the widget fore and background. NOTE: Headers take up more lines in addition to the border (one line for the normal, small header, two lines for the larger). You need to take that into account when setting the geometry. If no labels are passed in the HEADERS array, no space will be used for the headers. The KEYINDX value is currently only used to match keystrokes against for quick navigation. draw $lb->draw($mwh, 1); The draw method renders the list box in its current state. This requires a valid handle to a curses window in which it will render itself. The optional second argument, if true, will cause the field's text cursor to be rendered as well. HISTORY
1999/12/29 -- Original list box widget in functional model 2001/07/05 -- First incarnation in OO architecture AUTHOR
/COPYRIGHT (c) 2001 Arthur Corliss (corliss@digitalmages.com) perl v5.8.8 2006-09-14 Widgets::ListBox::MultiColumn(3pm)
All times are GMT -4. The time now is 05:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy