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!
|
I don't quite understand what you want to substitute. Can you be more specific? Perhaps, you could provide us with some fictional example?
|
Code:
sed '/name/ s|[0-9][0-9]/[0-9][0-9]*/[0-9][0-9]|something|g' file Are you going to replace it with a new date or what? |
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? |
Quote:
Code:
sed '/John Doe/ s|[0-9][0-9]*/[0-9][0-9]/[0-9][0-9]|21/03/74|g' file 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. |
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?
|
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 |
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 edit: if you want to make the change permanent, you'll need to use the -i switch. Quote:
|
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 dd/m/yy => dd/mm/yy d/mm/yy => dd/mm/yy |
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!
|
Quote:
btw. Are you sure you don't just want to standardise your dates to a 6-digit format:)? LOL |
All times are GMT -5. The time now is 04:22 PM. |