1

I would like to check all files in a directory whether they can contain two or more occurrences of a string.

Checking for a single "occurrence of a specific string using bash" seems easy:

if grep -q "LineString" "$File"; then
  Some Actions # SomeString was found
fi

But how to count to two?

codeforester
  • 39,467
  • 16
  • 112
  • 140
agmoermann
  • 86
  • 7

2 Answers2

1

Use (( )) for the numeric comparison:

if (( $(grep -c -- "LineString" "$file") >= 2 )); then
  # your logic
fi

To loop through all files:

#!/bin/bash
shopt -s nullglob # make glob expand to nothing if there are no matching files
for file in *; do
    [[ -f $file ]] || continue
    if (( $(grep -c -- "LineString" "$file") >= 2 )); then
      # your logic
    fi
done

If you are dealing with very huge files and your grep supports the -m option, then you can use grep -cm 2 to optimize the reads:

#!/bin/bash
shopt -s nullglob
for file in *; do
    [[ -f $file ]] || continue
    if (( $(grep -cm 2 -- "LineString" "$file") >= 2 )); then
      # your logic
    fi
done
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 1
    Note that `-c` is a standard option for `grep`, but `-m` is not. (Also, it may depend on the implementation , but `-m` and `-c` can be combined to avoid the need to use `wc -l`.) – chepner May 08 '18 at 20:12
  • 1
    From the 2nd answer I'm getting an "unexpected fi" error. Shouldn't I close the for() loop with an done? for file in *; do [[ -f $file ]] || continue filename="${file##*/}" echo $filename if (( $(grep -c -- "LineString" "$file") >= 2 )); then mv $file "$filename.bak" echo "select ST_LineMerge(ST_Union(geometry)) from $file"; ogr2ogr -f GeoJSON -explodecollections -dialect sqlite -sql \ "select ST_LineMerge(ST_Union(geometry)) from $file" "$filename.geojson" "$filename.bak" fi done – agmoermann May 08 '18 at 20:38
  • Corrected the answer. – codeforester May 08 '18 at 20:45
  • Thanks @chepner. Updated the answer to incorporate your suggestion. – codeforester May 08 '18 at 21:05
  • Since merging "MultiLineStrings" doesn't work – can I exclude them from the search? (Sorry, that's a little bit funny!) – agmoermann May 08 '18 at 22:10
  • That seems like a different requirement. Please update your question appropriately or ask a new question. – codeforester May 08 '18 at 22:49
0

Try this

if [ `grep "LineString" $file | wc -l` -gt 1 ]; then 
    echo "done found";
    ' do something
fi;
Eby Jacob
  • 1,418
  • 1
  • 10
  • 28