using substring in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using substring in shell script
# 1  
Old 10-16-2008
using substring in shell script

This is the data I am having in a file
Just for sample I have given 3 records. The file which I am having consists of n number of records.
Code:
ABC123 10 01/02/2008 2008-01-03-00.00.00.000000
DYUU   22 02/03/2008 2008-01-04-00.00.00.000000
RF33   88 03/05/2008 2008-01-05-00.00.00.000000

trackingnum in the position 1-6 6 bytes
trackingnumsuffix in the position 8-9 2 bytes
effdate in the position 11-20
tstmpupdated in the position 22-57 26 bytes
I dont know how to use substring.
Basically I need to extract the trackingnum, trackingnumsuffix and tstmpupdated and pass those values to a query. If it is present in that table, then I need to store the output of query to a file.
I have tried by writing the shell script as follows.
Code:
====================
#! /bin/ksh
############################
#   AFI Monitor Script
############################
. /db2/uszlad48/sqllib/db2profile
export mondir=/home/bmwdev1/script/krishna/arc
export monlog=$mondir/rcbl2_`date +%Y%m%d`.log
# connect to DB
db2 connect to r2pdev user bmwdevup using summer08
while read line
do
trackingnum=`expr substr $line 1 6`
trackingnumsuffix=`expr substr $line 8 9`
tstmpupdated=`expr substr $line 22 57`
#db2 "SELECT * FROM ZB_RCBL_ERROR_MSG_MIG WHERE TRACKING_NUM = $TRACKING_NUM AND TRACKING_NUM_SUFFIX = $TRACKING_NUM_SUFFIX AND TIMESTAMP_UPDATED = $TIMESTAMP_UPDATED WITH UR" >> new.log
done < "$monlog" 
# disconnect from DB2
db2 terminate
exit 0
====================

The above shell script is not working. It throws error message.
Can anyone help me to fix this issue.
Krishnakanth
# 2  
Old 10-16-2008
How about using awk, printing it $1, $2..

e.g.
trackingnum=echo $line|awk '{print $1}'
# 3  
Old 10-16-2008
Quote:
Originally Posted by kmanivan82
Code:
====================
#! /bin/ksh
############################
#   AFI Monitor Script
############################
. /db2/uszlad48/sqllib/db2profile
export mondir=/home/bmwdev1/script/krishna/arc
export monlog=$mondir/rcbl2_`date +%Y%m%d`.log
# connect to DB
db2 connect to r2pdev user bmwdevup using summer08
while read line
do
trackingnum=`expr substr $line 1 6`


Code:
trackingnum=`expr substr "$line" 1 6`

Quote:
Code:
trackingnumsuffix=`expr substr $line 8 9`
tstmpupdated=`expr substr $line 22 57`
#db2 "SELECT * FROM ZB_RCBL_ERROR_MSG_MIG WHERE TRACKING_NUM = $TRACKING_NUM AND TRACKING_NUM_SUFFIX = $TRACKING_NUM_SUFFIX AND TIMESTAMP_UPDATED = $TIMESTAMP_UPDATED WITH UR" >> new.log
done < "$monlog" 
# disconnect from DB2
db2 terminate
exit 0
====================

The above shell script is not working. It throws error message.

What is the error messsage?

Do you still get it after enclosing $line in quotes?
# 4  
Old 10-17-2008
If I give the following code
Code:
trackingnum=`expr substr "$line" 1 6`

I am not getting any output/error message.

If I give the following code
Code:
trackingnum=`expr substr $line 1 6`


I am getting the following error message
Code:
expr: non-numeric argument

expr: non-numeric argument

expr: non-numeric argument

expr: non-numeric argument

expr: non-numeric argument



Please advice how to proceed furthur

Krishnakanth
# 5  
Old 10-17-2008
please post the whts there in $line??
# 6  
Old 10-17-2008
There is a file called rcbl2_`date +%Y%m%d`.log

It consists of the following

Code:
ABC123 10 01/02/2008 2008-01-03-00.00.00.000000
DYUU   22 02/03/2008 2008-01-04-00.00.00.000000
RF33   88 03/05/2008 2008-01-05-00.00.00.000000

For sample I have given 3 records. But in production there will be n number of records.
So I am reading line by line in a loop.

The first field trackingnum will be in the position 1-6
The field trackingnumsuffix will be in the position 8-9
the tstmpupdated will be in the position 22-57

Code:
while read line
do

	trackingnum=`expr substr $line 1 6`
	trackingnumsuffix=`expr substr $line 8 9`
	tstmpupdated=`expr substr $line 22 57`
	db2 "SELECT * FROM ZB_RCBL_ERROR_MSG_MIG WHERE TRACKING_NUM = $TRACKING_NUM AND TRACKING_NUM_SUFFIX = $TRACKING_NUM_SUFFIX AND TIMESTAMP_UPDATED = $TIMESTAMP_UPDATED WITH UR" >> new.log

done < "rcbl2_`date +%Y%m%d`.log"

Please advice how to proceed furthur.

Krishnakanth
# 7  
Old 10-17-2008
try to echo $line inside while loop and check whether you are getting the required line..
and you have to use double quotes if $line has space
because i am getting no errors
Code:
> v="ABC123 10 01/02/2008 2008-01-03-00.00.00.000000"
> m=`expr substr "$v" 1 6`
> echo "$m"
ABC123

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Shell Extract substring

Hi all, Please, i'd like to extract string just before '.fr'. Here is some lines of my file: g-82.text.text1.fr.worker1 g-xx.yyyyyy.zzzz.fr.worker2 i'd like to extract this text: g-82.text.text1 g-xx.yyyyyy.zzzz Please, which command i have to use in my script shell ? ... (16 Replies)
Discussion started by: chercheur111
16 Replies

2. Shell Programming and Scripting

Substring check in IF condition in shell script

I want to check if the string has the substring in IF condition then process... i tried below but not working if ]; then ............. field can be "reserved1" ....reservedn / fillspaces1 ... fillspacesn (4 Replies)
Discussion started by: greenworld123
4 Replies

3. Shell Programming and Scripting

Need help with Korn Shell script for substring printing

Hi all, I am new to scripting. I have a file with colon separated values called mylist.txt cat mylist.txt 192.123.76.89:lmprod89 162.122.20.28:lmtstserver28 10.80.32.139:hewprod139 . . using our internal os utility (called mvsping) we need to check all these servers if they are... (6 Replies)
Discussion started by: kraljic
6 Replies

4. Shell Programming and Scripting

shell script for extracting out the shortest substring from the given starting and en

hi all, i need an urgent help for writing a shell script which will extract out and print a substring which is the shortest substring from the given string where first and last character of that substring will be given by the user. for e.g. if str="abcdpqracdpqaserd" now if the user gives 'a'... (18 Replies)
Discussion started by: pankajd
18 Replies

5. Shell Programming and Scripting

Substring in shell script

I need a help in getting substring of each line in input file. I am writing a script that will read a file from a directory on daily basis, I mean everyday a new file will be stored in this directory, it will replace old file. I have to read contents of this file, the contents will be as... (5 Replies)
Discussion started by: jyotib
5 Replies

6. Shell Programming and Scripting

help for shell script of finding shortest substring from given string by user

please give me proper solution for finding a shortest substring from given string if string itself and first char and last char of that substr are also given by user if S="dpoaoqooroo" and FC="o" and LC="o",then shortest substr is "oo" and rest of the string is "dpoaoqroo" i have code but it is... (1 Reply)
Discussion started by: pankajd
1 Replies

7. UNIX for Dummies Questions & Answers

Substring in Shell Script

Hi I'm new to Shell scripting. Someone please help me in extracting a portion of string from a file. Eg: I got a file like, Readme.txt and has the following name value pairs input1 : /homes/input1/ input2 : /homes/input2/ ... ... When I give the parameter input1, the value... (3 Replies)
Discussion started by: smartbuddy
3 Replies

8. UNIX for Dummies Questions & Answers

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (5 Replies)
Discussion started by: csrazdan
5 Replies

9. Shell Programming and Scripting

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (1 Reply)
Discussion started by: csrazdan
1 Replies

10. Shell Programming and Scripting

Substring in C shell script?

i am a new user of C-shell script. I want to know can i create a substring in a string. That means when i got a variable $input = "it is number 2" I want to get the "2" to be another variable. Can i do that in C-shell and how to ? Thank you so much dinodash (0 Replies)
Discussion started by: dinodash
0 Replies
Login or Register to Ask a Question