LinuxQuestions.org
Review your favorite Linux distribution.
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 10-31-2010, 02:34 PM   #1
dbissman
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Rep: Reputation: 0
linux sed question


I am taking a linux class at school and I need some help with the sed command. here is the question. can someone please help.

the receipt data file from the Unit 7 Assignment: receipt (UPC, description, and retail price)
5390:Captain Crunch:4.59
2460:Milk:2.99
8735:Peanut Butter:3.39
4402:Yogurt:0.99
How could you use sed to transform the file to the following format (dollar sign added before cost):
 
Old 10-31-2010, 02:36 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Hi,

here is a good sed-tutorial to get you started:
http://www.grymoire.com/Unix/Sed.html
 
Old 10-31-2010, 03:33 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
Quote:
Originally Posted by dbissman View Post
I am taking a linux class at school and I need some help with the sed command. here is the question. can someone please help.

the receipt data file from the Unit 7 Assignment: receipt (UPC, description, and retail price)
5390:Captain Crunch:4.59
2460:Milk:2.99
8735:Peanut Butter:3.39
4402:Yogurt:0.99
How could you use sed to transform the file to the following format (dollar sign added before cost):
Once you've looked through the excellent tutorial that was posted above and you still need help, please post what you've tried and where it fails. Please, remember to wrap your code in the code tags.
 
Old 11-02-2010, 03:30 PM   #4
dbissman
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Original Poster
Rep: Reputation: 0
I came up with this

sed 's/[0-9]*. /&$/' receipt

but the dollar sign is going infront of the description instead of the last column with the dollars. Please give me an idea on what I am doing wrong.
 
Old 11-02-2010, 03:37 PM   #5
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 553

Rep: Reputation: 116Reputation: 116
Try checking for the end of line character so it always matches the second numeric occurrence rather than the first.
 
Old 11-02-2010, 03:50 PM   #6
dbissman
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Ok I guess I am really confused. I don't understand how you would do that. I need a good easy web site that will explain the command very simply, can anyone help?
 
Old 11-02-2010, 03:58 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
The website that was suggested in the second post is probably the best one. I can think of 3 ways of accomplishing the task but I'm not going to spoil the fun for you:

I'll just give you some clues:

From your attempt I gather that you know what "&" means in this context
1. Substitute: digit(s) . digit(s) AT THE END with "$&"
2. Substitute the second occurrence of ":" with ":$"
3. Substitute the second occurrence of digit(s) with "$&"

If the rest of the file follows the same pattern all three will work.


edit: Number 2 should actually be: Substitute: digit(s).digit(s)End_of_Line_Character with "$&"

Last edited by sycamorex; 11-02-2010 at 04:01 PM.
 
Old 11-02-2010, 06:40 PM   #8
ibuc
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
another way.

I took a liberty with your data file and got rid of some spaces. I was in a bash shell. <file> is your data file.

cat file |
while read line
do
array0=(`echo $line|sed s'/ //g'`)
array=(`echo $array0|sed s'/:/ /g'`)
echo "${array[0]}:${array[1]}:\$${array[2]}"
done


[tmp]# ./ex
5390:CaptainCrunch:$4.59
2460:Milk:$2.99
8735:PeanutButter:$3.39
4402:Yogurt:$0.99


Quote:
Originally Posted by dbissman View Post
I am taking a linux class at school and I need some help with the sed command. here is the question. can someone please help.

the receipt data file from the Unit 7 Assignment: receipt (UPC, description, and retail price)
5390:Captain Crunch:4.59
2460:Milk:2.99
8735:Peanut Butter:3.39
4402:Yogurt:0.99
How could you use sed to transform the file to the following format (dollar sign added before cost):
 
Old 11-02-2010, 06:53 PM   #9
ibuc
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
one less line gets the same results.

Imagine. both ways actually work, but I think this is correct.
]$ sed s'/[0-9]*\.[0-9]*$/\$&/' receipt
5390:Captain Crunch:$4.59
2460:Milk:$2.99
8735:Peanut Butter:$3.39
4402:Yogurt:$0.99



Duh!
I agree with sycamorex. Think abut it a few minutes and you can get it down to one line.

[$] sed s'/[0-9]*.[0-9]*$/\$&/' receipt
5390:Captain Crunch:$4.59
2460:Milk:$2.99
8735:Peanut Butter:$3.39
4402:Yogurt:$0.99

================
This is too long but it might be useful for something else.

cat file |
while read line
do
array=(`echo $line | sed s'/ //g' | sed s'/:/ /g'`)
echo "${array[0]}:${array[1]}:\$${array[2]}"
done

[tmp]# ./ex
5390:CaptainCrunch:$4.59
2460:Milk:$2.99
8735:PeanutButter:$3.39
4402:Yogurt:$0.99


Quote:
Originally Posted by ibuc View Post
I took a liberty with your data file and got rid of some spaces. I was in a bash shell. <file> is your data file.

cat file |
while read line
do
array0=(`echo $line|sed s'/ //g'`)
array=(`echo $array0|sed s'/:/ /g'`)
echo "${array[0]}:${array[1]}:\$${array[2]}"
done


[tmp]# ./ex
5390:CaptainCrunch:$4.59
2460:Milk:$2.99
8735:PeanutButter:$3.39
4402:Yogurt:$0.99

Last edited by ibuc; 11-03-2010 at 10:33 AM. Reason: coffee!
 
  


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
sed question? slack66 Linux - Newbie 7 07-26-2006 07:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed question tifu Programming 5 03-18-2005 01:02 PM

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

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