LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-02-2006, 05:38 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
Command substitution and sed


Hi,

I am trying to use the output of a perl command with sed, but I get this error:

Code:
sed: -e expression #1, char 103: unknown option to `s'
The command I am using is:

Code:
sed "s/expression/`perl file.pl`/" file
Can someone tell me what the corect way of doing this is?

Running the perl command like this goes fine b.t.w:

Code:
perl file.pl
Thanks for your help.

Regards,

Ben
 
Old 11-02-2006, 05:50 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
hello daYz. please show us the output of perl file.pl enclosed in quotes.
 
Old 11-02-2006, 12:48 PM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Hi konsolebox,

From your answer I already understood what the problem was. The output of the perl command contained characters that where causing the problem. I don't know which exactly, but some of them could be interpreted by bash (like / and $ for example.)
I am going to look for an anternitive way to accomplish what I want.

Thanks for getting me on the right way.

Regards,

Ben
 
Old 11-02-2006, 04:05 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Thanks for getting me on the right way.
Ok no prob. I'm not sure but perhaps you need to use single quotes.

# eval 'sed s/"expression"/'"'`perl file.pl`'/" file

Good luck there.
 
Old 11-03-2006, 02:49 AM   #5
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
konsolebox,

I do not understand the eval command. Can you explain to me why you use it here?

You example does not work also. Can you tell me what part I should enclose in single quotes? (The expression is actually a variable b.t.w.)

I have tried it this way but the output of the perl command here is different then when I run just the perl command:

Code:
eval 'sed "s/"expression"/"`perl file.pl`"/" file'
output:
Code:
3+âÚ¦+¯+t$¶[üs‼►ö¦<âÙ³Ô¶ý|ƒ<►öPy,▼º9hò4À_îPc0ò0uøáP=¦Ñ+►↨U◄1◄V0+++*8eqPc4ò0ZøÿÉÀ
Oê+ÎøêP=¹↔ç↑¶WÛ³t▼ø♀òTú0øÈÎÀ`êvÀx£05ø¶k<►öPT,-Û-p-R-ôTálxdQ8O³C-ÜÜî+¸¸¦Ps¦¥Duö¦<
Code:
perl file.pl
gives:

Code:
3ɃéÝÙîÙt$ô[?s‼►"Û<ƒëüâôì|Y<►"Py,▼§9h4·_OPc00u>*P=þ¥¼►↨U◄1◄V0È+Àÿ8eqPc40Z>~?·O^Ú
×>^P=û↔╬↑¶Wêüt▼>♀T£0>Ô×·`^v·xo05>¶k<►"PT,ËêÊpÂRÄ"T*lxdQ8OüCÂssOÃ÷÷ºPsº¾Du"Û<
When I add `perl file` to a variable (like so a=`perl file.pl`), the variable also gets that different value than when the perl command is just run from the command line.


The file.pl file actually contains hexadecimal characters that are converted to ascii characters when the file is executed.

The perl file looks like this:

Code:
$string = "\x01\x0F\xEF";
print $string;
Thanks for your help.

Regards,

Ben

Last edited by daYz; 11-03-2006 at 02:52 AM.
 
Old 11-03-2006, 07:20 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
the output of the perl command has double quotes in it so we need to use single quotes but text in single quotes are not expanded. for example

# echo '`echo a`'

will just print `echo a` and not a. so we need to use eval:

# eval "echo '`echo a`'"

will print a

btw try to use this instead:

# eval REPLACE='`perl file.pl`'

then

# eval sed s/\"${EXPR}\"/\'${REPLACE}\'/ file

or put them together

# eval sed s/\"${EXPR}\"/\'`perl file.pl`\'/ file

Last edited by konsolebox; 11-03-2006 at 07:21 AM.
 
Old 11-03-2006, 11:10 AM   #7
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks for your explenation konsolebox.

When I make use of command substitution with the perl command, some characters put on the screen are different than when the perl command is just run from the command line. Can you tell me why that happens?

Regards,

Ben
 
Old 11-03-2006, 03:29 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
are you talking about the difference of the output to the screen and output to the file?

perhaps the character 'ò' (hex f2) is just not printed in the screen.
 
Old 11-04-2006, 12:40 AM   #9
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
I meant the difference between the characters that are put on the screen when the perl command is just run from the command line and when I implement command substitution with the perl command.
Sorry I can not explain that well.

Like so:

Code:
1# perl file.pl
Code:
2# eval "echo `perl file.pl`"
It seems to me that both commands would output the same charcaters, but there are some differences.

If I make use of the '\xBF' character for example, with the first command:

Code:
¿
is put on the screen.

and with command 2:

Code:
is put on the screen.

Ben
 
Old 11-04-2006, 01:15 AM   #10
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Never mind the last question. The problem of the difference in characters was caused by a minimal version of Cygwin that I am using. It does not happen on my Linux system.

Thanks for your help konsolebox

Regards,

Ben
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command ancys Programming 2 08-05-2006 10:50 AM
command substitution: ^ rhxk Linux - General 2 04-06-2006 09:51 AM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 12:36 AM
sed substitution error BlkPoohba Programming 1 08-25-2004 02:00 PM
sed Command linuxdev Linux - Newbie 3 02-09-2004 11:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration