How to fix this awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to fix this awk
# 1  
Old 06-17-2010
How to fix this awk

I have a script which will mask the 9th and 15th column in a record starting with BPR.

The record looks like below before my script
Code:
BPR*C*160860.04*C*ACH*CTX*01*072000326*DA*1548843*3006968523**01*071000013*DA*5529085*100323*VEN

The record will be masked after my script parses this record
Code:
BPR*C*160860.04*C*ACH*CTX*01*072000326*DA*XXX8843*3006968523**01*071000013*DA*XXX9085*100323*VEN*

I do not want the '*' character at the end of the record at any point.
Below is my script
Code:
function chgrec(rec){
  debug=1
  z=split(rec,rarray,"*")
  element=10
  unChanged = rarray[element]
  lUnChg = length(unChanged)-3
  lastFour = substr(unChanged,lUnChg)
  initValue = rarray[element]
  gsub(/[0-9A-Za-z]/,"X",initValue)
  preFix = substr(initValue,1,lUnChg-1)
  element2=16
  unChanged2 = rarray[element2]
  lUnChg2 = length(unChanged2)-3
  lastFour2 = substr(unChanged2,lUnChg2)
  initValue2 = rarray[element2]
  gsub(/[0-9A-Za-z]/,"X",initValue2)
  preFix2 = substr(initValue2,1,lUnChg2-1)
  rec=""
  for (i=1;i<=z;i++){
#    printf("-->1:%s-->2:%s\n", i, rarray[i])> "/tmp/fftt";
     if (debug=0)
       {
        if (i==element)
          rec=rec sprintf("-->%s%s<--(%s)*", preFix, lastFour,rarray[element])
        else
        if (i==element2)
          rec=rec sprintf("-->%s%s<--(%s)*", preFix2, lastFour2,rarray[element2])
        else
            if (i==z)
            rec=rec sprintf("%s*", rarray[i])
       }
     else
       {
        if (i==element)
          rec=rec sprintf("%s%s*", preFix, lastFour,rarray[element])
        else
        if (i==element2)
          rec=rec sprintf("%s%s*", preFix2, lastFour2,rarray[element2])
        else
          rec=rec sprintf("%s*", rarray[i])
       }
  }

# 2  
Old 06-17-2010
Something like this?
Code:
awk -F\* '/^BPR/{$10="XXX" substr($10,4);$16="XXX" substr($16,4)}1' OFS="*" file

# 3  
Old 06-17-2010
Is there a way where you can find the problem in my script.
And also I should show only the last 4 characters in the 9th column and I should the mask the rest. You code works good but it masks only the 1st the 3 characters.
# 4  
Old 06-17-2010
One way:
Code:
awk -F\* 'BEGIN{X="XXXXXXXXXX"}
/^BPR/{
  n=length($10)-4
  $10=substr(X,1,n) substr($10,n+1)
  n=length($16)-4
  $16=substr(X,1,n) substr($16,n+1)
}1' OFS="*" file

# 5  
Old 06-22-2010
Thanks a lot Frank.. It worked.. I wanted to include this in my script and I did as shown below. But I get the syntax error as shown below. Any advise.

Below is the script awkMask
Code:
/./{
  type=substr($0,1,3)
  rec = $0
awk -F\* 'BEGIN{X="XXXXXXXXXX"}
/^BPR/{
n=length($10)-4
$10=substr(X,1,n) substr($10,n+1)
n=length($16)-4
$16=substr(X,1,n) substr($16,n+1)
}1' OFS="*" rec > output
}

I am trying to call this from the other script as shown below,
Code:
awk -f $base/scripts/awkMask $InputFile

I get the below error in my log file

Code:
  7834   syntax error The source line is 58.
  7835   The error context is
  7836          awk >>>  -F\ <<< * 'BEGIN{X="XXXXXXXXXX"}
  7837   awk: The statement cannot be correctly parsed.
  7838   The source line is 58.
  7839   syntax error The source line is 63.

# 6  
Old 06-22-2010
Is this a snippet of an awk program?
Code:
/./{
  type=substr($0,1,3)
  rec = $0

You can't include the code in another awk program this way.
# 7  
Old 06-22-2010
What would be the ideal way to have this in my script..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk file subtracted by a fix value - conditioned

Hi all... i have been trying to make this work but I have been failing for 6 hours .. I know it should be something simple that I am missing to it would be great if you can help me ... I want to subtract a fixed value (lets set 1) from any value >=1 from the whole file my file looks like ... (4 Replies)
Discussion started by: A-V
4 Replies

2. Shell Programming and Scripting

awk if condition match and fix print decimal place

Hi All, I have problem in the middle of implementing to users, whereby the complaint is all about the decimal place which is too long. I need two decimal places only, but the outcome from command is always fixed to 6. See the sample : before: Sort Total Site Sort SortName Parts ... (3 Replies)
Discussion started by: horsepower
3 Replies

3. Shell Programming and Scripting

Help me please to fix my error

Can you help me to fix my error please?! I checked the code and I think there is no mistake, but when I run it gives me error such line 1: /Users/Manu/trials/hosts: is a directory sort: open failed: /Users/Manu/trials/hosts/*: No such file or directory Help me please... mycode NAME value.... (9 Replies)
Discussion started by: Manu1234567
9 Replies

4. UNIX for Dummies Questions & Answers

Please help to fix awk script

Good morning, fellows. I would need to ask for your help in editing my awk script. Here is the original version: BEGIN { printf ("CRYST1 200.000 200.000 200.000 90.00 90.00 90.00 P 1 1\n") maxatoms=1000 natom=0 found_struct = 0 found_bond = 0 } { if( NF == 5 ) { foundff=0 natom++... (9 Replies)
Discussion started by: snysmumrik
9 Replies

5. Shell Programming and Scripting

Fix timestamp with Sed or Awk

Hi I am dealing with the following string: Date: Thur, 13 March 2011 01:01:10 +0000 I asked for help in another topic that converted a similar string: Date: Thur, 13 March 2011 9:50 AM To a 24 hr standard. The problem is that it comes out as: Date: Thur, 13 March 2011 9:50:00 +0000... (4 Replies)
Discussion started by: duonut
4 Replies

6. Shell Programming and Scripting

how to fix the column length in a file using Awk Prog

Hi I use the following code to read the file and to fix the length of the column of the record in the file 'Sample.txt' ls Samp* | awk ' { a=$1 } END{ FS="n" for(i=1;i<=NR;i++) { while( getline < a ) { f1=$0; print("Line::",f1); f2=substr(f1,1,10) print("Field1::",f2);... (10 Replies)
Discussion started by: meva
10 Replies

7. Shell Programming and Scripting

how to fix this awk script?

i have a log file while looks like this ++ user_a blabla blabla nas_b blabla user_d this is a user_a junk line another junk line user_c nas_m blabla ++ basically most of the lines contain a "user" keywords, and the rest of the lines do not have "user" at all. So I have the... (17 Replies)
Discussion started by: fedora
17 Replies

8. Solaris

ZFS Help!!! Can I fix this??

Well I got ZFS going almost in version one and didn't have the money to raid it or mirror it. I run off of 7 - 750GB-1TB USB drives and it's run great for over a year. Recently I needed to power off and move some stuff around and when I powered it back on I got this:... (3 Replies)
Discussion started by: lacakid
3 Replies

9. Shell Programming and Scripting

AWK record length fix

Hi Friends, Need some help in AWK. Working on AIX 5 Have been trying the following functionality to make the record length fixed: if( length(record) < 300 ) { printf("%-300s\n", record); } In my opinion it will apply some fillers in the end. Its is not making any... (4 Replies)
Discussion started by: kanu_pathak
4 Replies

10. Shell Programming and Scripting

awk / shell - Fix broken lines and data

Gurus, I am struggling with a issue and thought I could use some of your expertise. Need Help with this I have a flat file that has millions of records 24|john|account ~ info |56| 25|kuo|account ~ journal |58| 27|kim|account ~ journal |59| 28|San|account ~ journal |60|... (3 Replies)
Discussion started by: rimss
3 Replies
Login or Register to Ask a Question