AWK question in the KORN shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK question in the KORN shell
# 1  
Old 02-03-2005
AWK question in the KORN shell

Hi,

I have two files with the following content:

gmrd.txt
235649;03;2563;598
291802;00;2563;598
314634;00;235649;598
235649;03;2563;598
393692;00;2563;598
411805;00;2563;598
411805;00;2563;598
235649;03;2563;598
414037;00;2563;598
575200;00;2563;598
70710;00;2563;598
70710;00;2563;598
235649;03;2563;598
70710;00;2563;598
808932;00;2563;5980
903857;00;2563;5980
979217;00;2563;598
235649;03;2563;598
A0ABVB;00;2563;598
235649;03;2563;598


and val_id.txt
235649;05;2563;598
235649;05;2563;598
564564;05;2563;598
235649;05;2563;598
235649;05;2563;598
212564;05;2563;598


(thesse are small samples of the actual files)

What I need to do is to use awk to get the first column of val_id.txt and then search gmrd.txt for any records with instances of the value occuring in the first column of vali_id.txt - if it finds any then it needs to replace the second column of that record by 05

Any help appreciated

I get the following error message:

./split.sh[2]: 235649;05;2563;598: syntax error

with the following code:

for i in $(< val_id.txt );do
index[i]="$i"
export index

awk 'BEGIN { FS = ";"; OFS = ";" }
{
if ($1 == "${index[i]")
$2 = "05"
print $0 ;
}' gmrd.txt
done
# 2  
Old 02-03-2005
Quote:
Originally Posted by penfold

I get the following error message:

./split.sh[2]: 235649;05;2563;598: syntax error

with the following code:

for i in $(< val_id.txt );do
index[i]="$i"
export index

awk 'BEGIN { FS = ";"; OFS = ";" }
{
if ($1 == "${index[i]")
$2 = "05"
print $0 ;
}' gmrd.txt
done
When you do "for i in $(< val_id.txt );do", i will be set to one line of input after another. For example, "235649;05;2563;598". This is not a valid subscript, yet the next line, "index[i]=$i" tries to use i as an index. Even if that had worked, you cannot usefully export an array. You should probably rewrite this to use just ksh or just awk. It's not very clear what you really want to do, so I can't really suggest specific code.
# 3  
Old 02-03-2005
Thank you for your response -

What I am trying to do is to get look in the file val_id.txt and take those values in there - if I find instances of those values occuring in gmrd.txt then I want to replace the second column of values with something else say '05'

Adam
# 4  
Old 02-03-2005
How big are these files? You can only have 1k elements in an array. Would that be enough?
# 5  
Old 02-03-2005
more than enough
# 6  
Old 02-03-2005
Code:
#! /usr/bin/ksh

exec < val_id.txt
i=0
while IFS=";" read f1 f2 f3 f4 ; do
        index[i]="$f1"
        ((i=i+1))
done
i=0
while ((i<${#index})) ; do
        echo ${index[i]}
        ((i=i+1))
done

exec < gmrd.txt
while IFS=";" read f1 f2 f3 f4 ; do
        found=0
        i=0
        while ((i<${#index})) ; do
                [[ $f1 = ${index[i]} ]] && found=1
                ((i=i+1))
        done
        if ((found)) ; then
                f2="xyz"
        fi
        echo "${f1};${f2};${f3};${f4}"
done
exit 0

# 7  
Old 02-04-2005
Using awk...
Code:
awk '
    BEGIN {
        FS = OFS = ";"
        while (getline < "val_id.txt" > 0)
            arr[$1] = 1
    }
    $1 in arr {
        $2 = 50
        $NF =  $NF "<---debug: line changed"
    }
    {print}
' gmrd.txt

Tested...
Code:
235649;50;2563;598<---debug: line changed
291802;00;2563;598
314634;00;235649;598
235649;50;2563;598<---debug: line changed
393692;00;2563;598
411805;00;2563;598
411805;00;2563;598
235649;50;2563;598<---debug: line changed
414037;00;2563;598
etc.

Remove the debug line if this is what you wanted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

korn shell remove files question

how do you show each filename in a giving directory and delete the specific file in korn script i was thinking using ls rm ? but i cant make it work (0 Replies)
Discussion started by: babuda0059
0 Replies

2. AIX

AIX 4.2 Korn shell and grep question

Ho do I find out the verion of the Kron shell on my client`s system ? There is no one to ask. They are not knowledged enough (hard to believe but yes). Also, on that AIX 4.2, I am trying to figure out how to do a grep using a search patter like below but does not seam to work. The '*' do... (11 Replies)
Discussion started by: Browser_ice
11 Replies

3. UNIX for Dummies Questions & Answers

Korn shell awk use for updating two files

Hi, I have two text files containing records in following format: file1 format is: name1 age1 nickname1 path1 name2 age2 nickname2 path2 file 1 example is: abcd 13 abcd.13 /home/temp/abcd.13 efgh 15 efgh.15 /home/temp/new/efgh.15 (4 Replies)
Discussion started by: alrinno
4 Replies

4. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

5. Shell Programming and Scripting

substr() thru awk Korn Shell Script

Hi, I am new stuff to learn substr() function through awk for writing the Korn shell script. Is there a way to copy from XXXX1234.ABCDEF to XXX1234 file names without changing both data files? I appreciate your time to response this email. Thanks, Steve (4 Replies)
Discussion started by: sbryant
4 Replies

6. Shell Programming and Scripting

Korn shell and awk question

I am modifying a Korn shell script in using the Exceed (Solaris 10 environment). My task is to read in a .txt file with dates arranged like this (01-Sep-2006). I am to read each line and take the dates, compare them to a benchmark date and depending on if it is older than the date or the date and... (6 Replies)
Discussion started by: mastachef
6 Replies

7. Shell Programming and Scripting

korn shell question

Hi all, I am trying to tweak my ksh , i am running V: Version M-11/16/88i I have my Backspace and up/down arrows working using the following code in my ~/.profile file. set -o emacs alias __A=$(print '\020' ) alias __B=$(print '\016' ) alias __C=$(print '\006' ) alias __D=$(print... (4 Replies)
Discussion started by: mich_elle
4 Replies

8. Shell Programming and Scripting

Korn Shell Coprocess Performance Question

I am wracking my brains over this. I am trying to use a Korn Shell script to execute an Oracle PL/SQL procedure, using the Oracle command line interface (sqlplus). The script starts sqlplus in a coprocess, and the two processes communicate using a two-way pipe. The bgnice option is off, so both... (8 Replies)
Discussion started by: Mark Puddephat
8 Replies

9. Shell Programming and Scripting

Korn Shell Loop question

I'm needing help with assigning variables inside a while loop of ksh script. I have an input text file and ksh script below and I'm trying to create a script which will read the input file line by line, assign first and second word to variables and process the variables according to the contents. ... (4 Replies)
Discussion started by: stevefox
4 Replies

10. Shell Programming and Scripting

Question about Korn Shell

In Korn Shell, can you use "go to" statements? Would you then put paragraph names with a colon? For example, would you specify "goto para1" and then have the paragraph with the label para1:? I am getting an error message when Idid this. I have my paragraph name 'clsbooks:' and I get... (13 Replies)
Discussion started by: Latha Nair
13 Replies
Login or Register to Ask a Question