ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
And instead of deleting the title i type in, for example , "apple", it deletes the whole text inside. How do i solve this problem?
Another problem which i am having is , when a database is displayed like this :
Fruit : Price : Quantity
apple : 20 : 30
banana : 30 : 40
how do i go about in making sure the whole row is deleted. For example, if i key in "apple" , to be deleted , all the information about apple should be gone. How do i do this?
And instead of deleting the title i type in, for example , "apple", it deletes the whole text inside. How do i solve this problem?
Another problem which i am having is , when a database is displayed like this :
Fruit : Price : Quantity
apple : 20 : 30
banana : 30 : 40
how do i go about in making sure the whole row is deleted. For example, if i key in "apple" , to be deleted , all the information about apple should be gone. How do i do this?
You need to put the sed command in quotes.
sed '/'"$title"'/d' Databasefile.txt >NewDatabasefile.txt
Code:
Inserting extra spaces to make the sed part more readable:
sed ' / ' "$title" ' /d '
You need to check your "read" command as well. It is still pseudocode.
For the highly structured text data you are using, it would be worthwhile
to learn about AWK as well. Your problem is better suited for awk.
* Don't use commands that read a file and write to the same file on same command line. The "> Database.txt" part is executed first, creating a new file and deleting the file you want to use for your input. This is a general rule and not just for sed. (note: You can use the -i option is sed for en-situ editing ).
* You don't need to cat the input file. The general form is
sed 'command(s)' inputfile >outputfile
Hey. Jshiwal, thanks for the help. I need to clarify with you. If I were to use awk, how would the coding be like? I read up on it but I can't seem to get it. How would It be able to retrieve the information I want. For example , if I were to search for apple, how will it pull out the information such as price and quantity.
Each field is assigned to a variable. $0 is the entire line. $1 is the first field, $2 the second and $3 is the third.
Since the "$" is used, enclose awk commands in single quotes so the shell doesn't expand them first as positional characters.
If you need a bash variable, the do the same thing as I showed you in my first post in this thread: awk -F: "/$fruit/"'{print $3}'
Use single quotes around awk variables and double around bash variables.
The info manual "GAWK: Effective Awk Programming" is very good. there are tutorials on the web as well.
thank for the tip. do you happen to know any websites or links which have good tutorials and examples of different commands? those which i found normally just have tutorials on the basic commands and not much examples of how it can be used.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.