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

1 comment:

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

© Jadu Saikia www.UNIXCL.com