1

Is there any way to fit in 1 line using the pipes the following:

output of

sha1sum $(xpi) | grep -Eow '^[^ ]+'

goes instead of 456

sed 's/@version@/456/' input.txt > output.txt
Trey Hunner
  • 10,975
  • 4
  • 55
  • 114
Pablo
  • 28,133
  • 34
  • 125
  • 215
  • You can't use a pipe to get the output into the command line arguments; the answers given both work, though I'd use (the other) Jonathan's with the double quotes. – Jonathan Leffler May 06 '10 at 03:58
  • @Jonathan Leffler: sed won't play nicely with nesting another command in those double quotes. It will try to parse it improperly before the shell tries to parse out the commands to execute. – Trey Hunner May 06 '10 at 04:05
  • @Trey: what do you mean? The commands in the `$(...)` notation will be executed completely before `sed` is even started! – Jonathan Leffler May 06 '10 at 04:24
  • @Jonathan Leffler: Try executing ` | sed -e 's/\(\/\|\\\|&\)/\\&/g'`. The output will be something like `sed: -e expression #1, char 9: unknown option to 's'`. – Trey Hunner May 06 '10 at 04:42
  • @Trey: we're talking at cross-purposes - I don't see how what you just wrote ties in with the rest of the discussion. Contact me by email (see my profile) if you want to discuss further. Otherwise, we'll let this go as a miscommunication. – Jonathan Leffler May 06 '10 at 05:11
  • xargs command will do this job, try man xargs – sganesh May 06 '10 at 11:39
  • I can't understand how it's possible to substitute 1 argument... would be helpful if you could elaborate more on this. – Pablo May 06 '10 at 23:10

2 Answers2

2

Um, I think you can nest $(command arg arg) occurances, so if you really need just one line, try

 sed "s/@version@/$(sha1sum $(xpi) | grep -Eow '^[^ ]+')/" input.txt \
     > output.txt

But I like Trey's solution putting it one two lines; it's less confusing.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jonathan
  • 345
  • 2
  • 8
  • @Trey: sed won't see the $; the shell will have executed `sha1sum $(xpi) | grep ...` before it attempts to run sed with the output from `sha1sum | grep` embedded in the command line. – Jonathan Leffler May 06 '10 at 05:13
1

This is not possible using pipes. Command nesting works though:

sed 's/@version@/'$(sha1sum $(xpi) | grep -Eow '^[^ ]+')'/' input.txt > output.txt

Also note that if the results of the nested command contain the / character you will need to use a different character as delimiter (#, |, $, and _ are popular ones) or somehow escape the forward slashes in your string. This StackOverflow question also has a solution to the escaping problem. The problem can be solved by piping the command to sed and replacing all forward slashes (for escape characters) and backslashes (to avoid conflicts with using / as the outer sed delimiter).

The following regular expression will escape all \ characters and all / characters in the command:

sha1sum $(xpi) | grep -Eow '^[^ ]+' | sed -e 's/\(\/\|\\\|&\)/\\&/g'

Nesting this as we did above we get this solution which should properly escape slashes where needed:

sed 's/@version@/'$(sha1sum $(xpi) | grep -Eow '^[^ ]+'  | sed -e 's/\(\/\|\\\|&\)/\\&/g')'/' input.txt > output.txt

Personally I think that looks like a mess as one line, but it works.

Community
  • 1
  • 1
Trey Hunner
  • 10,975
  • 4
  • 55
  • 114
  • @Trey: yea I know about that one, so there is no way to do it in `one line`, using pipes perhaps ? – Pablo May 06 '10 at 03:32
  • @Michael: sorry I misread your question. I just modified it to work in one line. I do not believe it would be possible with pipes as you would need to spawn off a subcommand, not redirect output directly to the input of another command (which is what pipes are for) – Trey Hunner May 06 '10 at 03:33
  • @Michael: My first solution does not use semicolons. It uses command nesting which is the closest you'll get to "piping" in this case. – Trey Hunner May 06 '10 at 03:50
  • @Trey: yea, this one works like a charm. Is there any way to escape `/` somehow? because there could be some other command instead of sha1sum, where I don't really know the output? – Pablo May 06 '10 at 04:13
  • @Michael: I just modified the question and added a solution that should escape the `/` and `\` characters in the command output of the nested command. – Trey Hunner May 06 '10 at 04:26