awk substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk substitution
# 1  
Old 10-01-2013
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:
Code:
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 to substitute 2;19733248 with GSM;GSM_ABS_SUB_HLR from errors.txt file.
errors.txt
Code:
.................................................................
GSM;33555968;GSM_ABNORMAL_DLG
GSM;19733249;GSM_ABS_SUB_DETACHED_HLR
GSM;19667713;GSM_ABS_SUB_DETACHED_MSC
GSM;19733248;GSM_ABS_SUB_HLR
GSM;19667712;GSM_ABS_SUB_MSC
GSM;19733250;GSM_ABS_SUB_PAGEFAIL_HLR
GSM;19667714;GSM_ABS_SUB_PAGEFAIL_MSC
GSM;19727879;GSM_ABS_SUB_SM_DEREG_4_GPRS_HLR
GSM;19662343;GSM_ABS_SUB_SM_DEREG_4_GPRS_MSC
GSM;19727875;GSM_ABS_SUB_SM_DEREG_4_NON_GPRS_HLR
............................................................

Thank you in advance.

Last edited by vasil; 10-01-2013 at 05:11 AM..
# 2  
Old 10-01-2013
Code:
zcat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz | sed 's/:/;/g' | awk -F";" '{if($2==1 && $10~/389123456789/) sub (2,"GSM",$36); sub(19733248,"GSM_ABS_SUB_HLR",$37); print $36";"$37}'

# 3  
Old 10-01-2013
Code:
sed 's/:/;/g' | awk -F";"

can be simplified to
Code:
awk -F ':|;'

(As usual, use nawk vs awk with Solaris).
# 4  
Old 10-01-2013
Thanks pravin27 for the prompt answer, but in this case the conditional {if($2==1 && $10~/389123456789/); doesn't work.
Code:
zcat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz | sed 's/:/;/g' | awk -F";" '{if($2==1 && $10~/389123456789/); sub(2,"GSM",$36); sub(19733248,"GSM_ABS_SUB_HLR",$37); print $36";"$37}' 

    0;0
    0;0
    0;0
    0;0
    0;0
    GSM;GSM_ABS_SUB_HLR
    0;0
    0;0
    0;0

Because errors.txt is too long, is there options to calling external script or command (grep for example) inside awk in action part to get values from errors.txt?
I read about the system() function in awk but it isn't clear for me enough.

@jlliagre: Thank you. I'll keep that in mind.
# 5  
Old 10-01-2013
Not clear with your requirement. Please provide some part of your input file and desire output file.
# 6  
Old 10-01-2013
Code:
zcat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz|
sed 's/:/;/g' |awk -F";" '
(NR==FNR) {A[$2]=$1 FS $3; next}
($2==1 && $10~/389123456789/) {print $36,$37,A[$37]}' errors.txt -

# 7  
Old 10-01-2013
Code:
zcat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz |  awk -F':|;' '{if($2==1 && $10~/389123456789/) print}' 

13E075CB2;    1;    2;    8;    none:none;       389123456789:1:1;       389123456789:1:1;    000000000000000;       389123456789:1:1;    284010160340741;       38988000014;    20130925075430;    20130927095618;    8;       0
;      6;      000000000000;      0;     0;    11e6b1e22;       389123456789:1:1;    284010160340741;    0:0;        35988000301;        35988000014;    2:19733248;

What i want is:
Code:
13E075CB2;    1;    2;    8;    none:none;       389123456789:1:1;        389123456789:1:1;    000000000000000;       389123456789:1:1;     284010160340741;       38988000014;    20130925075430;     20130927095618;    8;       0
;      6;      000000000000;      0;      0;    11e6b1e22;       389123456789:1:1;    284010160340741;     0:0;        35988000301;        35988000014;    GSM:GSM_ABS_SUB_HLR;

---------- Post updated at 05:03 AM ---------- Previous update was at 03:56 AM ----------

Thanks MadeInGermany,

I've tested your suggestion
Code:
cat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz|sed 's/:/;/g' |awk -F";" '(NR==FNR) {A[$2]=$1 FS $3; next}($2==1 && $10~/389123456789/) {print $0"|"A[$37]}' /tmp/errors.txt -

13E075CB2;    1;    2;    8;    none;none;       389123456789;1;1;       389123456789;1;1;    000000000000000;       389123456789;1;1;    284010160340741;       389123456789;    20130925075430;    20130927095618;
    8;       0;      6;      000000000000;      0;     0;    11e6b1e22;       389123456789;1;1;    284010160340741;    0;0;        35988000301;        35988000014;    2;19733248;|GSM;GSM_ABS_SUB_HLR

This is not exactly substitution but i think it is better to have number and explanation of the error. Thank you again.

---------- Post updated at 07:14 AM ---------- Previous update was at 05:03 AM ----------

Hi again,

In the example in previous post how can i improve the output if i have different type of errors with the same value.
For example:
Code:
zcat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz|sed 's/:/;/g' |awk -F";" '{if(($36==14 || $36==21)  && $37==1) print $36";"$37}'

    14;1
    14;1
    21;1
    14;1
    14;1
    14;1
    21;1

errors.txt (Type;NumericValue;Definition)
Code:
CRB_INFRASTRUCTURE;1;CRB_INFRASTRUCTURE_NO_ROUTE
CRB_INFRASTRUCTURE;2;CRB_INFRASTRUCTURE_EP_UNAVAIL
CODIAC;1;CODIAC_SUB_PREPAID_SKINT
CODIAC;7;CODIAC_UNKNOWN_RESULT

In the other words how to check $36(type) first and then $37(value) from errors.txt?
It is not problem to change errors.txt like this:
Code:
14;1;CRB_INFRASTRUCTURE_NO_ROUTE
14;2;CRB_INFRASTRUCTURE_EP_UNAVAIL
21;1;CODIAC_SUB_PREPAID_SKINT
21;7;CODIAC_UNKNOWN_RESULT

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Does awk have parameter substitution?

Can I specify a default value to a variable in AWK like BASH in one statement using parameter substitution? BASH example: argument=${$1-"default if empty"} (BASH) I know I can do: argument=$1; sub ( "^$", "default if empty", argument) (AWK) Mike (13 Replies)
Discussion started by: Michael Stora
13 Replies

2. Shell Programming and Scripting

awk comparison and substitution

Hi, here's my - not so easy to describe - problem: I want to compare the values of one file (FileA) with a cutoff-value and, if this comparison is true, substitute those values with those in the second file (FileB). However, there are many FileA's (FileA), whereas there is only one FileB. Every... (10 Replies)
Discussion started by: waddle
10 Replies

3. Shell Programming and Scripting

Variable substitution in awk

Hi, I have a variable to be substituted in awk. I am using AIX 5.3. Here is my piece of code: REPL_DT=`date +'%Y\\\\\\\\\/%m\\\\\\\\\/%d'` NEW_LINE=$(echo $Line | awk '{sub ($4, '$REPL_DT'); printf "# %-7s %9s %18s\n", $2,$3,$4}') sed $n" s/.*/$NEW_LINE/" kfile > tmp mv tmp kfile Here,... (2 Replies)
Discussion started by: sugan
2 Replies

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

5. Shell Programming and Scripting

Substitution using awk/gawk

Hello, I have a file containing lines such as: (1 104 (16) (17) (18) (102))$ (1 105 (16) (17) (19:21) (102))$ I would like to extract the numbers, only by using awk (or gawk). I do not want to use "sed" as it is very slow. For now my solution consists in... (2 Replies)
Discussion started by: jolecanard
2 Replies

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

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

8. Shell Programming and Scripting

Substitution of char with AWK

I created a file contains: create table .....; create index ....; create trigger...; and I want to substitue from my file ; by ;-- from the line where appears create trigger to the end of the file and keep intact the create table step and index in the file. awk '/CREATE... (2 Replies)
Discussion started by: mario dionne
2 Replies

9. Shell Programming and Scripting

Filed substitution with awk

guys, I'm trying to 9k lines of the following: aaa aaa 1 1 1 to aaa aaa 1 01 1 Im pretty ignorant when it comes to subtituting fields using awk any help ? Tony (1 Reply)
Discussion started by: tony3101
1 Replies

10. UNIX for Dummies Questions & Answers

awk variable substitution

for the command below, it looks for the 3rd field value matching "P" and printing it. awk '{if ($3 == "P") print}' file how would i express this if i use a loop to find more that 1 variable fro a list? this doesn't seem to work... cat list | while read n do awk '{if ($3 == "$n") print}'... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question