LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-12-2008, 01:55 PM   #1
FIRATYILDIRIM
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Rep: Reputation: 0
i am missing new lines when substituting with sed in a bash script


i have a file like

~ 4 ~ 2 ~ 4 ~
~ 11 ~ 2 ~ 4 ~
~ 2 ~ 2 ~ 4 ~

i want to substitute '~'s with '\t's and i am using

Code:
sed -e :a -e 's/~/\t/' "filename"
this is my first experiment with bash scripting so i did not write this on my own so i do not know what does this line doing too much but on the other hand it works great when it writes the result to screen and does what i want but when i write this to have result to a output file

Code:
echo `sed -e :a -e 's/~/\t/' "filename"` >> "filename"
it is writing them as only one line, not in three lines...

i need help

thanks for your time to read this
 
Old 12-12-2008, 02:22 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
There are several issues here:

1. The :a (label used for branching) does nothing in this context. Why is it there?

2. To replace ALL of the "~", you need the "g" flag--ie: sed 's/~/\t/g'

3. This construct: " sed <stuff> filename >> filename" Says: read from filename, apply the sed command, and append the result to filename. Thus, you wind up with the original content plus the modified content.

When I run it (with the g flag), the file winds up looking like this:
Code:
~ 4 ~ 2 ~ 4 ~
~ 11 ~ 2 ~ 4 ~
~ 2 ~ 2 ~ 4 ~
	 4 	 2 	 4 	
	 11 	 2 	 4 	
	 2 	 2 	 4
(Exactly what I would expect)

finally, the echo plus the command in backtics is not necessary---all you need is:
sed '<command>' file > newfile
OR
sed '<command>' file >> file
 
Old 12-12-2008, 02:23 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
go here for a good SED tutorial:
http://www.grymoire.com/Unix/Sed.html
 
Old 12-12-2008, 04:31 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
See... sed guru.
 
Old 12-12-2008, 04:36 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Don't redirect the output to the same filename as the input. The file will be zeroed out before being read and you will end up with a zero length file. Either use a different file name or the "-i" "in place" option.
 
Old 12-12-2008, 06:01 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by acid_kewpie View Post
See... sed guru.
Where?....
 
Old 12-15-2008, 09:55 AM   #7
FIRATYILDIRIM
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
thank you everybody who paid attantion and answered my question... my answer is late because i couldn't work on this code for a few day because of my exams (os & circuits )...

also thank you pixellany your sed 's/~/\t/g' code worked really good...

i saw you asked some question like i said i dont know how to write bash script too much... this is my first time... also i dont have too much time until to submit my experiment... (this is the experiment if someone is interested: ftp://ftp.cs.hacettepe.edu.tr/pub/de...5/Bil235-V.pdf)

also you have write.. the output you wrote is what i was getting... i think i had a little confusion while writing message... i am sorry for this...

thank you to everybody who spent time to write these messages...
 
Old 12-15-2008, 03:07 PM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
hehe, well you managed to sneak homework past us which is pretty rare, so credit for being able to bend the rules without us noticing!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script: SED for Copy/Paste Lines from Files unihiekka Programming 2 10-07-2008 06:12 AM
substituting multiline string in sed rohit2983 Programming 3 01-31-2007 09:00 AM
bash: read lines from a configuration script ldp Programming 2 09-23-2005 11:58 AM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM
need help substituting with sed parker Programming 2 08-08-2003 10:03 AM

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

All times are GMT -5. The time now is 11:59 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
Open Source Consulting | Domain Registration