LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using sed (https://www.linuxquestions.org/questions/linux-newbie-8/using-sed-734839/)

dots 06-22-2009 03:44 PM

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!

sycamorex 06-22-2009 03:54 PM

I don't quite understand what you want to substitute. Can you be more specific? Perhaps, you could provide us with some fictional example?

sycamorex 06-22-2009 04:03 PM

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?

dots 06-22-2009 04:10 PM

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?

sycamorex 06-22-2009 04:19 PM

Quote:

Originally Posted by dots (Post 3582718)
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.

sycamorex 06-22-2009 04:23 PM

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?

dots 06-22-2009 04:37 PM

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

sycamorex 06-22-2009 04:44 PM

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

sycamorex 06-22-2009 04:52 PM

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

dots 06-22-2009 04:55 PM

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!

sycamorex 06-22-2009 04:58 PM

Quote:

Originally Posted by dots (Post 3582754)
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


All times are GMT -5. The time now is 04:22 PM.