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 04-11-2009, 05:14 PM   #1
SHARPY
LQ Newbie
 
Registered: Apr 2009
Location: Toronto, Ontario, Canada
Distribution: Fedora 11 X64
Posts: 8

Rep: Reputation: 0
Question Help with Simple sed one liner


For the past hour and a half i have been trolling forums and google for an answer to my problem, though everything i have attempted has failed I've decided to post my issue. It seems to me like a simple issue but I'm lost at this point:

oldtote='cat /tmp/temp.txt'
newtote='cat /tmp/temp2.txt'

sed -n "s/$oldtote/$newtote/g" $selfile


Both oldtote and newtote get a value. I have tested this with an echo, $selfile has a value of sales.txt, the script runs with no errors but when i cat the sales.txt the values remain unchanged.

Any feedback would be greatly Appreciated

Thanks,
SHARPY
 
Old 04-11-2009, 06:32 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,329

Rep: Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473
Quote:
Originally Posted by SHARPY View Post
For the past hour and a half i have been trolling forums and google for an answer to my problem, though everything i have attempted has failed I've decided to post my issue. It seems to me like a simple issue but I'm lost at this point:

oldtote='cat /tmp/temp.txt'
newtote='cat /tmp/temp2.txt'

sed -n "s/$oldtote/$newtote/g" $selfile


Both oldtote and newtote get a value. I have tested this with an echo, $selfile has a value of sales.txt, the script runs with no errors but when i cat the sales.txt the values remain unchanged.

Any feedback would be greatly Appreciated

Thanks,
SHARPY
Would love to give some feedback, but your question is lacking detail. What are you trying to do? If you're just trying to burp out the sed into the $selfile, just redirect it with the ">", like:

sed -n "s/$oldtote/$newtote/g" > $selfile.

If that's not it, it'd help if you posted your script, and some detail about exactly what you're trying to do.
 
Old 04-11-2009, 06:52 PM   #3
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
If you want to change the content of the target file, use the option -i to edit the file in place. To make a safe backup copy of the file, use sed -i.bak. This will save a copy of the original file with the .bak extension appended. You can choose any other extension at your pleasure. more details in man sed.
 
Old 04-11-2009, 10:14 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 341

Rep: Reputation: 143Reputation: 143
Your 'sed -n' command will not give any output.
The '-n' option turns off automatic printing; then sed prints only when the 'p' command or the 'p' flag of the 's' command are given.
Code:
sed -n "s/cat/dog/"   infile > outfile  # No output
sed -n "s/cat/dog/p"  infile > outfile  # Outputs only changed lines
sed -n "s/cat/dog/;p" infile > outfile  # Outputs all lines
sed    "s/cat/dog/"   infile > outfile  # Outputs all lines
 
Old 04-12-2009, 02:43 AM   #5
SHARPY
LQ Newbie
 
Registered: Apr 2009
Location: Toronto, Ontario, Canada
Distribution: Fedora 11 X64
Posts: 8

Original Poster
Rep: Reputation: 0
Sorry about the lack of detail...Thanks for all the replies in the meantime...I should have explained better in the first place just so fatigued from sitting in front of the machine. Basically what i am trying to do is search a text file for the oldtote value and replace it with the newtote value.

------------------
Text File
bill:2900
bob:2300
------------------
In script variable values
oldtote=2300
newtote=2400
-------------------------
After the sed line is executed the text file should be changed to

Text File
bill:2900
bob:2400
------------------------

So i am trying to replace the old value with a new one, I have the script working so that it only selects the line based on the name so that's no issue, just actually writing the changes to the file is the issue I can not figure out how to use two variables in the sed line.

Thanks again, let me know if any more details are needed.

Last edited by SHARPY; 04-12-2009 at 02:54 AM.
 
Old 04-12-2009, 03:25 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,001

Rep: Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219
So have you taken any notice of the previous responses ? Play around with the suggestions above, and learn what works, and what doesn't.
For example:
sed "s/$oldtote/$newtote/g" $selfile
sed -n "s/$oldtote/$newtote/gp" $selfile
 
Old 04-12-2009, 04:16 AM   #7
SHARPY
LQ Newbie
 
Registered: Apr 2009
Location: Toronto, Ontario, Canada
Distribution: Fedora 11 X64
Posts: 8

Original Poster
Rep: Reputation: 0
I have actually tried, and played around with the above examples with no luck...I have tried double quotes, back ticks, so many things i cant even recall...below i'll post the function that the sed is in perhaps there is something i may be missing out on.

Code:
addsal_func()
{
echo "Item Selected: Add Sale"
echo
read -p "Enter Customer Name: " cusname
read -p "Enter Item Price: " itprice
read -p "Enter Quantity Purchased: " quant
totesale=$(($itprice*$quant))
#wittax=$(($totesale*1.13))

#######################################################################
#This egrep tests the database for the name of the customer entered
####This if condition tests if the egrep returns a true or false value and manipulates the database data accordingly
 
if gname=$(grep -F "$cusname" $selfile)
then

#Awk command which reads in the current total from the salary for the Customer Name and outputs the new number to a temp file
awk -F":" '/^'"$cusname"'/ {printf "%.2f\n",$2}' > /tmp/temp2.txt $selfile
awk -F":" '/^'"$cusname"'/ {printf "%.2f\n",totesale+$2}' > /tmp/temp.txt totesale=$totesale $selfile

#newtote is the variable which gets its value by reading in the value in the temp file
oldtote=`cat /tmp/temp2.txt`	
newtote=`cat /tmp/temp.txt`


#temp file is deleted once newtote has it's value
	rm /tmp/temp.txt
	rm /tmp/temp2.txt

sed -e "s/"$oldtote"/"$newtote"/gp" $selfile

else 

#If the customer is not already in the file, the below statement echo's to new customer name and totals into the file

echo $cusname":"$totesale >> sales.txt


fi
##############################################################################################################################
menu_func


}

Last edited by SHARPY; 04-12-2009 at 04:21 AM.
 
Old 04-12-2009, 05:35 AM   #8
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
You don't need to redirect the output of awk commands to temporary files, if their only purpose is to store a number which has to be assigned to a shell variable. You can use command substitution to assign the output of awk to the shell variable:
Code:
oldtote=$(awk -F":" '/^'"$cusname"'/ {printf "%.2f\n",$2}' $selfile)
newtote=$(awk -F":" '/^'"$cusname"'/ {printf "%.2f\n",totesale+$2}' totesale=$totesale $selfile)
The sed command shoud work. Use the -i option if you want to change its content. The only thing I notice is that when you extract the number from $selfile, you probably change the original format using the printf statement, so that when you try the substitution the pattern is not recognized anymore.

Moreover, there is a problem if the same number appear more time in the file. You can use the customer name to change only the relevant line in the file: For example:
Code:
sed "/$cusname/s/\(.*:\).*/\1$newtote/" $selfile
 
Old 04-12-2009, 01:37 PM   #9
SHARPY
LQ Newbie
 
Registered: Apr 2009
Location: Toronto, Ontario, Canada
Distribution: Fedora 11 X64
Posts: 8

Original Poster
Rep: Reputation: 0
Smile

Regular expressions of course!...I'm so new to shell scripting i forgot how it can be used. I guess 4 hours of racking my brain over other things i tend to not think so straight. Colucix, thanks for the assistance with the code lines they worked like a charm, I'm currently testing the script with different values in the database file and it all seems to be functioning. Looks like I've got a lot more to read up on about sed :P. Thanks to you all for your patience with me (a bash scripting noob) and also your great help and quick responses I've truly learned alot from all your feedback.

Thanks,
SHARPY
 
Old 04-12-2009, 01:47 PM   #10
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
You're welcome! Here is a great tutorial about sed programming. I learned a lot by reading it.
 
Old 04-12-2009, 01:49 PM   #11
SHARPY
LQ Newbie
 
Registered: Apr 2009
Location: Toronto, Ontario, Canada
Distribution: Fedora 11 X64
Posts: 8

Original Poster
Rep: Reputation: 0
Oh man that's gold! Thanks so much
 
  


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
simple sed question faizlo Programming 2 01-20-2009 08:45 PM
Sed one-liner to drop data from beginning of file? lodi Programming 6 10-29-2008 06:22 PM
LXer: Simple Shell One-Liner To Enumerate File Types In Linux and Unix LXer Syndicated Linux News 2 05-30-2008 09:47 AM
simple substitution with sed? ocicat Programming 9 02-23-2008 12:45 AM
using sed, simple question bobthebat Programming 3 04-30-2001 11:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:33 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