LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2010, 05:39 PM   #1
logar0
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Rep: Reputation: 0
[Solved] Problem - question for command inside sed.


I have the following one-liner:

echo "one two ΟΟΞ―Ξ four" | sed -e 's/[ ]*[^ ]*Ξ[^ ]*Ξ[^ ]*/'"$(echo '&' | iconv -f UTF8 -t ISO_8859-7)"'/g'

sed, locates the word with the different encoding (ΟΟΞ―Ξ). The output should be something like

one two three four

but it returns the same.

one two ΟΟΞ―Ξ four

It's like the data in & from sed can't be changed.

I also tried passing & to an external script but it did work.

What am I doing something wrong?

Last edited by logar0; 10-25-2010 at 12:01 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-24-2010, 06:10 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
The "&" you're using in your echo is from "outside" sed. The shell
doesn't know anything about sed's "&".
 
Old 10-25-2010, 06:05 AM   #3
logar0
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Is there any way to make it work?
 
Old 10-25-2010, 06:12 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I think the OP is trying to use "&" on the RHS the same way that "\&" is used in AWK, to represent the match on the LHS.
For this, I would try: put the whole LHS into a \(...\) and then represent it with "\1" on the RHS. However, I'm still not sure whether or not the stuff represented by \1 will be passed into that $(subshell) as you expect-- but `xargs` could solve this, as well as probably other ways.
 
Old 10-25-2010, 06:26 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Quote:
I think the OP is trying to use "&" on the RHS the same way that "\&" is used in AWK, to represent the match on the LHS.
Sed supports this too
 
Old 10-25-2010, 06:29 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551

of course - thanks grail!

Well there's more of a problem here, at least on my machine: I tried echoing just the input string, through the `iconv` command, and it doesn't work for me, so if OP's in the same boat, the sed is never going to work.

When I do the echo of the string through `iconv` I get:
Code:
one two     four
I don't know if this is something to do with my locale or what...
 
Old 10-25-2010, 06:54 AM   #7
logar0
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Original Poster
Rep: Reputation: 0
It seems that you can't edit the matched string inside sed.
 
Old 10-25-2010, 06:56 AM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
In GNU sed the 'e' command can execute commands in the pattern space.
For example
Code:
echo "2.2+3.3" | sed "s/.*/ echo '&'|bc /e"
5.5

echo "one two" | sed -r "s/(.*) (.*)/ echo '\2 \1'|rev /e"
eno owt
The s command creates a line of shell code in the pattern space which is then executed by the e command.
When the code is executed it is replaced in the pattern space by its output.
The 'e' command uses the default shell, /bin/sh, which is often linked to /bin/bash, but in Ubuntu it's now linked to /bin/dash.
 
2 members found this post helpful.
Old 10-25-2010, 07:17 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Well I get:
Quote:
��ία
So not much more useful than a blank.

@Kenhelm - thanks for that .. still getting sed-jitsu together
 
Old 10-25-2010, 10:10 AM   #10
logar0
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks you very much Guys.
 
Old 10-25-2010, 11:41 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Please mark as SOLVED if you have your solution.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem with a function having a background process inside a command substitution rylphs Linux - Newbie 2 10-05-2010 10:36 AM
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 12:21 PM
How to use a command inside sed ZAMO Linux - General 3 02-27-2009 04:37 AM
sed command question mokku Linux - Newbie 2 03-05-2008 11:30 AM
question about using sed command feetyouwell Linux - Software 4 12-13-2004 08:47 AM


All times are GMT -5. The time now is 02:57 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration