1

I have two comma delimited files

I will read 1 line from file 1,

I need to find matching lines in file2 including the comma's in between

If I do it literally by typing the line (i.e. copy-n-paste with mouse) I get what I need

If I assign the line to a variable, I get nothing

I'm stumped. I know I am doing something wrong as far as regex matching goes but what ?

Here is an example:

$ pattern=$(head -1 accts.csv)
$ echo $pattern
_ccc_net_pfx,_ccc_net_pfx_export
$ grep "$pattern" all.csv
$ grep "_ccc_net_pfx,_ccc_net_pfx_export" all.csv
MyDomain\user1,User-1,Administrator,_ccc_net_pfx,_ccc_net_pfx_export,owner,Application,,
MyDomain\user2,User-2,User,_ccc_net_pfx,_ccc_net_pfx_export,readonly,Application,,
  • You can help yourself and answerers by running set -x, running that sequence of commands again, and editing the question to show that too. – JdeBP Sep 20 '18 at 18:18
  • Works for me. As said, try running with the xtrace option. – RudiC Sep 20 '18 at 18:20
1

Your $pattern variable contains spaces -- or maybe a spurious ^M / CR / \r at the end if accts.csv comes from a windows machine.

You can't see the spaces with echo $pattern, because the variable is not quoted (as it is in the call to grep), and so it's nicely split and trimmed by the IFS mechanism. Try something like echo "{$pattern}" instead.

You should trim it yourself:

$ pattern=$(sed 's/[ \r]//g;q' accts.csv)
  • It was not the spaces but the <CR> (^M) character at the end of each line. Coming from Excel export I presume as I exported these few minutes ago. Once the ^M characters trimmed, it works fine. Thank you for your suggestion and directing me to the correct path – Scott Sep 20 '18 at 19:10

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.