Need help with replacing characters in unix

August 9, 2010 at 09:02:46
Specs: Windows XP
Hi,

I have a requirement to replace 5 characters from position 8 for a each line with spaces in shell script.

Input:

001ABCD00001XYZ
097XYZU00034NYU
457GFDS08654YGT

Required Output:

001ABCD XYZ
097XYZU NYU
457GFDS YGT

Could you someone please help me with the command to be used in UNIX.

Appreciate your help.


See More: Need help with replacing characters in unix

Report •

#1
August 9, 2010 at 14:49:39
One way is to use the unix cut command to save the strings you want and glue them back together:

#!/bin/ksh

while read myline
do
  var1=$(echo "$myline"|cut -c1-7)
  var2=$(echo "$myline"|cut -c13-)
  echo "${var1} ${var2}"
done < dfile1.txt


Report •

#2
August 9, 2010 at 16:52:56
@nails, with ksh, you can use substring.
var1=${line:0:7}
var2=${line:12:}

GNU win32 packages | Gawk


Report •

#3
August 9, 2010 at 16:54:44
@OP, you can use awk

awk '{print substr($0,1,7) substr($0,13) }' file

GNU win32 packages | Gawk


Report •

Related Solutions

#4
August 9, 2010 at 20:36:37
This works. Thank You.

Report •

Ask Question