The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to read file line by line ani12345 UNIX for Dummies Questions & Answers 4 07-13-2009 05:50 PM
read line by line and create new file borobudur Shell Programming and Scripting 7 04-07-2009 06:30 PM
cat file1 read line-per-line then grep -A 15 lines down in fileb irongeekio Shell Programming and Scripting 6 01-28-2009 06:30 AM
I need suggestion on problem read a file line by line and do stuff madi3d8 Shell Programming and Scripting 3 01-15-2009 11:33 AM
query on how to search for a line and read 4th word from that line jaggesh UNIX for Dummies Questions & Answers 4 07-01-2008 11:21 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
kshuser kshuser is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 30
while read line do..

I have a base file FILE1 with the following data

FILE1.dat
21111111110001343 000001004OLF 029100020091112
21111111110000060 000001004ODL-CH001000020091112
22222222220000780 000001013OLF 006500020091112
23333333330001695 000001039OLF 030600020091112
23333333330000111 000001039ODL-SP002000020091112
23333333330000060 000001039ODL-CH001000020091112
24444444440001416 000001045OLF 011800020091112
......
......
..etc
i want to write a program in .KSH that the FILE2.dat should have only 4 records like below appending two new columns spouse_col & child_col at the end of each line remember this should be in while read line do loop
ODL-Ch = child_col ,
ODL-Sp = spouse_col

FILE2.DAT
spouse_col child_col
21111111110001343 000001004OLF 029100020091112 0000000 0000060
22222222220000780 000001013OLF 006500020091112 0000000 0000000
23333333330001695 000001039OLF 030600020091112 0000111 0000060
24444444440001416 000001045OLF 011800020091112 0000000 0000000

write now i am doing like this but


Code:
while read line 
 do
rec_no=`echo $line|cut -c2-10`
ben_type=`echo $line|cut -c28-33`
amount=`echo $line|cut -c11-18`
if [[ $rec_cnt -eq 1 ]]
then
     prior_rec_no=$rec_no
     prev_line=$line
else
    if [[ $rec_no -eq $prior_rec_no ]]
    then

           if [[ $ben_type =  "ODL-SP" ]]
               then
               spouse_amt=$amount
               prev_line="$prev_line  $spouse_amt"

           elif  [[ $ben_type = "ODL-CH" ]]
                then
	       child_amt=$amount
               prev_line="$prev_line  $child_amt"
           fi
    else
        echo $prev_line >>  FILE2.DAT
        prev_line=$line
	prior_rec_no=$rec_no
    fi
     spouse_amt=""
     child_amt=""
fi 
(( rec_cnt=rec_cnt + 1 )) 
  prior_rec_no=$rec_no
done <FILE1.DAT

i getting the outfile FILE2.DAT for the above code


spouse_col child_col
21111111110001343 000001004OLFXXX029100020091112 0000060 (this is wrong as this should map to child_column)
22222222220000780 000001013OLFXXX006500020091112
23333333330001695 000001039OLFXXX030600020091112 0000111 0000060
24444444440001416 000001045OLFXXX011800020091112

Last edited by kshuser; 4 Weeks Ago at 03:52 PM..
  #2 (permalink)  
Old 4 Weeks Ago
frans's Avatar
frans frans is offline
Registered User
  
 

Join Date: Oct 2009
Location: Drôme, France
Posts: 131
Your script seems a bit complicated as your explanations. I don't really understand what you want to do.
Why not use 'join' which seems to me a good tool for doing such a job.
join

Last edited by frans; 4 Weeks Ago at 05:52 PM.. Reason: add link
  #3 (permalink)  
Old 4 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761
This is how I would do it in ksh:

Code:
#!/bin/ksh
echo|cat FILE1.DAT -|while read line; do
  case ${line:27:6} in
    ODL-SP) spouse=${line:10:7} ;;
    ODL-CH) child=${line:10:7} ;;
    *)  if [[ -n $prev ]]; then
          print $prev $spouse $child
        fi
        prev=$line
        spouse="0000000"
        child="0000000"   ;;
  esac
done > FILE2.DAT


Code:
$> cat FILE2.DAT
21111111110001343 000001004OLF 029100020091112 0000000 0000060
22222222220000780 000001013OLF 006500020091112 0000000 0000000
23333333330001695 000001039OLF 030600020091112 0000111 0000060
24444444440001416 000001045OLF 011800020091112 0000000 0000000


Last edited by Scrutinizer; 4 Weeks Ago at 08:08 PM..
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0