1

is sed -e 'script' -e 'script' the same as sed "script;script" ?

Moreover do those multiple commands operate during one scan of the file? Or is it the case that the more scripts you put the more scans/passes of the file it will do? (one per script)

Since sed is a scripting language, I am leaning towards the multiple pass theory... For example if you have sed "script;script" this is like having a sed script like this:

script
script

So it would be natural to assume that everything happends procedurally.

But how about -e 'script' -e 'script'?

And most importantly, is there a way to tell sed to only perform one pass, therefore saving valuable CPU cycles on large files? And if sed cannot do that, is there another way in doing something like this ? (Maybe awk is more suitable for example? ect)

Thank you for taking the time to help

  • Did you test your theory? – muru May 13 '17 at 19:28
  • @muru how would you test it – HashWizard May 13 '17 at 22:09
  • By sending a huge file to sed over a pipe and observing sed's memory and storage usage. Pipes aren't seekable, so, if sed wanted to do multiple passes, it would have to store all that data. – muru May 14 '17 at 13:12
3
  • For each line of input, each expression/script is applied to it in turn. The command

    sed -e 's/apple/orange/g' -e 's/orange/red/g'
    

    would replace all occurrences of the string apple and orange with the string red.

  • There is only one pass through the input data. sed handles streams, it is a stream editor after all, and the input stream doesn't even have to be seekable.

The example that you had,

sed -e "script1" -e "script2"

is the same as

sed "script1;script2"

which is the same as

sed -f file1 -f file2

where file1 contains script1 and file2 contains script2, which is the same as

sed -f file

where file contains

script1
script2

or

script1;script2

Whether sed is suitable for your task is totally depending on the task. Just like sed, awk does not need to parse a file or input stream more than once. Some tasks are more suitable for awk, especially those that require aggregation of data.

sed is an editor, awk is more of a text processing language in comparison.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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