Sponsored Content
Top Forums Shell Programming and Scripting How to insert data befor some field in a row of data depending up on values in row Post 302298695 by Goldorakk on Wednesday 18th of March 2009 10:20:34 AM
Old 03-18-2009
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

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
MAKE_SOCKDFC(8) 					      System Manager's Manual						   MAKE_SOCKDFC(8)

NAME
make_sockdfc - Generates frozen configuratyion file for SOCKS server SYNOPSIS
make_sockdfc [infile [outfile] ] DESCRIPTION
make_sockdfc reads in a plain-text configuration file for the SOCKS server and produces a frozen configuration file as the output. Both arguments are optional. The default for infile is /etc/sockd.conf; the default for outfile is /etc/sockd.fc. You may specify infile while omitting outfile, but you cannot specify outfile without also speficying infile. The contents of the frozen configuration file is essentially the memory image of the parsed input file. Using the frozen configuration file can reduce the start-up delay of the SOCKS server program since it no longer has to parse the file contents. When the SOCKS server starts, it always looks for the frozen configuration file /etc/sockd.fc first. If that file is not found, it then tries to use the plain-text configuration file /etc/sockd.conf. If you use frozen configuration, you must remember to run make_sockdfc every time after you modify the plain-text file or the SOCKS server will continue to use the frozen file of a previous configuration. To find out the contents of a frozen configuration file, use dump_sockdfc. FILES
/etc/sockd.fc, /etc/sockd.conf SEE ALSO
dump_sockdfc(8), sockd.conf(5), sockd.fc(5) AUTHOR
Ying-Da Lee, yingda@best.com or yingda@esd.sgi.com May 6, 1996 MAKE_SOCKDFC(8)
All times are GMT -4. The time now is 01:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy