File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1323
1323
\0 problem. But then you need to explain the awk model to your
1324
1324
coworker.
1325
1325
1326
+ *** grep -Ec
1327
+ Say you want to find lines that match 2 words, not just 1. in whatever order.
1328
+
1329
+ #+begin_src bash
1330
+ echo "foo bar" | grep foo | grep bar # That works, but it only works for very fixed inputs.
1331
+
1332
+ echo "foo bar" | grep -E 'foo.*bar' # That doesn't , because order matters.
1333
+
1334
+ echo "foo baz" | grep -E '(foo|bar)' | grep -E '(foo|bar)' # That is not going to work either, because if only one word matches, it will match twice.
1335
+ #+end_src
1336
+
1337
+ Here's how we can match multiple things from a list.
1338
+ =echo "foo bar" | tr ' ' '\n' |grep -Ec "(foo|bar)"=
1339
+
1340
+ #+begin_src bash
1341
+
1342
+ match_two () {
1343
+ while read line; do
1344
+ if [ "$(tr ' ' '\n' <<<"$line" | rg -Uc "^($search)$")" = 2 ]; then
1345
+ echo "$line"
1346
+ fi
1347
+ done
1348
+ }
1349
+
1350
+ cat file_with_two_words_per_line.txt | match_two "foo|bar|baz"
1351
+ #+end_src
1352
+
1353
+ With this, you can find out how many matches happened in that
1354
+ line. The example above filters lines which have 2 words from the
1355
+ 'foo|bar|baz' regex.
1356
+
1357
+
1358
+
1326
1359
*** Set operations
1327
1360
There are lots of other "set level" operations you can perform on
1328
1361
files/streams using basic unix tools.
You can’t perform that action at this time.
0 commit comments