Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I got this question on a task. Nothing works.. Can someone help me?

Write a sed script that replace all names with an alternative,

search for every words that:

  • Start with a capital,
  • Longer than two letters,
  • Where there is a white space in front of it and
  • The character before the white space isn't a punctuation that ends a sentence.

Replace those words in "Derp"+ the last two characters of the word.

share|improve this question
    
Can you please provide an example of the desired action? And can you also include what have you tried already? –  Eugene S Feb 16 '12 at 22:00

2 Answers 2

up vote 1 down vote accepted

This is quite awkward to do in sed, but here's a version that can work provided there's one character (I chose %) that is never going to appear in the input. The character is used for marking.

Say you have a text file called words with the following contents:

Will He beat Sit Down Boy Oh Not now Latch Wi, Qq or Spat? GNU Hurd, protocols on GNU Mach. The Hurd versus Unix.

The following bash script will do the job:

cat words
sed 's/ [A-Z][A-Za-z]*[A-Za-z]\{2\}/%&/g' words|tee a
sed 's/\([.!?]\)%/\1/g' a|tee b
sed 's/% [A-Za-z]*\([A-Za-z]\{2\}\)/ Derp\1/g' b|tee c

The output of the above will be (I separated each by new line):

Will He beat Sit Down Boy Oh Not now Latch Wi, Qq or Spat? GNU Hurd, protocols on GNU Mach. The Hurd versus Unix.

Will He beat% Sit% Down% Boy Oh% Not now% Latch Wi, Qq or% Spat?% GNU% Hurd, protocols on% GNU% Mach.% The% Hurd versus% Unix.

Will He beat% Sit% Down% Boy Oh% Not now% Latch Wi, Qq or% Spat? GNU% Hurd, protocols on% GNU% Mach. The% Hurd versus% Unix.

Will He beat Derpit Derpwn Derpoy Oh Derpot now Derpch Wi, Qq or Derpat? GNU Derprd, protocols on DerpNU Derpch. The Derprd versus Derpix.

Here's how it works:

  • The first line just prints the file, so you see the starting position.
  • The second line marks all capitalized words that are preceded by a space and are longer then 2 characters with %. Thus, it will e.g. mark Latch as % Latch. Note the space, I'll call this space-word.
  • The third line will remove the mark from all the space-words that are preceded by a character ending a sentence (for simplicity, I chose only ., !, or ? - you can add others, such as ) or such if needed)
  • The third line will do actual Derp transformation - i.e. replace all space-words that are marked with % with DerpXX, XX being two last characters of that space-word

Note there are technicalities that were not addressed here, such as:

  • This will work only for US ASCII words (e.g. will not work for all French words, such as Être)
  • Other characters might need to be considered (e.g. is Oceans in "Oceans Eleven" considered a word even though it has " in front?)
  • Will not work for non-space whitespace (e.g. tabs)

and so on.

To make it one purely a sed script, just concatenate:

sed '
s/ [A-Z][A-Za-z]*[A-Za-z]\{2\}/%&/g
s/\([.!?]\)%/\1/g
s/% [A-Za-z]*\([A-Za-z]\{2\}\)/ Derp\1/g
' words

Obviously, in real world, I would not use sed to do tasks like this. Then again, I would probably not have tasks like this, either... :)

share|improve this answer
    
Thank you ! It worked ! I my answer on that question was wrong because I only searched for [A-Z]... Thanks ! –  Jleeeeny Feb 17 '12 at 8:23
    
Sure thing, glad it worked! –  icyrock.com Feb 18 '12 at 0:46
    
@Jleeeeny: Welcome to Super User, please consider voting up and accepting answers that helped you. You can do this with the up arrow above the vote number as well as the accept mark under the voting controls. This way both the person that helped you and you yourself will receive reputation. Also check out the shorter version by potong. –  Tom Wijsman Feb 19 '12 at 14:30

This might work for you:

sed ':a;s/\([^.!?] \)[A-Z][A-Za-z]*\([A-Za-z]\{2\}\)\>/\1\nDerp\2/;ta;s/\n//g' file
share|improve this answer
    
Welcome to Super User! Please note that the other answer has a comment that it helped. What does your version do different / better? Could you elaborate on the differences? –  Tom Wijsman Feb 19 '12 at 14:27
1  
But now it only change the surname. So when there is a surname and familyname it only change the surname in derp+2characters. How can I also change the familyname, in herp+2characters? I tought the same search string but that didn't work... –  Jleeeeny Feb 19 '12 at 19:39
    
@Jleeeeny please provide test data –  potong Feb 20 '12 at 23:51
    
@potong I got it ! :) but thanks for your help –  Jleeeeny Feb 21 '12 at 10:41

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.