1

I have a directory containing daily cds for the month. I need to search all cds for a list of ph numbers and write result to individual files for each pattern.

Currently I'm using the below command manually for each ph number in the dir containing monthly cds and writing the result of each number to individual file.

grep -rn 111222333 /dir-containing-new-indivdual-logs/111222333.csv

I tried adding grep -f option to specify search list but no luck as my syntax for the entire command is wrong.

  • If you want to use grep, but write to individual files, you need to loop over the grep, for example a while loop reading from the ph number file. – Philippos Oct 12 '17 at 6:24
  • It's unclear whether you want to write the result to individual files for each pattern or for each file that you search. Please edit your question to clarify. – Kusalananda Oct 12 '17 at 7:02
0

If you'd rather for staying with grep, consider this loop

nums=/path/to/the/file_with_numbers_one_per_line
wdir=/path/to/dir/where/records2searchIn/located
rdir=/path/to/dir/to/put/results

for anum in $(cat ${nums}); do
 grep -nr "${anum}" ${wdir} > ${rdir}/result_file_for_${anum}; 
done

which does grepping for each number in list and saves individual results

0

Use awk for that.

awk 'NR==FNR{ptrns[$0]++;next} 
    FILENAME!="ptrns_list"{for(x in ptrns) if($0 ~ x) print $0>x}' ptrns_list infiles*.csv

First read all patterns to an array from where those are in ptrns_list file. Then for each .csv file if in entire line $0 one of those patterns seen $0 ~ x then print that line into individual file which is the same name as pattern matched.

Just pass your actual search patterns list file with ptrns_list and input .csv files with infiles*.csv in ptrns_list infiles*.csv.

  • Thanks, but I'm hopeless at scripting, its a foreign language to me, hence my one line grep cmd. I would need further explanation of the obvious, as to how I'd use this. – mudgee.garry Oct 12 '17 at 5:34
  • I'm still doing something wrong & I don't understand where the output files are directed to go. This does not give me errors but no output code awk 'NR==FNR{ptrns[$0]++;next} FILENAME!="ptrns_list"{for(x in ptns) if($0 ~ x) print $0>x}' ./ptns_list ./Aug-2017/*.csv code – mudgee.garry Oct 12 '17 at 22:43
  • Sorry for my typo above, I've simplified loco & file names. So script "awkphsearch.sh" is in its root dir, containing all mthly *.csv cdrs files & a ph numbers list file, listing numbers to be searched in individual lines - "phno-list" & output files r to go to sub dir "results" Pls adjust / fill in correct info to get your AWK script to achieve this. Thanks – mudgee.garry Oct 12 '17 at 23:45
  • if your PATTERNs listed in a file called "myPattern_list" and your .csv files in a directory like /path/to/all*.csv then this awk script awk 'NR==FNR{ptrns[$0]++;next} FILENAME!="myPattern_list"{for(x in ptrns) if($0 ~ x) print $0>x}' myPattern_list /path/to/all*.csv will process all those .csv files and produce a result in current working directory where you executed the awk command with multiple files which their name exactly are what you have patterns in your myPattern_list file. – αғsнιη Oct 12 '17 at 23:52

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.