0

I have a daily script that needs to extract an error log and remove users from the master csv file, before uploading into the database. I'm able to use awk to extract the first column with great results. However, the error I get back from my 3rd party application contains a comma in the error column. This is preventing an exact match and causing issues.

This is a sample of the error file I get back

"USER_ID","FIRSTNAME","LASTNAME","ERROR"
"CA781558","Dani","Roper","parent is inactive, cannot update record"
"BT055163","Alexis","Richardo","parent is inactive, cannot update record"
"LN764767","Peter","Rajosz","no parent record, update denied"
"SG839717","Jerry","Alindos","parent is inactive, cannot update record"

I need to match exactly for "parent is inactive, cannot update record", to use this to update the parent record, so it can be updated. Likewise, I need to match the "no parent record" so I can add a parent for this record and process. In reality, I have a slew of similar error messages that require different actions. Matching on the exact string, with the comma, is critical.

The expected output is:

"USER_ID"
"CA781558"
"BT055163"
"SG839717"
1
  • 1
    Use a language with a proper CSV parser. Commented Apr 26, 2016 at 17:31

3 Answers 3

1

Using awk you can do this:

s='parent is inactive, cannot update record'
awk -v s="\"$s\"" -F, 'NR==1 || $0 ~ s{print $1}' file

"USER_ID"
"CA781558"
"BT055163"
"SG839717"
Sign up to request clarification or add additional context in comments.

Comments

1

Simply use this (awk is overkill for this purpose):

cat infile.txt | grep 'parent is inactive, cannot update record' | cut -d ',' -f1

Comments

0

I would go with a proper csv parser. Following is an example that uses core module Text::ParseWords so you don't need to download it from CPAN.

perl -MText::ParseWords -lne '
    @line = parse_line(",", 1, $_);
    print $line[0] if $.==1;
    print $line[0] if $line[3] =~ /parent is inactive, cannot update record/;
' file
"USER_ID"
"CA781558"
"BT055163"
"SG839717"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.