LinuxQuestions.org
Visit Jeremy's Blog.
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 01-07-2010, 02:13 PM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Searching and deleting using Sed


Hey guys ,im working on a mini project which basically use a simple database(Database.txt) which displays its information in this format :

apple
chiku
banana


Code:
#!/bin/bash
Title=
echo -n "Fruit to delete: "
read Fruit to delete

cat Database.txt | \
sed -e / $title /d > Database.txt
I tried typing out this coding bout for some reason , it gives me a error message saying

Code:
sed: -e expression #1, char 1: unterminated address regex
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?
 
Old 01-07-2010, 02:18 PM   #2
slacker_et
Member
 
Registered: Dec 2009
Distribution: Slackware
Posts: 138

Rep: Reputation: 27
"title" doesn't appeare to be defined.

--ET
 
Old 01-07-2010, 02:22 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by gregarion View Post
Hey guys ,im working on a mini project which basically use a simple database(Database.txt) which displays its information in this format :

apple
chiku
banana


Code:
#!/bin/bash
Title=
echo -n "Fruit to delete: "
read Fruit to delete

cat Database.txt | \
sed -e / $title /d > Database.txt
I tried typing out this coding bout for some reason , it gives me a error message saying

Code:
sed: -e expression #1, char 1: unterminated address regex
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

Good Luck!

Last edited by jschiwal; 01-07-2010 at 02:33 PM.
 
Old 01-07-2010, 02:28 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Code:
read title -p "Fruit to delete: "
sed -i -e "/$title/d" database
 
Old 01-07-2010, 11:15 PM   #5
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
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.
 
Old 01-08-2010, 03:23 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
This prints third record if the line contains "apple" and prints the second field.
Code:
echo 'apple : 20 : 30' | awk -F: '/apple/{print $3}'
This checks the first field for the apple pattern and prints the second and third field:
Code:
echo 'apple : 20 : 30' | awk -F: '$0 ~ /apple/{print $2,$3}'

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.

Last edited by jschiwal; 01-08-2010 at 05:50 PM.
 
Old 01-09-2010, 11:24 AM   #7
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
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.
 
Old 01-10-2010, 02:42 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I bought the O'Reilly "Sed & Awk" book. The 2nd edition is in the stores. They may have the first edition available on the web.

Here is the "GAWK: Effective Awk Programming" book. http://books.google.com/books?id=D_u...age&q=&f=false

If you install the gawk package source code and run "./configure" and "make pdf" you can generate this book yourself.

Last edited by jschiwal; 01-10-2010 at 02:59 AM.
 
Old 01-10-2010, 03:53 AM   #9
drstupid
LQ Newbie
 
Registered: Dec 2009
Posts: 21

Rep: Reputation: 15
Good awk tips:

http://adminlinux.blogspot.com/2009/...rocessing.html
 
Old 01-10-2010, 07:51 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
http://www.grymoire.com/Unix/

Some of the best tutorials I have seen
 
  


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
searching using sed ... srini_nw Linux - Newbie 7 06-30-2009 10:32 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
Probably a simple sed questions for deleting lines... oldcarguy85 Linux - General 4 03-28-2008 07:11 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

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

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