LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 06:38 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
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, 06:50 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
hello daYz. please show us the output of perl file.pl enclosed in quotes.
 
Old 11-02-2006, 01:48 PM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
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, 05:05 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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, 03:49 AM   #5
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
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,▼9h4_Pc00uP=+►↨U◄1◄V0+++*8eqPc40Z
O+P=↔↑W۳t▼♀T0`vx05k<►PT,--p-R-TlxdQ8OC-+PsDu<
Code:
perl file.pl
gives:

Code:
3ƒt$[?s‼►"<ƒ|Y<►"Py,▼9h4_OPc00u>*P=►↨U◄1◄V0+8eqPc40Z>~?O^
>^P=↔╬↑Wt▼>♀T0>׷`^vxo05>k<►"PT,pR"T*lxdQ8OCssOPsDu"<
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 03:52 AM.
 
Old 11-03-2006, 08:20 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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 08:21 AM.
 
Old 11-03-2006, 12:10 PM   #7
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
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, 04:29 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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, 01:40 AM   #9
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
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, 02:15 AM   #10
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:50 AM
command substitution: ^ rhxk Linux - General 2 04-06-2006 10:51 AM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 01:36 AM
sed substitution error BlkPoohba Programming 1 08-25-2004 03:00 PM
sed Command linuxdev Linux - Newbie 3 02-09-2004 12:27 PM


All times are GMT -5. The time now is 12:44 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration