0

input.csv

12345,client1
2345,client1
345,client2

target.csv

12345,client1,2021-09-13
22345,client1,2021-09-14
44345,client2,2021-09-15

expected output

12345,client1,2021-09-13

tried grep -f but clearly its not the correct. is there any other option that I can add to search from start of line?

ppatidar
  • 177
  • 2
  • 10
  • Take a look at `man grep` for a option to match only those lines containing whole words. – Cyrus Sep 14 '21 at 03:49

1 Answers1

0

If using ksh93, bash, zsh, or another shell that supports process substitution:

grep -f <(sed 's/^/^/' input.csv) target.csv

Adds a start of line anchor to each line of input.csv on the fly for grep to use.

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • `sed | grep -f-` will do the same in any shell. – stevesliva Sep 15 '21 at 19:20
  • @stevesliva I was wondering if someone would mention that. I didn't see anything in the POSIX description of `grep` about `-f -` reading from standard input so I didn't mention it as an alternative for people stuck on plain sh. Is that behavior standardized somewhere in the document? – Shawn Sep 15 '21 at 22:53
  • 1
    Good question. It's magic. https://stackoverflow.com/a/3797806/3120884 -- POSIX tools should not have '-' mean anything other than stdin or out in file context. Not sure from the "shall" vs "should" discussion whether that means it's basically universal POSIX this century. – stevesliva Sep 16 '21 at 02:04