0

I have this file:

file.txt:

...
threshold:
  swipe: 2
  pinch: 2 

interval:
  swipe: 2
  pinch: 2

No, if I do :

$ locate config.yml | while read i; do sed '/swipe|pinch/s/[0-9]/3/' $i; done

it will changed the 2 to 3:

 ...
    threshold:
      swipe: 3
      pinch: 3 

    interval:
      swipe: 3
      pinch: 3

but this with find does not :

sudo find / -name config.yml -exec sed -n '/swipe|pinch/s/[0-9]/3/' '{}' \+

the regex is the same, so that is not the issue, so what is?

8
  • 1
    Why is there a -n in your -exec-run sed?
    – fra-san
    Commented May 17, 2020 at 20:30
  • from man sed: ` -n, --quiet, --silent suppress automatic printing of pattern space`
    – Herdsman
    Commented May 17, 2020 at 20:34
  • 1
    Change | to \| or add option -E, add option -i if you want to edit in-place, remove -n and you don't need to escape +.
    – Freddy
    Commented May 17, 2020 at 20:40
  • still does not work: sudo find / -name config.yml -exec sed -En '/swipe\|pinch/s/[0-9]/3/' '{}' \+ . I will add -i once I would see it substitued correctly but for now, do not even substitued
    – Herdsman
    Commented May 17, 2020 at 20:44
  • 1
    Remove the -n, it suppresses any output. And you don't need to escape | if you use -E.
    – Freddy
    Commented May 17, 2020 at 20:47

1 Answer 1

1

In the top you reference a file name file.txt, but then only deal with files of the name config.yml, so I assume it's config.yml that contains these patterns.

The locate and bash tags are kinda misleading, as this has nothing to do with both of those :) More importantly, what kind of environment is this? On Linux systems, GNU/sed is commonly installed and needs the -E option to understand the swipe|pinch condition. Bracket expressions (the [0-9] part of your pattern) works even without -E.

So, with that being considered, the following works with GNU/sed and BSD/sed:

locate config.yml | while read -r i; do sed -E '/swipe|pinch/s/[0-9]/3/' "$i"; done

Or, with find:

find . -name config.yml -exec sed -E '/swipe|pinch/s/[0-9]/3/' '{}' +

NB: your pattern /swipe|pinch/ was correct, the suggestion to change it to escape the pipe symbol to /swipe\|pinch/ will not work as now it's no longer a regular expression and would match a literal | and thus would not match any content of your file. However, it would work if the surrounding apostrophes (') are omitted:

sed -E /swipe\|pinch/s/[0-9]/3/
2
  • It looks like the suggestion was to escape | with a \ or to add the -E option. As far as I can see they both work with GNU sed (or, for what it's worth, Busybox sed; I can't test any other implementation). I.e. GNU BRE supports alternation, it just requires the \| syntax.
    – fra-san
    Commented May 18, 2020 at 12:50
  • I know find has many option, but I rather use locate since it is faster, and then simpy pipe output for further proccessing (e.g. find blabla | xargs cat is the same as find / -type f -name blabla -exec cat, but find from root takes so long, but locate uses database for this purpose\
    – Herdsman
    Commented May 18, 2020 at 17:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .