Reputation: 31
I'm trying to parenthesize the third character of every word in a text file using sed
. I've tried manipulating:
sed 's/\(\b[A-Z]\)/\(\1\)/g' filename
This does what I need except, it parenthesizes the first character, not the third.
It gives me: "Welcome To The Geek Stuff" => "(W)elcome (T)o (T)he (G)eek (S)tuff"
What I want is: "Welcome To The Geek Stuff" => “We(l)come To Th(e) Ge(e)k St(u)ff”
How can I parenthesize the third character of each word?
Upvotes: 3
Views: 1569
Reputation: 58483
This might work for you (GNU sed):
sed 's/\b\(.\B.\)\B\(.\)/\1(\2)/g' file
This looks for the second non-word boundary from the start of a word boundary and surrounds the following character with parens.
Upvotes: 0
Reputation: 41460
Here is an awk
version:
cat file
Hi, this is a test with some data.
awk '{for (i=1;i<=NF;i++) {split($i,a,"");if (a[3]~/[[:alpha:]]/) $i=substr($i,1,2)"("a[3]")"substr($i,4)}}1' file
Hi, th(i)s is a te(s)t wi(t)h so(m)e da(t)a.
It test if third letter in the word is an alphabetic character,
if so, recreate the word with ()
around third character.
Upvotes: 0
Reputation: 174786
This sed command must put every third character present in a word within parenthesis.
$ echo 'Welcome To The Geek Stuff' | sed 's/\b\([A-Z][a-z]\)\([a-z]\)/\1(\2)/g'
We(l)come To Th(e) Ge(e)k St(u)ff
$ echo 'Welcome To The Geek Stuff' | sed 's/\b\([a-z][a-z]\)\([a-z]\)/\1(\2)/gi'
We(l)come To Th(e) Ge(e)k St(u)ff
Add i
modifier in-order to do a case-insensitive match.
Upvotes: 6