How to insert data befor some field in a row of data depending up on values in row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert data befor some field in a row of data depending up on values in row
# 1  
Old 03-17-2009
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  
Old 03-17-2009
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  
Old 03-17-2009
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,
Smilie
# 4  
Old 03-18-2009
Hi
where i can mention the file name to read the lines from...in ur script.
# 5  
Old 03-18-2009
Quote:
Originally Posted by aemunathan
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  
Old 03-18-2009
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  
Old 03-18-2009
Quote:
Originally Posted by aemunathan
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Analyzing last 2 fields of 1 row and 3rd field of next row

I have the following script that will average the last two fields of each row, but im not sure how to include the 3rd field of the following row. An example of the analysis that I need to perform from the input - (66.61+58.01+54.16)/3 awk '{sum=cnt=0; for (i=13;i<=NF;i++) { sum+=$i; cnt++... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. Emergency UNIX and Linux Support

[Solved] Mysql - Take data from row and copy it to another row

Sorry if I repost my question in this section, but I'm really in a hurry since I have to finish my work... :( Dear community, I have a table with two rows like: Row1 Row2 ======= ======= 7,3 text 1 1,3 text 2 1,2,3 blabla What i need to do is add/copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

4. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

5. Shell Programming and Scripting

Insert row without deleting previous data using sed

Hello, I want to add a new row to a file to insert data without deleting the previous data there. Example: file a b c d Output a b newtext c (6 Replies)
Discussion started by: joseamck
6 Replies

6. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

7. Shell Programming and Scripting

Convert row data to column data

Hi Guys, I have a file as follows: a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 I want: a 1 9875 b 786 989 (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. Shell Programming and Scripting

Add value of each row when each row has different data ype

Guys -- I am noob and I am really strugging on this one. I cant even write the pseudo code for it. I have a file that spits values in individual rows as such: file: user date type size joe 2005-25-09 log 1 joe 2005-25-09 snd 10 joe 2005-25-09 rcd 12 joe 2005-24-09 log 2 joe... (1 Reply)
Discussion started by: made2last
1 Replies

9. Shell Programming and Scripting

first row data into variables

I have a datafile having structure like col1,col2,col3 col1,col2,col3,col4,col5 col1,col2,col3,col4,col5 ..... Can we take only first row of values from datafile and put into respective variable. How col1 can be captured in a variable called v_col1 from unix script. like first row(only)... (1 Reply)
Discussion started by: u263066
1 Replies
Login or Register to Ask a Question