0

sed -e 'expression1' -e 'expression2' inputfile works well.

For my works I add many expression in a sed command inside a shell script. But, the expressions are divided into two different colors.

enter image description here How many expression can be added in one sed command so that sed can handle it?

closed as unclear what you're asking by muru, Shadur, Fox, Rui F Ribeiro, Jeff Schaller Jan 6 '18 at 17:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • There's no restriction according to POSIX standard. But you can face the argument list too long error, as it's the limit for arguments passing to execve() – cuonglm Jan 6 '18 at 5:40
  • 6
    The change in colour is most likely to do with the terminal application and not the length of the command line. – Tigger Jan 6 '18 at 5:51
  • 1
    Really similar: unix.stackexchange.com/q/322433/117549 – Jeff Schaller Jan 6 '18 at 10:43
  • Which sed? Which editor? – muru Jan 6 '18 at 16:07
6
  1. sed scripts entered on the command line (or embedded in a shell script) can be any length up to the maximum command-line length allowed by your shell - megabytes long with modern shells.

    Also it's not necessary to use -e multiple times if you want a sed script to do more than one thing. You can separate multiple commands with either a newline or a semicolon.

    For example, instead of:

    sed -e 's/foo/bar/' -e 's/abc/def/'
    

    you can do:

    sed -e 's/foo/bar/; s/abc/def/'
    

    or:

    sed -e 's/foo/bar
            s/abc/def'
    

    As with sh, additional whitespace between statements is insignificant. I've used a space after the ; in the first example above and more spaces for indentation in the second example to improve readability.

    Note that some sed commands are terminated only by a newline, not by a ;. These are well-documented exceptions to the general rule that ; is a sed command separator, along with newline. For details, see https://www.gnu.org/software/sed/manual/html_node/Multiple-commands-syntax.html (this documentation is suitable for all/most versions of sed - GNU's non-standard extensions are noted when mentioned).

    For example, if you want to add a few lines of text immediately after a specific string, the a (append) command is one of the sed commands that can only be terminated by a newline.

    /search-string-here/ a\
    text line 1 ; semicolons are just text\
    text line 2 ; and more text here\
    
    <more sed commands can go here>
    
  2. sed scripts can be any length you want, as long as you put the sed script in a file and separate each sed command with either a ; or a newline (or both). Run them with, e.g.:

    sed -f scriptfilename ...
    

    You can write standalone sed scripts just as you can for other scripting languages. They can even be made executable with chmod +x if you have #!/bin/sed -f as the first line.

    For example:

    #!/bin/sed -f
    
    s/foo/bar/
    s/abc/def/
    
  3. sed is not just a simple "command", it's a scripting language - most people just use it in a pipeline for a few small and simple regexp substitutions, but sed is capable of a lot more than that.

    See man sed for more details. GNU sed also comes with complete documentation in Texinfo format. Run info sed to read it (or pinfo sed if you have the pinfo viewer installed - it's far superior to info). The same documentation is also available in a variety of formats at https://www.gnu.org/software/sed/manual/.

  • The semicolon works in this particular case, it does work for many sed commands - but that doesn't mean it can be used as a separator for any sed commands as some of them can only be followed by a newline. – don_crissti Jan 6 '18 at 12:13
  • it's more accurate to say that ; is a command separator in sed except for some well-documented exceptions which all have good reasons for being exceptions. e.g. a for append, c for change, and i for insert. they insert, change, or append text until an (unescaped) newline. A semicolon in the text that's being inserted/etc, is just part of the text. Also commands that read or write from files or execute shell commands (because ; is used by sh itself, and it's a valid character in filenames, quoted strings, etc). All of those are terminated by newline and not a semicolon. – cas Jan 6 '18 at 13:38
3

Up to the max length of the command line. The limit for the length of a command line is not imposed by the shell, but by the operating system. This limit is usually in the range of hundred kilobytes. POSIX denotes this limit ARG_MAX and on POSIX conformant systems you can query it with

$ getconf ARG_MAX    # Get argument limit in bytes

Test with:

$ sed $(printf ' -e s/%s//g' $(seq 0001000000)) file
  • @issac In my system it is 2097152 that means greater than 2G. see edited question. – alhelal Jan 6 '18 at 5:47
  • @alhelal No, that is character count, that is a little over 2 meg, not giga. – Isaac Jan 6 '18 at 5:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.