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-25-2010, 12:12 AM   #1
mschiefmaker
LQ Newbie
 
Registered: Jun 2009
Posts: 3

Rep: Reputation: 0
Removing the comma from a currency entry


Hi

I have a file of the form
col1, col2, $330,000, col4 ...
col1, col2, col3, col4 ...
col1, col2, $230,000, col4 ...

The comma within the currency value causes the column to display unevenly when I open it so I want to remove it.

I think I should be able to use the sed command but I am not sure extactly how. All the values have 3 digits after the $ and before the , so have tried

sed -e 's/\$[0-9]*,/ but I am not sure how to 'replaced with the string found just without the comma'

So two guestions:
1. Is this a good way to do it?
2. How do I do it?

Thanks

Catherine
 
Old 04-25-2010, 12:46 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Code:
sed -r 's/(\$[0-9]{3}),/\1/'
The brackets help you save what you found and each set is assigned a number, only one here so \1
 
Old 04-25-2010, 12:51 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,936

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Look at it from the opposite perspective - define what you need to keep. Sed allows the use of extended regex sub-expressions. Psuedo code should give you the idea
Code:
 sed -r 's:(sub-1),(sub-2):\1\2:' somefile
The \1 represents what was matched by sub-1, likewise \2 (semicolon used as separator for clarity).
Have a go at the necessary regex. BTW I would suggest you use "+" rather than "*" - or better, a specification count.

Or see grails answer ...

Last edited by syg00; 04-25-2010 at 12:53 AM.
 
Old 04-25-2010, 06:30 PM   #4
mschiefmaker
LQ Newbie
 
Registered: Jun 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for this. Both solutions work but I am still having problems. When I type in

sed -r 's\$ ....

the records displayed on screen are corrected as I want but when I try to actually get it to update the file using

sed -e 's\$[0-9]{3}),:\1:g' filename for example I get the error

sed: -e expression #1, char 10: Invalid reference \1 on `s' command's RHS

Is -e the wrong instruction. have tried anything else i know, which is not alot, and am scratching my head

Thanks

Catherine
 
Old 04-25-2010, 06:51 PM   #5
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
Well.. without -r (extended regular expressions) the parentheses must be escaped. Furthermore the matching count {3} does not work and it is interpreted literally. An equivalent sed command without -r is:
Code:
sed -e 's:\(\$[0-9][0-9][0-9]\),:\1:g' file
 
Old 04-25-2010, 07:01 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,936

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
"man sed" - although in this case it's unlikely to help. You need the '-r' (you can have multiple switches)
'-e' won't do what you apparently want unless you redirect the output to a new file, or (instead) use '-i'.
 
Old 04-25-2010, 07:18 PM   #7
mschiefmaker
LQ Newbie
 
Registered: Jun 2009
Posts: 3

Original Poster
Rep: Reputation: 0
ok this is where I show how new I am. I am happy to use -r but it is not actually updating the file, it is only displaying it to the screen so I thought it meant something other than what it obviously does. How do i use -r and get it to update the file?

Thanks

Catherine
 
Old 04-25-2010, 08:38 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Use the -i option to overwrite the original file with the output.
 
Old 04-26-2010, 03:23 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Just to confirm/add to MTK358's information, you need both the -r and the -i
 
  


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
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 06:50 PM
script in removing an entry in a long list of text packets Linux - Newbie 3 09-10-2008 03:06 AM
Removing LILO Entry DrEwMoNeY Slackware 3 03-16-2006 07:55 PM
netstat: removing an entry from it LinuxRam Linux - Networking 5 11-02-2004 08:07 AM
routing tables - removing entry zigmund555 Linux - Networking 2 10-03-2003 08:57 AM

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

All times are GMT -5. The time now is 02:28 AM.

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