awk comparison and substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk comparison and substitution
# 8  
Old 11-16-2011
Post the ex. file (content and name ) that generates the wrong result.
# 9  
Old 11-16-2011
ok, so here are all my files:

FileA's:
1ABC:
Code:
3.75289
3.74839
3.74117

2DEF:
Code:
3.45011
3.44657
3.46905

3GHI:
Code:
3.27445
3.27389
3.30938

etc. etc.

FileB:
Code:
0.0154    0.0139    0.0227
0.0198    0.0259    0.0231
0.0126    0.0216    0.0174
0.0115    0.0145    0.0237
0.0146    0.0124    0.0149
0.0128    0.0142    0.0161
...

Code:
# cat ren.sh             
#!/usr/bin/ksh

echo "Please choose cutoff value", read cut

FileB="${1}"
patFileA="${2}"


awk -v val=cut '
NR==FNR{o[NR""1]=$1
        o[NR""2]=$2
        o[NR""3]=$3
        next}
{
if ( FILENAME  != lFN ) 
   extF=substr(FILENAME,match(FILENAME,/[0-9]/))
val+=0
cmp=$1+0
if ( val <= cmp ) 
   print 
else 
   print o[extF""FNR] 
lFN=FILENAME}' ${FileB} ${patFileA}*

sorry to annoy you that much,
thanks so far
# 10  
Old 11-16-2011
Ok , you have to adjust the regex to match the file pattern.
Code:
# ls [0-9]*[A-Z]*            
1ABC  2DEF  3GHI

Code:
#!/usr/bin/ksh

echo "Please choose cutoff value: \c"
read cut

FileB="${1}"
patFileA="${2}"


awk -v val="${cut}" '
NR==FNR{o[NR""1]=$1
        o[NR""2]=$2
        o[NR""3]=$3
        next}
{
if ( FILENAME  != lFN ) 
   extF=substr(FILENAME,match(FILENAME,/^[0-9]*/),RLENGTH)
val+=0
cmp=$1+0
if ( val <= cmp ) 
   print 
else 
   print o[extF""FNR] 
lFN=FILENAME}' ${FileB} ${patFileA}*

Code:
# ren.sh FileB "[0-9]*[A-Z]*"
Please choose cutoff value: 3.45
3.75289
3.74839
3.74117
3.45011
0.0259
3.46905
0.0126
0.0216
0.0174

For an individual file:
Code:
# ren.sh FileB 1ABC          
Please choose cutoff value: 3.75
3.75289
0.0139
0.0227

This User Gave Thanks to Klashxx For This Post:
# 11  
Old 11-17-2011
Hi Klashxx,
I applied exactly your code, it just didn't work out for me. I still got some incorrect output with a lot of empty lines. It's ok though, I solved it with another (more artless) bash-script.
Thanks anyway!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File comparison using awk

Hi All, i have two files file1 ,file 2 file 1 col1|col2|col3|col4|col5|col6|col7|col8 11346925|0|2009-09-20|9999-12-31|100|0 11346925|0|2009-09-20|9999-12-31|120|0 12954311|0|2009-09-11|9999-12-31|100|0 12954311|0|2009-07-23|2999-12-31|120|0 12954312|0|2009-09-11|9999-12-31|100|0... (9 Replies)
Discussion started by: mohanalakshmi
9 Replies

2. Shell Programming and Scripting

awk substitution

Hi all, I need some help with substitution in awk. Is it possible to substitute field from awk output with string from file? For example: zcat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz | sed 's/:/;/g' | awk -F";" '{if($2==1 && $10~/389123456789/) print $36";"$37}' 2;19733248 I want... (6 Replies)
Discussion started by: vasil
6 Replies

3. Shell Programming and Scripting

awk comparison not working

Can you please help me on belw awk comparsion which doest not work cat employee_list NAME Last-login Jack 03/25/2013 Maneul 03/26/2013 Eric 03/26/2013 Samuel 03/28/2013 loak 03/29/2013 zac 03/29/2013 this is my awk .. it gives me error cat employee_list | awk '(($2=='date... (3 Replies)
Discussion started by: Sara_84
3 Replies

4. Shell Programming and Scripting

Need help with simple comparison in AWK

Hi, I'm new to AWK and I'm having problems comparing a field to a string variable. /ARTIST/ {x = $2} $1 ~ x {print $0}My code tries to find a record with the string "ARTIST". Once it finds it, it stores the second field of the record into a variable. I don't know what the problem is for the... (7 Replies)
Discussion started by: klusps
7 Replies

5. Shell Programming and Scripting

AWK string comparison

Hey guys.. New in linux scripting and need some help on some scripting with history command. I managed to export the command history into a file and now i'm trying to select from that file some specific commands that were made in a certain period. Here's what i got so far echo -n... (2 Replies)
Discussion started by: mishu_cgm
2 Replies

6. Shell Programming and Scripting

awk comparison

Hello all, Probably a very simple question, I am stuck with a small part of a code: I am trying to do a comparison to get the maximum value of column 6 if columns 1, 4 and 5 of two or more rows match. Here is what I am doing: awk -F'\t' '{if ($6 > a)a=$6}END{for (i in a) print i"\t"a}' ... (4 Replies)
Discussion started by: jaysean
4 Replies

7. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

8. Shell Programming and Scripting

Substitution in AWK

I am trying to use AWK to replace dallinux02 to dallinux03 everywhere in the servers.txt file and move it over to "awk2". Here is my script "awk2.awk": gsub(/dallinux02/, "dallinux03"); print > "awk2" I am trying to run this using the following: $ awk -f awk2.awk... (3 Replies)
Discussion started by: ora_umair
3 Replies

9. Shell Programming and Scripting

help with awk substitution

Hi again. A have a CSV-file in the following format: 2008.09.01,15:17:42,9227096485,9233175320,CTC10,SMS,0901151742098314,Target_MSIS DN_is_blacklisted I want to have an awk command that will say: If the first 3 digits of $4 does not begin with 922 or 923, then make $8 say "Invalid... (3 Replies)
Discussion started by: daytripper1021
3 Replies

10. Shell Programming and Scripting

AWK substitution

I need to copy field 2 to field 3 for only those records that have the 1st field equal to account e.g. file account|123|789|xxx|yyy|zzz|... account_group|444|555|xxx|yy|zz|.... account|456|901|aaa|bbb|ccc|..... after running awk script should look like account|123|123|xxx|yyy|zzz|...... (4 Replies)
Discussion started by: klut
4 Replies
Login or Register to Ask a Question