1

I have a .csv files as shown below:

RepDate;FOO1;ProcessId;ClusterName;ProcessType
19700101;-1;Job_1000969943637142044;PROD;auswertung.mpeTestPlayer.xml
20181231;root;Job_107953405843118835;PROD;mpeEditTablesImport.xml

Now we want to filter rows when the ProcessType is mpeEditTablesImport.xml and ClusterName is PROD.

I have tried the following code, but it is giving empty output:

awk -F "," '{ if(($ProcessType == "mpeEditTablesImport.xml") && ($ClusterName ==  "PROD")) { print } }' OutputFile.csv > OutputFile_Filtered.csv | tail

I am not sure what I am doing wrong, any help is useful.

4
  • 1
    What are $ProcessType and $ClusterName? Are you expecting those to match columns with that name?
    – terdon
    Commented May 19, 2022 at 9:18
  • @tredon those are my column names. Commented May 19, 2022 at 9:21
  • That's not how awk works, I'm afraid. See answer.
    – terdon
    Commented May 19, 2022 at 9:22
  • If you want to refer to columns by name rather than number, you'll need to use tools that have good support for CSV built in, like miller, csvkit, or GNU datamash; or programming languages with CSV-parsing libraries like perl's Text::CSV or python's csv. In theory, you could write a decent CSV parser in awk. In practice, it's easier and better to use existing tools.
    – cas
    Commented May 19, 2022 at 9:46

1 Answer 1

1

Awk works by splitting the line into fields which are then referred to by the field number and not by the text in the field. So you don't use $field1 for the first field, you use $1 and then $2 for the second and so on. What you want is:

$ awk -F';' '$5=="mpeEditTablesImport.xml" && $4=="PROD"'  file
20181231;root;Job_107953405843118835;PROD;mpeEditTablesImport.xml

In this case, you could also simply use grep:

$ grep ';PROD;mpeEditTablesImport.xml$' file
20181231;root;Job_107953405843118835;PROD;mpeEditTablesImport.xml
1
  • It worked, thanks for the explanation. Commented May 19, 2022 at 11:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .