![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 06:33 AM |
| help me to to insert data | babu@shell | UNIX for Dummies Questions & Answers | 10 | 10-24-2006 03:25 AM |
| sed, insert data from a file to another? | ctcuser | Shell Programming and Scripting | 4 | 05-03-2005 02:43 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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!!! |
|
|||||
|
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,
|
|
||||
|
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. |
|
|||||
|
It's very difficult in script shell to manipulate line with spaces ... Try this code and launch it like : ./myscript infile outfile Assume value 441/442 on positions 116 and 130. If some lines contains a # character, change it in the script with another non-used character. Code:
#!/bin/ksh
POS1=116 ; VAL1=441
POS2=130 ; VAL2=442
VAL_LEN=3
XPOS1=$((POS1 + $VAL_LEN - 1))
XPOS2=$((POS2 + $VAL_LEN - 1))
echo POS1=$POS1,XPOS1=$XPOS1
echo POS2=$POS2,XPOS2=$XPOS2
infile=$1
outfile=$2
NBL=$(wc -l $infile)
NBC=0
while [ $NBC -lt $NBL ]
do
(( NBC = NBC + 1 ))
xval1=$(head -$NBC $infile | tail -1 | cut -b$POS1-$XPOS1)
xval2=$(head -$NBC $infile | tail -1 | cut -b$POS2-$XPOS2)
if [ "$xval2" = "$VAL2" ]
then
if [ "$xval1" = "$VAL1" ]
then
head -$NBC $infile | tail -1 >> $outfile
else
# Must replace with $VAL1
echo "Must replace on line "$NBC
k1=$(head -$NBC $infile | tail -1 | sed 's/ /#/g')
(( ZPOS1 = POS1 - 1 ))
k2=$(echo $k1 | cut -b1-$ZPOS1)
(( ZPOS1 = XPOS1 + 1 ))
k3=$(echo $k1 | cut -b$ZPOS1-)
echo "${k2}${VAL1}${k3}" | sed 's/#/ /g' >> $outfile
fi
else
head -$NBC $infile | tail -1 >> $outfile
fi
done
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|