using substring in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using substring in shell script
# 8  
Old 10-17-2008
Your data looks like well formatted, whitespace separated data, so you can use read to do the parsing:

Code:
#!/bin/bash

while read trackingnum trackingnumsuffix date tstmpupdated
do
    echo Tracking: $trackingnum, Suffix: $trackingnumsuffix, Date: $date, Timestamp: $tstmpupdated
done

$ ./parse.sh < data
Tracking: ABC123, Suffix: 10, Date: 01/02/2008, Timestamp: 2008-01-03-00.00.00.000000
Tracking: DYUU, Suffix: 22, Date: 02/03/2008, Timestamp: 2008-01-04-00.00.00.000000
Tracking: RF33, Suffix: 88, Date: 03/05/2008, Timestamp: 2008-01-05-00.00.00.000000

MrC
# 9  
Old 10-17-2008
Code:
trackingnum=`echo $line|cut -c1-6`
trackingnumsuffix=`echo $line|cut -c8-9`
tstmpupdated=`echo $line|cut -c22-57`

# 10  
Old 10-17-2008
Quote:
Originally Posted by palsevlohit_123
Code:
trackingnum=`echo $line|cut -c1-6`
trackingnumsuffix=`echo $line|cut -c8-9`
tstmpupdated=`echo $line|cut -c22-57`

For a large file, this has very large overhead. Per line of the file, this code does a unwarranted number of fork/execs, and will be very slow.

Always use built-ins when possible, and better yet, use programs designed for fast data processing and scripting such as awk or perl.
MrC
# 11  
Old 10-17-2008
The following code is not working

Code:
trackingnum=`echo $line|cut -c1-6`
trackingnumsuffix=`echo $line|cut -c8-9`
tstmpupdated=`echo $line|cut -c22-57`

I didnot get either output or error msg..
F Y I ... we are using Korn shell script

Following is the actual data in production.

Code:
BEYJYWG83L                     0 R            902     -                    B       System Exception Occured, please refer to BMW_EXCEPTION_LOG table                                    2008-09-10-00.41.32.787352             
BEYJYXMT4K                     0 R            902     -                    B       System Exception Occured, please refer to BMW_EXCEPTION_LOG table                                    2008-09-11-19.01.23.878551             
BEYEHF93QY                     0 R            003                          B       No message exists in ZBV_BILL_REJCT_RSN table                                                        2008-08-28-16.31.01.881717             
BEYJYTT8FQ                     0 R            013                          B       Invalid Transaction Code                                                                             2008-09-04-17.39.04.005237             
BEYJYTT8FQ                     0 R            016                          B       Invalid Fee Code                                                                                     2008-09-04-17.39.04.008137             
BEYJYTT8FQ                     0 R            016                          B       Invalid Fee Code                                                                                     2008-09-04-17.39.04.011939             
BEYJYTT8FQ                     0 R            016                          B       Invalid Fee Code                                                                                     2008-09-04-17.39.04.012851             
BEYJY2SZVL                     0 R            003                          B       Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG                               2008-09-16-09.22.29.120192             
BEYJY2SXNG                     0 R            003                          B       Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG                               2008-09-16-09.19.33.868772             
BEYJY2SMS3                     0 R            003                          B       Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG                               2008-09-16-08.53.07.872138             
BEYJY2SMJD                     0 R            003                          B       Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG                               2008-09-16-08.50.17.307262             
BEYJY2EG2K                     0 R            003                          B       Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG                               2008-09-17-15.56.03.418917

I am writing for a sample one. If it works I will customize it according to the production job.

Please advice how to proceed furthur..

Krishnakanth
# 12  
Old 10-17-2008
Ok, we can't use whitespace as a column separator, but we can use another trick. We'll use sed in one pass to rearrange the data into whitespace separated columns that read can use:

Code:
#!/bin/ksh

sed -E 's/^(.{10}).{36}(.{3}).{34}(.{101})(.{26})/\1 \2 \4 \3/' in2 |
while read trackingnum trackingnumsuffix tstmpupdated text
do
    echo Tracking: \"$trackingnum\", Suffix: \"$trackingnumsuffix\", Timestamp: \"$tstmpupdated\", Text: \"$text\"
done

$ ./parse.sh
Tracking: "", Suffix: "", Timestamp: "", Text: ""
Tracking: "BEYJYWG83L", Suffix: "902", Timestamp: "2008-09-10-00.41.32.787352", Text: "System Exception Occured, please refer to BMW_EXCEPTION_LOG table"
Tracking: "BEYJYXMT4K", Suffix: "902", Timestamp: "2008-09-11-19.01.23.878551", Text: "System Exception Occured, please refer to BMW_EXCEPTION_LOG table"
Tracking: "BEYEHF93QY", Suffix: "003", Timestamp: "2008-08-28-16.31.01.881717", Text: "No message exists in ZBV_BILL_REJCT_RSN table"
Tracking: "BEYJYTT8FQ", Suffix: "013", Timestamp: "2008-09-04-17.39.04.005237", Text: "Invalid Transaction Code"
Tracking: "BEYJYTT8FQ", Suffix: "016", Timestamp: "2008-09-04-17.39.04.008137", Text: "Invalid Fee Code"
Tracking: "BEYJYTT8FQ", Suffix: "016", Timestamp: "2008-09-04-17.39.04.011939", Text: "Invalid Fee Code"
Tracking: "BEYJYTT8FQ", Suffix: "016", Timestamp: "2008-09-04-17.39.04.012851", Text: "Invalid Fee Code"
Tracking: "BEYJY2SZVL", Suffix: "003", Timestamp: "2008-09-16-09.22.29.120192", Text: "Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG"
Tracking: "BEYJY2SXNG", Suffix: "003", Timestamp: "2008-09-16-09.19.33.868772", Text: "Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG"
Tracking: "BEYJY2SMS3", Suffix: "003", Timestamp: "2008-09-16-08.53.07.872138", Text: "Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG"
Tracking: "BEYJY2SMJD", Suffix: "003", Timestamp: "2008-09-16-08.50.17.307262", Text: "Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG"
Tracking: "BEYJY2EG2K", Suffix: "003", Timestamp: "2008-09-17-15.56.03.418917", Text: "Policy / Contract Account Number are pending in the ZB_MASTER_DATA_LOG"

MrC
# 13  
Old 10-17-2008
Quote:
Originally Posted by kmanivan82
If I give the following code
Code:
trackingnum=`expr substr "$line" 1 6`

I am not getting any output/error message.

You wouldn't get any output because you are not outputing anything.

Do you mean that $trackingnum is empty?
Quote:

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


Of course. Since $line is not quoted, it is being separated into multiple strings, instead of a single string.

You can get the first 6 characters of $line with:

Code:
temp=${line#??????}
trackingnum=${line%"$temp"}


Last edited by cfajohnson; 10-17-2008 at 08:22 PM..
# 14  
Old 10-21-2008
I have tried to execute the following script as per MrC's syntax. It is throwing the error message as follows.

Code:
#! /bin/ksh
############################
#   AFI Monitor Script
############################

. /db2/uszlad48/sqllib/db2profile
export mondir=/home/bmwdev1/script/krishna/arc
export monlog=$mondir/rcbl1.log

sed -E 's/^(.{10}).{36}(.{3}).{34}(.{101})(.{26})/\1 \2 \4 \3/' in2 | 
while read trackingnum trackingnumsuffix tstmpupdated text
do
    echo Tracking: \"$trackingnum\", Suffix: \"$trackingnumsuffix\", Timestamp: \"$tstmpupdated\", Text: \"$text\"
done

exit 0

# ksh arc11.ksh
arc11.ksh[6]: ./db2/uszlad48/sqllib/db2profile:  not found
sed: illegal option -- E
Usage:  sed [-n] Script [File ...]
        sed [-n] [-e Script] ... [-f Script_file] ... [File ...]
bmwdev1@laaddb26  /home/bmwdev1/script/krishna/arc
#

what is wrong with this syntax ?

Please help me to fix this problem.

Krishnakanth
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