The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn shell and awk question mastachef Shell Programming and Scripting 6 10-31-2007 03:15 AM
korn shell question mich_elle Shell Programming and Scripting 4 02-22-2006 05:03 PM
Korn Shell Coprocess Performance Question Mark Puddephat Shell Programming and Scripting 8 12-14-2005 01:33 PM
Korn Shell Loop question stevefox Shell Programming and Scripting 4 12-08-2005 09:27 PM
Question about Korn Shell Latha Nair Shell Programming and Scripting 13 11-05-2003 03:30 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-03-2005
Registered User
 

Join Date: Feb 2005
Posts: 48
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-03-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
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.
Reply With Quote
  #3 (permalink)  
Old 02-03-2005
Registered User
 

Join Date: Feb 2005
Posts: 48
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
Reply With Quote
  #4 (permalink)  
Old 02-03-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
How big are these files? You can only have 1k elements in an array. Would that be enough?
Reply With Quote
  #5 (permalink)  
Old 02-03-2005
Registered User
 

Join Date: Feb 2005
Posts: 48
more than enough
Reply With Quote
  #6 (permalink)  
Old 02-03-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
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
Reply With Quote
  #7 (permalink)  
Old 02-03-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,226
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.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0