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!

Need help with substrings in UNIX scripting

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 (using IBM AIX and Korn Shell). 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
 
#!/bin/ksh

file='btpy30'
read first - < ${file}
cp ${file} ${file}${first#${first%?}}

# FYI
#to get the last char. - ksh
#suf=${first#${first%?}}

#to get the last char. - sed
#suf=$(echo ${first} | sed &quot;s/.*\(.\)$/\1/&quot;)


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks so much Vlad for your solution..
It works except I am getting this error message....

copy[4]: -: This is not an identifier.

Can the &quot;-&quot; be replaced by another character? Also, is there any way to write the substring character in lower case...thanks again..
 
You have to adjust the posted script for your particular application. Try this:

#!/bin/ksh

file='btpy30'
read first junk < ${file}
typeset -l suffix=&quot;${first#${first%?}}&quot;
cp &quot;${file}&quot; &quot;${file}${suffix}&quot;

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I got the script to work...

You are awesome vlad!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top