1

I am not able to attach the CSV file, so I have attached the images so that the content is clear. I used awk and perl one liners but that is not giving any output. Please help me know, how can I get the rows with matching column number and column text/value.

can this be written in form of a script than command.

$ perl -F, -lane 'print if $F[8]==WINDOWS2000-2' Compare_20180715191103.csv

and

$ awk -F, -v OFS=, '$8 == "WINDOWS2000-2 "{ print }' Compare_20180715191103.csv

enter image description here

  • Well, column 8 (H) does not seem to contain the sting NAB-ITSA-SOLARIS nor NAB-ITSA-LINUX-7 (which should have been double quoted in the Perl code btw). – Kusalananda Jul 24 '18 at 14:23
  • You want a list of all entries where column H is equal to NAB-ITSA-LINUX-7? – Raman Sailopal Jul 24 '18 at 14:24
  • 3
    You write, "I am not able to attach the CSV file". That's good - I for one wouldn't want your full file. What I would want, though, is a cut down example that illustrates the issue. – roaima Jul 24 '18 at 14:59
  • 1
    Modern perl CSV-processing is entirely with Text::CSV_XS which is probably the best module for any language to process CSVs. And likely the fastest without doing the very same thing in C yourself. It's a shame that the module isn't a library that other languages can mooch off of. – Evan Carroll Jul 24 '18 at 19:33
  • 1
    Post some of the lines from your csv file on which you ran your perl/awk commands and not this snapshot of the csv file opened in excel. Also tell us what you see when you run the command: cat -vet Compare_20180715191103.csv from the command line ? – Rakesh Sharma Jul 25 '18 at 5:06
1

You need to do this:

perl -F, -lane 'print if $F[7] eq "NAB-ITSA-LINUX-7"' URT_Compare_20180715191103.csv

Once you take care of the below mentioned three things, you should be fine:

  1. "==" operator is for testing mathematical equality of it's operands. For string comparison, as in your case, eq is what you need.
  2. constant strings need to be quoted, OTW they get treated as subroutines.
  3. Since Perl array indices begin from zero, hence $F[8], would really refer to the ninth element of the array @F.

Underlying assumptions are:

  • The 8th field is a whole-field comparison and not partial. Meaning, the contents have to be exactly like you show here, not a character amiss.
  • There are no fields which have commas in them, for that would split midfield and the field count goes for a toss.
  • Not that it matters here, but just that you are aware that the line endings should ideally be Unix line endings (LF = \n). If they aren't, then you should convert them to it first.
  • Yea, may be assumptions you told are not fulfilled yet. – Dipit Sethi Jul 24 '18 at 19:07
  • @DipitSethi What does your comment even mean? Which of the assumptions? There are a number of people here offering their help in solving your problem: I suggest you cooperate and post an input sample, detailed description of edge cases, and meaningful answers. – simlev Jul 25 '18 at 9:13
0

Step 1: First check What is there in the 8th column,

awk -F, '{print $8}' URT_Compare_20180715191103.csv

Step 2: Then grep the desired pattern.

awk -F, '{print $8}' URT_Compare_20180715191103.csv | grep "WINDOWS2000-2"

Step 3: If you get your desired result in step2, try the below code.

awk -F,  '$8 ~ "WINDOWS2000-2" ' URT_Compare_20180715191103.csv
  • Din't work. It gave empty output again. – Dipit Sethi Jul 24 '18 at 18:57
  • @DipitSethi try the updated answer. – msp9011 Jul 24 '18 at 20:14
  • @DipitSethi if still getting empty output, share the file from vim editor – msp9011 Jul 24 '18 at 20:18
  • First step gives empty output, it is not able to get the column. – Dipit Sethi Jul 25 '18 at 7:32
  • then, the problem might be with delimiter. can you share the result of head -n5 URT_Compare_20180715191103.csv – msp9011 Jul 25 '18 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.