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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substrings using Korn Shell

Status
Not open for further replies.

javsap

Programmer
Jul 14, 2003
6
US
I am not able to get substring syntax using awk to work in my UNIX script. I want to look at the first record from my data files and then copy the data file to a new file name based on a string in the first record. In the example (first record from data file) below the data file is called "btpy30". I want to look at "PY3012B" in the first record below and since I have "B" at the end I want to copy this data file btpy30 to btpy30b. I would really appreciate it if anybody has any ideas on how to accomplish this! Thanks for your help.

Example of record:

PY3012B 04 ME06302003 12B MED PAYROLL EXPENDITURE ME0406302003
20030630011000 3314 NN 04
000000000000011311
1.08
 
Hi javsap,

file=$1 -- Input File btpy30
file2=$file"b"
if [`head -1 $file | cut -f 1 -d " "` = "PY3012B" ];then
cp $file $file2
fi

or
if [ `head -1 $file | cut -c7` = "B" ];then

the -d option with the cut command is for your field separator. If your field separator is a tab then you don't need to include -d option.

Hope this is of help.

Teklid
 
In Ksh93 the substring referencing is done using ${variable:eek:ffset:length} or ${variable:eek:ffset}
 
Thanks for your help folks..I was able to resolve this problem yesterday.
 
sorry...I should have included that...below is the code I used


#!/bin/ksh

file='btpy30'
read first junk < ${file}
typeset -l suffix=&quot;${first#${first%?}}&quot;
cp &quot;${file}&quot; &quot;${file}${suffix}&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top