Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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:
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.
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.
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
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.
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
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
}
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:
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.