File handling in bash shell scripting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers File handling in bash shell scripting
# 1  
Old 02-09-2011
File handling in bash shell scripting

i am new to shell scripting and stuck at one place in my program.

i am reading data from one structured file and extracting some data from particular lines and then writing into the output file. In that reading input file line by line from while loop.

Code:
while read line 
 do
   rectype=line | cut c-1-2
   case rectype in
         10)
             header
          ;;
         20)
             continue
         ;;
         30)
           X=line | cut c-1-6
            data1 X

         ;;
         31)
            data2
          ;;
         32)
            data3
          ;;
         88)
            continue
         ;;
         89)
            trailer
         ;;
         *)
            echo "error in the file"
            exit 1 #exit script with error
         ;;
      esac
 done < input.txt

inside case i am calling different functions and writing into the output file, while i am extracting values with the help of cut it will go to the next line and loop continue there until end of file but i want to extract text at particular line and again go to the next line loop and then check the case.
thanks in advance.

Last edited by radoulov; 02-09-2011 at 07:14 PM.. Reason: Code tags, please!
# 2  
Old 02-09-2011
I believe that if you post a sample of your input data and an example of the desired output, you'll get a quicker (and most probably better) answer.
# 3  
Old 02-10-2011
thanks for the reply

input file like:
Code:
10014526728998900
20097627893409458908890890485
30977874018348558583834089497876974994
319739759085094890589088498979787020923908
322209489458797847594758058498949479985985984
88038542759792P499845
897394098598908989689089

the file data is not fix but first 2 letter will same and 30, 31 and 32 will come many times inside 20 and 88.

and i want to extract 485585 (position 13 to 19 data ) from 30 and some more fields from 30, 31 and 32 and then write into the output file.

Last edited by radoulov; 02-10-2011 at 06:12 AM.. Reason: Code tags, please!
# 4  
Old 02-10-2011
It would be easier if you provide the desired output, based on the sample output above, as well.
# 5  
Old 02-10-2011
extract (position 13 to 19 data ) from 30, 31 and 32
Code:
awk '/^3[0-9]/{print substr($0,13,6)}' infile

485585
094890
797847

# 6  
Old 02-10-2011
Code:
 $ ruby -ne 'puts $_[12,6] if /^3[0-2]/ ' file

# 7  
Old 02-10-2011
sample outout will be

0001256457558
001644855855960790
002494850496568568

starting 000,001 and 002 is fixed and rest data is variable and 485585 coming from the input file.

my whole code is below
#!/bin/sh
# write the output file header
function header()
{
rec_typ=000
file_var=00001
fieldid_1=BP
fieldid_2=`date +%y%m%d`
fieldid_3=XXXXXX
flag=P
echo $rec_typ $file_var $fieldid_1$fieldid_2$fieldid_3 $flag > output.txt
}
# append the BPF file with data record and extract the KID information
function transaction1()
{
rec_typ=001
bin_num=$1
account_num=$2
ref_num=
debit_rej_cde=
payment_cde=XXX
adjust_cde=
cre_deb_cde=
pay_curr_cde=
sett_ref_num=
o_sett_ref_num=
o_tran_num=
pay_amount=
card_number=
echo $rec_typ $bin_num $account_num $ref_num $debit_rej_cde $payment_cde $adjust_cde $cre_deb_cde $pay_curr_cde $sett_ref_num $o_sett_ref_num $o_tran_num $pay
_des $pay_amount $card_number >> BPF
}
#append the BPF file with this record
function transaction2()
{
echo="ABC"
}
#append the BPF file with transaction3 record
function transaction3()
{
pay_des=
}
#append the BPF file with trailer record
function trailer ()
{
rec_typ=002
deb_amount=
deb_count=
cre_amount=
cre_count=
}
#if [ -f "OCR.txt"]; then
END=no
while [ $END=no ]
do
exec a1
echo $a1
rectype=`line | cut -c1-2`;
echo "$rectype"
case "$rectype" in
10)
header
echo "inside header"
continue
;;
20)
echo "inside batch header"
continue
;;
30)
BIN=`line | cut -c13-18`;
ACCOUNT=`line | cut -c19-30`;
echo "$ACCOUNT"
transaction1 "$BIN" "$ACCOUNT"
echo "inside transaction1"
continue
;;
31)
transaction2
echo "inside transaction2"
continue
;;
32)
transaction3
echo "inside transaction3"
continue
;;
88)
echo "inside batch trailer"
continue
;;
89)
trailer
echo "inside trailer"
END=yes
exit 0
;;
*)
echo "$rectype"
echo "reeta error in the file"
exit 1 #exit script with error
;;
esac
done < "input.txt"
#read of file
#exit script OK
# fi

may be now you will get better idea what i am trying to do.
code is working file only thing BIN=`line | cut -c13-18`; extracting data from the next line of the input file i want to extarct data from current line of the input file.

thanks in advance.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Learn bash shell scripting

I do not know shell scripting. But at work place, I have got an in and out shell scripting task. I just need to understand a very big script. Is there any tool in which I can place the script and it can tell me the meaning of the whole script? (3 Replies)
Discussion started by: lg123
3 Replies

2. UNIX for Dummies Questions & Answers

BASH Shell Scripting: If, Then Statement

I'm having trouble trying to create a BASH shell script. I want the user to input a command "cat file_name.c" and then the shell script will delete all comments "/* */" from file_name.c else exit. So far I have this: #!/bin/bash read "cat file" // User will input command cat... (7 Replies)
Discussion started by: inkjoy00
7 Replies

3. Shell Programming and Scripting

Bash shell scripting doubt

Hello All, I am setting up a cron job, where i am calling a shell script to make few builds. I got struck at a point, need some expert inputs to proceed further. The script is categorized in 5 parts and in the last part while building software it asks for few questions like:- 1. Build mode... (4 Replies)
Discussion started by: sahil_jammu
4 Replies

4. Homework & Coursework Questions

Help with bash shell scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a bash shell script that takes as an argument a name. Then your script will ask the user for two numbers.... (15 Replies)
Discussion started by: boyboy1212
15 Replies

5. Shell Programming and Scripting

Error handling in Unix shell scripting

Hello, I have written a shell script and suppose there is any error in the script. How i can do exception handling in shell script.for example i have below code sqlplus -s <<uid>>/<<pwd>>@<<$ORACLE_SID>> <<EOF > 1_pid1.log set pagesize 0 set feedback off set heading off set linesize 200... (1 Reply)
Discussion started by: rksingh003
1 Replies

6. Shell Programming and Scripting

File handling with bash shell scripting

Hi all, Can anyone guide to get tricks for file handling in bash shell? Thanks in advance. Thanks Deepak (2 Replies)
Discussion started by: naw_deepak
2 Replies

7. Shell Programming and Scripting

Bash shell Scripting help

wwww wwwwwwww wwwwwwwwwwwww (0 Replies)
Discussion started by: keyvan
0 Replies

8. Shell Programming and Scripting

Help!! bash shell scripting..

Can any1 please help me... i'm really lost in using bash shell scripting... and i got to hand this up on monday... please anyone teach me how to do this assignment... Please use basic things because i just learn the program only... thanks ... (1 Reply)
Discussion started by: Fr0z3n999
1 Replies

9. Shell Programming and Scripting

bash shell scripting

Hi, i am new to UNIX. I have couple of basic questions. 1. Is the syntax for BASH shell programming same in the LINUX and SUN SOLARIS operating systems? 2. I have to work on BASH shell programming in SUN SOLARIS operating system. I am going through the documentation from the following... (4 Replies)
Discussion started by: azazalis
4 Replies

10. Shell Programming and Scripting

file handling with ksh scripting

how can i write content of a variable to a file? how can i read standard output into a variable? (1 Reply)
Discussion started by: gfhgfnhhn
1 Replies
Login or Register to Ask a Question

Featured Tech Videos