Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.ksh scripting question 2

Status
Not open for further replies.

mwesticle

Programmer
Nov 19, 2003
51
US
I'm trying to write a .ksh script that will take an input file that contains records that are 10 bytes long, and then, based on the first character, append an eleventh character. If the first character of the record = 'H', I want the script to append an 'A' to the end of the record (as byte 11). If the first character of the record = 'D', I want the script to append an 'B' to the end of the record (as byte 11). If the first character of the record = 'T', I want the script to append an 'C' to the end of the record (as byte 11).

So, if my input looks like this (without the single-quotes):

'Hxxxxxxxxx'
'Hxxxxx '
'Dxxxxxxxxx'
'Dxxxxxxx '
'Txxxx '
'Txxxxxxxxx'

I want my output to look like this (again, without the single-quotes):

'HxxxxxxxxxA'
'Hxxxxx A'
'DxxxxxxxxxB'
'Dxxxxxxx B'
'Txxxx C'
'TxxxxxxxxxC'

I don't know if I can do this with sed or nawk or something... If you have any suggestions, please let me know... Thanks!
 
Try something like this:
awk '{c=substr($0,1,1)
a=(c=="H" ? "A" : (c=="D" ? "B" : (c=="T" ? "C" : "?")))
printf "%10.10s%s\n",$0,a
}' /path/to/inputfile

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
When I try this, I get "awk: syntax error near line 2". Do you know why?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top