The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Not able to insert data through crontab megh SUN Solaris 2 12-29-2008 08:36 AM
Not able to insert data megh SUN Solaris 1 12-05-2008 12:20 AM
unable Insert data from .dat file to .xls can anybody help me kreddy2003 Shell Programming and Scripting 1 05-28-2008 05:33 AM
help me to to insert data babu@shell UNIX for Dummies Questions & Answers 10 10-24-2006 02:25 AM
sed, insert data from a file to another? ctcuser Shell Programming and Scripting 4 05-03-2005 01:43 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2009
aemunathan aemunathan is offline
Registered User
  
 

Join Date: May 2008
Posts: 75
How to insert data befor some field in a row of data depending up on values in row

Hi

I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example.

The original data record should be some thing like this
Code:
60119827  RTMS_LOCATION_CDR    INSTANT_POSITION_QUERY    1236574686123083rtmssrv7      20090309102806279           441           442
           783           WEB       1568          GMLC919443259137    FAILURE6         1236574665595654lessrv1
but due to some system errors the file generated has got one missing information and it looks like

Code:
60119827  RTMS_LOCATION_CDR    INSTANT_POSITION_QUERY    1236574686123083rtmssrv7      20090309102806279                         442
           783           WEB       1568          GMLC919443259137    FAILURE6         1236574665595654lessrv1
both has got 248 charaters length. but in the second record one data is missing i.e 441

i need to insert 441 exactly in that position if 442 is available as next data.
its some thing like case statements...

i will be having different values instead of 442 and their corresponding previous position data

How to go about this...any one help me out...
Am having lot of error files like this to be treated, hence can i do anything like upload in a DB and insert data based on the values in the row at the position i told before (442)

Help me out!!!
  #2 (permalink)  
Old 03-17-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by aemunathan
i will be having different values instead of 442 and their corresponding previous position data
How would you know where EXACTLY to insert the missing 'data'?
How do you know that the missing '441' should be surround by n spaces/tabs on the left and m spaces/tabs on the right?
  #3 (permalink)  
Old 03-17-2009
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Registered User
  
 

Join Date: Mar 2009
Location: Iran (Tehran)
Posts: 44
Hello there,

As it was said, you really have to define a pattern/structure for your file including the exact field numbers where data is supposed to be modified or at least a group of specific values according to which the script has to search the file and to add what is missing.

Just for giving you a first idea, if the specific values are 441 and 442, then the following KornShell script will do the job

Code:
#!/bin/ksh

RESULT=""

while read LINE
do
    for ITERATOR in $LINE
    do
        if [[ $ITERATOR = "442"  ]]
        then
            RESULT="$RESULT 441 $ITERATOR"
        else
            RESULT="$RESULT $ITERATOR"
        fi
    done
    RESULT="$RESULT\n"
done < $1

print "$RESULT" > $1
So if there are several values, you can specify them in the for loop.

Yet, this works if and only if the delimiters in your file are the default values for IFS that is, \n \t and ' '.

Regards,
  #4 (permalink)  
Old 03-18-2009
aemunathan aemunathan is offline
Registered User
  
 

Join Date: May 2008
Posts: 75
Hi
where i can mention the file name to read the lines from...in ur script.
  #5 (permalink)  
Old 03-18-2009
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Registered User
  
 

Join Date: Mar 2009
Location: Iran (Tehran)
Posts: 44
Quote:
Originally Posted by aemunathan View Post
Hi
where i can mention the file name to read the lines from...in ur script.
Obviously on the command line, for example if the script file name is myscript.ksh and your file name is myfile you write:

$ ./myscript.ksh myfile
  #6 (permalink)  
Old 03-18-2009
aemunathan aemunathan is offline
Registered User
  
 

Join Date: May 2008
Posts: 75
Hi

Its working but there is realignment in the data spacing.

The existing data positions should not be disturbed and the number of characters 248 should be maintained in the result too

There will be blank spaces at the end of each line and in between data as well. the blank space indiactes there is no data for that field.

Actually there are certain field names by which the reports are generated, if that field is not available in the report there will be blank space to mention that.

I need to maintain the length 248 characters. Since this file will be sent for formatting to different format in another server. So data has to be intact.

there must be 11 blank spaces after the timestamp : 20090309102806279
after that i need to insert the 441 if 442 is present.
  #7 (permalink)  
Old 03-18-2009
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Registered User
  
 

Join Date: Mar 2009
Location: Iran (Tehran)
Posts: 44
Quote:
Originally Posted by aemunathan View Post
Hi

Its working but there is realignment in the data spacing.

The existing data positions should not be disturbed and the number of characters 248 should be maintained in the result too

There will be blank spaces at the end of each line and in between data as well. the blank space indiactes there is no data for that field.

Actually there are certain field names by which the reports are generated, if that field is not available in the report there will be blank space to mention that.

I need to maintain the length 248 characters. Since this file will be sent for formatting to different format in another server. So data has to be intact.

there must be 11 blank spaces after the timestamp : 20090309102806279
after that i need to insert the 441 if 442 is present.

As I said what I gave you was just a first idea (not the final solution) about how to proceed. According to what you wrote, you would need to add conditions inside the for loop in order to add or remove space characters according to the appropriate context.

Code:
...
for ITERATOR in $LINE
...
splits the current line into tokens. So you have tokens and it's up to you to decide when and where to remove or insert space characters and how many. However if you look carefully at the script I just add one '\n' at the end of each line which I suppose is needed and one ' ' between each token '$RESULT $ITERATOR', so If you need more speces well you can increase them here or as I said elsewhere inside the loop by some If Else block.
Sponsored Links
Closed Thread

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 10:36 PM.


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