![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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 |
|
||||
|
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
|
|
||||
|
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
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. |
||||
| Google UNIX.COM |