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 07-17-2011, 11:26 PM   #1
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Rep: Reputation: 16
sed to replace first and second instance


Guys,

I might just be crazy or particularly dumb today but what I'm trying to do is have sed replace the first and second instance of a character on a line but not after that.

Basically it is a csv file with a date formatted as dd/mm/yyyy and I need to completely remove the slashes. No problem using:

Code:
sed 's/\///g' < file > outputfile
The only problem is that one of the columns after this may also contain these slashes and shouldn't be removed.

I must have read every single sed page on the net today to find a way to do it to no avail! Is it as simple as :

Code:
sed 's/\///1' < file > outputfile
And then run the same on the output file again? these two slashes will always be the first and second slashes in the file. I guess this would accomplish it but I would rather find the right way to do it!!

As always any help is greatly appreciated!
 
Old 07-18-2011, 12:03 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,138

Rep: Reputation: 987Reputation: 987Reputation: 987Reputation: 987Reputation: 987Reputation: 987Reputation: 987Reputation: 987
I prefer to use regex differently - look for what you know exists, and concentrate on that.
If the date is always of that format, and always occurs before any other slash, the following should work.
Code:
sed -r 's@([^/]*[[:digit:]]{2})/([[:digit:]]{2})/([[:digit:]]{4}.*)@\1\2\3@'
If the leading zeroes could be missing you can also accommodate that with a minor change.

Last edited by syg00; 07-18-2011 at 12:05 AM. Reason: chnged separator char for clarity
 
Old 07-18-2011, 12:33 AM   #3
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Thanks for the quick reply syg00, tried your command and not quite what I expected:

I ran the command on the file with:

Code:
sed -r 's@([^/]*[[:digit:]]{2})/([[:digit:]]{2})/([[:digit:]]{4}.*)@\1\2\3@' < inputfile.csv > outputfile.csv
And the resulting file had no '\' removed, it was basically a copy of the original. So to test I just ran:

Code:
echo test1\test2\test3\test4\test5\ | sed -r 's@([^/]*[[:digit:]]{2})/([[:digit:]]{2})/([[:digit:]]{4}.*)@\1\2\3@'
and the output is test1test2test3test4test5 - Removing all of the slashes! This seems to be the result of everything I have tried, it will either remove all or none.

I did test with multiple '\\'s around the string just randomly and it seemed to remove 1 or two in a series and leave the rest, for instance:

Code:
echo test1\\\\test2\\\\test3\\\\ | sed -r 's@([^/]*[[:digit:]]{2})/([[:digit:]]{2})/([[:digit:]]{4}.*)@\1\2\3@'
Will result in test1\\test2\\test3
 
Old 07-18-2011, 12:39 AM   #4
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Sorry, here is a sample line of the file:

Code:
"14#####1","AUD","64#####09","10/07/20","501","AUTOMATIC DRAWINGS"," ","W1####97          ORIX/FLEET       1#####",9####.45
I've hashed out a few numbers but that is a good sample, the thrid '\' in ORIX\FLEET needs to stay, but the date slashes need to go!

MAybe I should be looking at AWK instead of sed?
 
Old 07-18-2011, 12:51 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Could this not be simpler and just be:
Code:
sed -r 's@([^/]+)/([^/]+)/@\1\2@' file

Last edited by grail; 07-18-2011 at 12:54 AM.
 
1 members found this post helpful.
Old 07-18-2011, 01:03 AM   #6
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Wow, you are a legend, works like a charm!!

I might have to look into it further cause I still cant wrap my head around the context of sed, every time I think I have it straight it turns out I was completely wrong!

But thanks again.
 
Old 07-18-2011, 02:00 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
No Probs It is as syg00 said, sometimes it pays to look at what you have exactly as being too generic can cause issues.
 
  


Reply

Tags
sed


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
find or replace through sed adarshmca Linux - Newbie 3 12-27-2010 11:35 PM
Oneline filename batch rename (specific sed instance pattern matching) briana.paige Linux - Newbie 1 11-11-2010 05:56 PM
How do I replace ' with sed.... @ngelot Linux - Newbie 2 11-02-2007 07:04 PM
SED - replace / insert furquan Programming 5 03-01-2006 06:58 PM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM


All times are GMT -5. The time now is 08:53 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration