Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • Instead of fooling around alot with variables, cut, echo and sed, you could replace all by a simple sed -E "s/.*,(.*),(.*)/'\1.*,\2'/", but actually you don't want the single quotes to be part of the search pattern, so it's -E "s/.*,(.*),(.*)/\1.*,\2/". Escape the pattern variable with double quotes, like @Romeo indicated in his answer. Commented Apr 26, 2017 at 7:39
  • Thanks, I replaced the variables with the sed command, but it generates a space at the beginning of the line which causes the grep to fail. Commented Apr 26, 2017 at 8:23
  • What did you try? I do for pattern in "$(sed -E "s/.*,(.*),(.*)/\1.*,\2/" input.csv)"; do grep "$pattern" modded_file.csv.dat; done to iterate through your input file. You can also do the whole thing in one pass, but this requires deeper sed or awk experience Commented Apr 26, 2017 at 8:40
  • Turns out that since the input file was windows based, it had carriage return for each line which I had to remove. Your solution worked perfect after this. Thanks! Commented Apr 26, 2017 at 11:50
  • 1
    @ShashankKR That proves once more that it is always a bad idea to process CSV files (which only in a small subset of cases are line based) with line oriented tools. You should use Perl/Python/Ruby, that have libraries to deal with real CSV files. Commented Apr 26, 2017 at 15:53