Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Wondering what the fastest way would be to strip the text from just some delimited fields in a list.

My list looks like this:
text text:number:text:text:text:text:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*

And I want it to look like this:
text text:*:*:*:*:text:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*

So some fields that have data need to be replaced by asterisks, some fields need to be untouched, and the delimiter isn't consistent (first and second field separated by spaces). This is on a linux filesystem and a way to do it inline on the file is preferred.

Thanks much for the help!

share|improve this question

2 Answers 2

I would use a regular expression that matches your text (parentheses capture text into buffer designated by \1, \2, etc):

(.*):([0-9]+):(.*):(.*):(.*):(.*):(.*):(.*):(.*):(.*):(.*):(.*)

and a replacement regular expression:

\1:\2:\*:\*:\*:\6:\*:\*:\*:\*:\*:\*

with sed:

sed 's/matching-regularexpression/replacement-regular-expression/' name-of-text-file

You may have to fiddle with escape characters a little depending on your shell.

share|improve this answer
    
don't forget sed -f <sedscript> if you've got multiple changes to make.... –  Greeblesnort Sep 19 '09 at 4:37

I'd probably do it in perl:

perl -pi.bak -e 's/^(\w+\s\w+):\d+:\w+:\w+:\w+:(.*)/$1:$2/' FILENAME

share|improve this answer

Your Answer

 
discard

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