chmln / sd

Intuitive find & replace CLI (sed alternative)
MIT License
5.72k stars 136 forks source link

Are these features missing or not intended? Possibility in the future? #297

Open emrakyz opened 4 months ago

emrakyz commented 4 months ago

I aimed to replace sed with sd but I guess, this is not the main idea of it.

This post is meant for the community. So, maybe some of these features will be requested by a considerable amount of people, so they will be considered to be implemented in the future.

Or, maybe the developers would recommend better alternatives, different ways we don't know about.

I am assuming that sed was developed with an aim that it would have been a scripting language on its own. You can write multi-line sed commands in a single session having conditionals, loops and similar processes. I am not completely sure if some of these are possible on sd or if you do have any plans to make at least some of these possible.

I am assuming that the syntax for sd where we split pattern and replacement in two different blocks with quotes, makes most of these impossible or harder. Or not? Please correct me if I am wrong.

I think, there would be a lot more similar or non-similar examples where sd is lacking. I will only give some examples since I could only encounter some of them trying to use it.

First Example

sed 's/\}, /\},\n   /g
    s/, /,\n    /
    s/ }/\n}/
    s/,\s*pages=/,\n\tpages=/' |
sed '1s/^ *//
    1s/[0-9]*\([0-9]\{2\}\)/\1/
    1s/_//
    1s/.*/\L&/
    s/.*=/\L&/
    s/=/ = /'

Second Example

It is the same for simpler commands: sed -E '1d;$d; s/^ *([0-9]+).*\s{2,}(.+)$/\1 \2/'

Third Example

sed -E '
/Emerging/,/Completed/{/^$/d;}
/Always study the list/,+11d'
[other ones]'

Fourth Example

sed -e :a -e '/^\n*$/N; /^\n$/D; ta' "logfile.txt"

In short, this command removes consecutive blank lines, leaving only a single blank line between blocks of text. First match looks for an empty line. If the second match is also true, then it deletes the first match.

Fifth Example

sed -n '/0\s*c12a7328-f81f-11d2-ba4b-00a0c93ec93b/ {s|^[^ ]*|/dev/&|; s| .*||p; q}'

Sixth Example

sed -i '|^nvidia/'"${GPU_CODE}"'|!d' "linux-firmware-"*

Seventh Example

sed -i /^FFLAGS/ a\RUSTFLAGS="-C debuginfo=0 -C codegen-units=1 -C target-cpu=native -C opt-level=3"'

Eighth Example

sed -n 's|^|/dev/|; 2p'

chmln commented 3 months ago

Hey @emrakyz thanks for taking the time to write this all out.

sd was created out of my personal frustrations with sed back then when it came to simple find and replace tasks. sed of course is much more powerful as you've noticed, having support for appends, deletions, etc. I think having a DSL like sed does would go against the simplicity of this tool, but I would be open to supporting basic things like line deletions (e.g. delete a range of lines that match a pair of regexes) with via flags for instance. The most important thing is to keep things simple and approachable, so that you don't need to look up stack overflow on how to use this command line utility. We are very much open to contributions, proposals, and discussions like this

emrakyz commented 3 months ago

Thanks for your answer!

I understand that the actual aim is to use this tool for simple tasks and in fact I already do that. Sometimes, I also use it in scripts for simple tasks since it's faster for simple purposes. It really makes a difference especially since we can get rid of forward slashes and escaping.

At first, I was discovering Rust based tools and found that ripgrep and fd can totally replace their counterparts but I guess they are written with the aim of rewriting their equivalent programs; not like this project. It's understandable since they are already simpler compared to sed.

I agree that the features such as append, conditionals, labels or loops that make sed behave as a language in itself are totally overkill for this project.

But I still think that some of the features can still be discussed. I think they won't make the program overly complex for the users since they can already use the current functionality.

These are just my observations and I would like to have your opinion; or other people's comments.

The features I think that are suitable for this project are:

  1. Command chaining: We can use a simple flag between different replacements in order to prevent subsequent executions with pipes: sd -e 'find' 'replace' -e 'find2' 'replace2'

  2. We can delete lines, ranges by numbers or matching regexes or placements such as the last line ($) using flags or similar simpler approaches without changing the current functionality or over-complicating the current usage.

  3. I also think that matching specific lines with regex and do the task on those lines, is also a basic functionality but I would like to hear your opinion. For example can't we do something like this: sd --match (or -m) 'important_line' -e 'find' 'replace'

  4. Reverse match seems possible and simple too.

Thanks a lot for creating this tool and I hope it doesn't die.