How does one combine two commands like the ones shown below into one command with one output file?

first command:

printf '%s\n' {001..500} input > output

second command:

sed 's/^/PREFIX /; s/$/ SUFFIX/' input > output

I realise you've answered your question, but a simpler solution would be to put the prefix and suffix in the printf command.

printf 'PREFIX %s SUFFIX\n' {001..500} > output

(I'm not sure if the input part should be there. It's absent in your answer.)

  • Oh even better! Awesome, thanks! p.s. You just taught me something... :) – Anonymous Mar 27 at 4:38
  • How come the sed command changed to look more simple? How is that possible? Is that like printf's equivalent? – Anonymous Mar 27 at 4:40
  • The sed command replaces the beginning ^ and end $ of each line. printf defines how it will "print" the numbers, i.e. each line consist of PREFIX followed by a string %s, then ` SUFFIX` and a newline \n. The numbers expanded from {001..500} replace the string placeholder %s. – Sparhawk Mar 27 at 4:42
  • because it doesn't actually need the input, it works fine without. – Anonymous Mar 27 at 4:42
  • Regarding input, if this is present, the command will create another line after the numbers, but with the literal string input instead. (I'm presuming you don't want this.) – Sparhawk Mar 27 at 4:43

I figured it out!

Both commands as one:

printf '%s\n' {001..500} | sed 's/^/PREFIX /; s/$/ SUFFIX/' > output
  • 1
    Simpler: printf 'PREFIX %s SUFFIX\n' {001..500} >output – John1024 Mar 27 at 4:31

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.