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 06-22-2009, 04:44 PM   #1
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Rep: Reputation: 0
Using sed


I haven't been on linux/unix for very long and need a little help using sed. I have a file that has lines of data in it. The data is in this format---> Name:SocialSecurityNum:Address:Birthdate:Salary. I need to search by name and replace the birth date. My problem is that some of the dates only have one digit for the month and they are in the MM/DD/YY format. How can I get sed to do the substitution when some months have two digits and some have one digit for the month and I don't know the birth date of the person I need to change? Thanks for any help!
 
Old 06-22-2009, 04:54 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
I don't quite understand what you want to substitute. Can you be more specific? Perhaps, you could provide us with some fictional example?
 
Old 06-22-2009, 05:03 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Code:
sed '/name/ s|[0-9][0-9]/[0-9][0-9]*/[0-9][0-9]|something|g' file
This will look only at lines containing 'name' and replace 00/00/00 and 00/0/00 (any digits in this format) with 'something'.
Are you going to replace it with a new date or what?
 
Old 06-22-2009, 05:10 PM   #4
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
I wondered how clear my description was. My data has phone numbers instead of social security numbers. Sorry about that, I looked at it wrong.

Example line of data:
John Doe:123-456-7890:123 Something Lane, Somewhere, TX, 12345:1/12/77:50000

I need to search by the name and change the birth date(1/12/77 in the example). How do I substitute a date for the one that is there when I'm not sure if there are one or two digits in the month and day?
 
Old 06-22-2009, 05:19 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by dots View Post
I wondered how clear my description was. My data has phone numbers instead of social security numbers. Sorry about that, I looked at it wrong.

Example line of data:
John Doe:123-456-7890:123 Something Lane, Somewhere, TX, 12345:1/12/77:50000

I need to search by the name and change the birth date(1/12/77 in the example). How do I substitute a date for the one that is there when I'm not sure if there are one or two digits in the month and day?
I gave you an example above:

Code:
sed '/John Doe/ s|[0-9][0-9]*/[0-9][0-9]/[0-9][0-9]|21/03/74|g' file
It will replace 1/12/77 (or 01/12/77) with 21/03/74 on the line containing 'John Doe'.
It looks for the following pattern:
1 or 2 digits / 2 digits / 2 digits

[0-9]* - it means 0 or 1 instance of the pattern preceding * (the asterisk)
In that case it's 0 or 1 digit. So [0-9][0-9]* will match 1 or 2 digits.

I hope what I'm saying makes sense.

Last edited by sycamorex; 06-22-2009 at 05:53 PM.
 
Old 06-22-2009, 05:23 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Now it's dawned on me. Silly of me. Why would you want to change the date for some other one? You want to standardise all the dates to consist of 6 digits, am I right?
 
Old 06-22-2009, 05:37 PM   #7
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
For instance, there was a data entry error and someone entered the wrong birthday. I only need to change the birthday for that one entry that is wrong. In the example, what if I need to search for "John" and change his birthday to "12/5/67". Sorry I have you so confused!

Example line of data:
John Doe:123-456-7890:123 Something Lane, Somewhere, TX, 12345:1/12/77:50000
 
Old 06-22-2009, 05:44 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
In that case, it should work:
Code:
sed '/John Doe/ s|[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]|15/6/74|g' file
We'll put the asterisk after DD and MM as we don't know which one might be in a single digit format.
edit: if you want to make the change permanent, you'll need to use the -i switch.

Quote:
sed -i '/John.......
Make sure you back up the files before try it out

Last edited by sycamorex; 06-22-2009 at 05:46 PM.
 
Old 06-22-2009, 05:52 PM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
As I have already came up with a solution for the 6-digit standardisation, I'll post it as well. Perhaps it'll come handy It seems to be working fine:

Code:
sed '/john/ s|\([^0-9]\)\([0-9]/\)|\10\2|g' file
It will do the following on the lines containing 'john':
dd/m/yy => dd/mm/yy
d/mm/yy => dd/mm/yy
 
Old 06-22-2009, 05:55 PM   #10
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
I was close earlier. I didn't have the "g" at the end. I think that is what messed me up. Thank you so much for your help!
 
Old 06-22-2009, 05:58 PM   #11
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by dots View Post
I was close earlier. I didn't have the "g" at the end. I think that is what messed me up. Thank you so much for your help!
It stands for 'global'.

btw. Are you sure you don't just want to standardise your dates to a 6-digit format? LOL
 
  


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
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
Help with sed odious1 Linux - General 2 08-29-2003 11:52 AM


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