I want to recursively search and extract in all files inside my current directory, for any string that starts with " B-0 " followed by any number of digits .
If a match is found, i want to extract it. But at the same time, i want to extract unique strings . For example - search might find B-05255 in 2 files. So i want to extract B-05255 only once (unique).
Once the search is done, i want to allocate all strings to one final string comma separated as :
FINAL_STRING = B-05255,B-05256,B-04152
Waiting for suggestions?
Thanks.
You can use the option -r
(recursive) :
GREP_OPTIONS="" /bin/grep -ohr '\bB-0[0-9]*' . | tr $'\n' ','
This iterates recursively trough the currently directory and searches for the pattern in every file. The option -o
extracts only the matched strings (not the whole line). Note that I'm using the escape sequence \\b
which stands for word boundary (Because you said: "... that starts with ...")
However grep
will print each result on a separate line. I'm piping the results to tr
to replace newlines by commas.
You can use this grep
, sort
, tr
combination:
FINAL_STRING=$(grep -rIhEo '\bB-0[[:digit:]]*' . | sort -u | tr '\n' ',')
echo "${FINAL_STRING%,*}"
grep
options used are:
r
- recursive I
- ignore binary files h
- omit filename in output E
- extended regex o
- only print matched output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.