0

I have CVS file which as search pattern and file name seperated by comma. I want to read the CVS for get search pattern and file and do grep. I tried but its not working. Any pointer in this will helpfull. Please find CSV File(DC2LOGS.csv.

SearchPattern,FilenamewithPath
-116329548,/opt/httpd/logs/apps/atasvc/prod3/was70/atasvc-59596/ata.log
-91756,/opt/httpd/logs/apps/atasvc/prod3/was70/atasvc-59596/ata.log
-86160,/opt/httpd/logs/apps/atasvc/prod3/was70/atasvc-59596/ata.log.1

Grep code:

 cat DC2Logs.csv | while read a b c d e; do grep  -E "CLAManager.getAttributeFromCLAMapping() took.*$d.*"  < "$e";  done;

Thanks in advance.

2
  • What are you trying to achieve? Commented Apr 18, 2014 at 11:45
  • I am trying to do dynamic grep where input and filename comes from CSV file. Commented Apr 18, 2014 at 11:51

1 Answer 1

2

I think you are looking for:

while IFS=, read pattern filename; do 
  grep -e "$pattern" "$filename"; 
done < DC2LOGS.csv

(You definitely want -e to protect against the leading hypen of pattern causing grep to treat it as an option, and whether or not you want -E is up to you.)

Note that this will try to use the first line (the headers), so perhaps you want:

sed -e 1d DC2LOGS.csv | while IFS=, read pattern filename; do
  grep -e "$pattern" "$filename"
done

instead.

Sign up to request clarification or add additional context in comments.

2 Comments

when i am trying to run following command. i am getting following error. i am using -E option. error:-grep: CLAManager.getAttributeFromCLAMapping() took.*-226746.*: No such file or directory. i dont know how to solve this. i am bit learning unix.
Replace grep with echo and see if the command makes sense. I see now that you want the pattern read from the file to only make up a portion of the pattern, so use grep -E -e "CLAmanager... $pattern" "$filename"

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.