LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2008, 09:21 AM   #1
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Rep: Reputation: 15
Piping output fm cut into sed


Hi, I'm trying to pipe the output from cut into sed to substitute for another character "y" in a text file, but having difficulty.......

Code:
echo $i | cut -d_ -f2 | cut -c5 | sed -i s/y/? $append
Any assistance appreciated
 
Old 12-09-2008, 09:28 AM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch buster
Posts: 1,621

Rep: Reputation: 98
Hya,

The only thing I can understand is that you have some sort of difficulty. I (or we) at least need to know what you want to do and what you get.

Happy Penguins!
 
Old 12-09-2008, 09:39 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Echoing the above.....you need to say what the code is supposed to do, and what it actually does. eg, tell us the error messages.

Quick glance:
the SED "s" command is not terminated
when piping into SED, you would not use a filename and the "-i" flag

General forms:

sed <command> oldfile > newfile
sed -i <command> oldfile ##oldfile gets modified in place
<otherstuff> | sed <command> >newfile ## the output from "otherstuff" is piped to SED, and the SED output is sent to "newfile".
 
Old 12-09-2008, 09:47 AM   #4
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Original Poster
Rep: Reputation: 15
The part
Code:
echo $i | cut -d_ -f2 | cut -c5
is working ok, but I can't seem to take the output fm that into sed in order to do a substitution. Obviously not getting syntax correct
 
Old 12-09-2008, 10:28 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
Originally Posted by wtaicken View Post
Obviously not getting syntax correct
Then go back and re-read Pixellany's post carefully. He just explained your syntax errors to you. I suggest you also have a careful read of the sed man page.

For more advanced help, this page offers a very detailed sed primer.

Finally, a quick look at the first link in my signature may be helpful to you.
 
Old 12-09-2008, 10:45 AM   #6
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Original Poster
Rep: Reputation: 15
Ok, I'll try to be more explanatory

Code:
echo $i | cut -d_ -f2 | cut -c5 | sed -i s/y/? $append
The first part, i.e. stripping out a character from the filename $i works. However, I want to use that character to substitute for another character y in the file $append. Hence I'm trying to pipe to sed to do the substitution within $append, but not sure how to represent the character currently labelled ?
 
Old 12-09-2008, 11:00 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
As already explained by pixellany and Co. you can't do this passage in one shot. You use sed to modify the standard input as the one coming from the pipe OR to modify the content of a file. The solution is to store the stripped character in a shell variable and use that shell variable in the sed command:
Code:
new_char=$(echo $i | cut -d_ -f2 | cut -c5)
sed -i "s/y/$new_char/" $append
provided the value of $append is a valid file name. In my example I terminated the sed command adding a trailing slash. If you want to change ALL the occurrences of "y" in the file, put a "g" (for global) after the trailing slash. In any case do a backup of the original file before using sed with the -i option, you never know...!

Last edited by colucix; 12-09-2008 at 11:01 AM.
 
Old 12-09-2008, 11:54 AM   #8
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Original Poster
Rep: Reputation: 15
Yep, that works, cheers
 
  


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
piping output of find to vim babu198649 Linux - Newbie 4 07-24-2008 07:25 AM
Piping output between processes. C. banan Programming 1 11-13-2006 12:25 AM
How do I cut out a specific piece of a html page (using sed/awk or similar)? bomix Linux - General 2 10-08-2005 05:30 PM
Piping output to text files corbis_demon Linux - General 3 10-12-2004 04:33 AM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 04:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration