Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I replace the following string

hd_ma_prod_customer_ro:*:123456789:john.doe

with john.doe

Basically I need to look for the last colon (:) and delete everything before and including it.

share|improve this question

4 Answers 4

Assuming what you actually mean is that you want to delete everything up to the last colon and leave the john.doe intact:

echo 'hd_ma_prod_customer_ro:*:123456789:john.doe' |
  sed 's/.*://'
share|improve this answer
    
This works! Thanks all! –  yihyoon Jun 12 at 17:31
3  
@yihyoon, if this worked please accept this answer by clicking the green arrow next to the answer :) This will mark the question as resolved :) –  Ramesh Jun 12 at 17:33
sed -r 's/:/\t/g' filename | awk -F'\t' '{print $4}'

I am replacing all the occurrences of : with a tab and then using awk to extract the string john.doe.

If you do not have a file you can try this.

echo 'hd_ma_prod_customer_ro:*:123456789:john.doe' | sed -r 's/:/\t/g' | 
awk -F'\t' '{print $4}'

As per Graeme's comments we can use awk to print the last column alone using the NF variable of awk as below.

echo 'hd_ma_prod_customer_ro:*:123456789:john.doe' | sed -r 's/:/\t/g' | awk -F'\t' '{print $NF}'

Incorporating Graeme's comments to get rid of unnecessary sed

The command can be modified as below.

echo 'hd_ma_prod_customer_ro:*:123456789:john.doe' | awk -F':' '{print $NF}'
share|improve this answer
    
If you are going to use awk, why not just do awk -F:? –  Graeme Jun 12 at 16:37
    
@Graeme, I am still not that comfortable with awk. Basically, I try to test the commands that I originally got in my answer and try to modify them according to the user's requirement. :) –  Ramesh Jun 12 at 16:39
3  
The -F option for awk specifies the field separator. If you tell awk to use a colon, there is no need to replace the colons with tabs. Also, if you want to get the last field you can use $NF instead of $4, this would then work if there is not always 3 colons. –  Graeme Jun 12 at 16:44
    
@Graeme, I have incorporated the changes you have mentioned. Please let me know if this is fine :) –  Ramesh Jun 12 at 17:29
    
Yes, works perfectly! –  Graeme Jun 12 at 17:31

Please try

edit: removed anchor

echo 'abc:fjk' |sed 's/.*:/john.doe/g'

and to delete

echo 'abc:fjk' |sed 's/.*://g'
share|improve this answer
    
that's not what the OP's asking for –  1_CR Jun 12 at 16:34
    
please re-read the question again. –  akash Jun 12 at 16:37
    
Well you snuck in the delete after my comment, never mind –  1_CR Jun 12 at 16:39
    
oh i see . my apologies 1_CR –  akash Jun 12 at 16:40
1  
The ^anchor makes no difference in either the question's case or in your example case. Any address beginning with /.* already implies the ^. –  mikeserv Jun 13 at 3:01

Another method using awk:

awk -F: '{ print $NF }'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.