Monday, December 7, 2009

Replace first 5 characters - awk and sed


Input file:

$ cat file.txt
sl909678342-slp10
sl919678362-slp11
sl929678322-slp12
sl929678382-slp12

Required output:

Replace first 5 characters in each line of the above file with 'XXXXX'. i.e. the required output:

XXXXX678342-slp10
XXXXX678362-slp11
XXXXX678322-slp12
XXXXX678382-slp12

Awk solution:

$ awk '{
first=substr($0,1,5)
gsub(/./,"X",first)
end=substr($0,6)
print first end
}' file.txt

Read about awk substr function here

Sed solution:

Using 'extended regular expressions' with sed (-r option)

$ sed -r "s/^(.{0})(.{5})/\1XXXXX/" file.txt


Related posts:

- Substitute character by position using sed
- Replace text based on position using awk
- Print or remove first few characters in bash
- Print first character in a field using awk
- Insert text after certain characters using awk and sed

3 comments:

Johan said...

Why is the sed version like this?
sed -r "s/^(.{0})(.{5})/\1XXXXX/" file.txt

The first match in regex is "anything 0 times" which amounts to nothing. This should suffice:
sed -r "s/^(.{5})/XXXXX/" file.txt

Karan Bohra said...

Some of the methods to replace first 5 characters with Xs:

1. partition the string into 2 halves (A5 and remaining, aka, A*),
then drop the first half & in it's place put 5 Xs
perl -ple '$_ = "X" x 5 . (unpack "A5A*")[1]' file.txt

2. here we make use of the lvalue form of 'substr'
perl -ple 'substr($_, 0, 5) = "X" x 5' file.txt

3. Sprinkle the "l" (list pattern space) command at various locations to see what's going on...

sed -e '
s/./&\n/5
h;s/.*\n//;x
s/\n.*//
s/./X/g
G
s/\n//

' file.txt

sed -e '
l
s/^/\n/
:loop
l
s/^\(X\{0,5\}\)\n./\1X\n/
tloop
l
s/\n//
l

' file.txt

Taufik Zukhan F said...

Hi Jadu,

If i have line like below :

1234


First i want to create something like below :


1234XXXX
1234XXX
1234XX





How to solve this challenges jadu ?




Thanks & Regards,


Taufik

© Jadu Saikia www.UNIXCL.com