replace in Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace in Awk
# 1  
Old 07-01-2011
Error replace in Awk

i have a file that has data like
template.txt
Code:
Version:V1.0
name:ACTION
Number Of Actions:1
A:ACC,EVE,P1,1,1,ACC,2,ACC,3,1,5,20090228 10070800,6,20130628 10070800


now i have to replace all occurrence of ACC with a value and occurences of EVE with a value.

my shell script is as below

Code:
$acc=abc
$var1=efg
cat template.txt |awk -v var="$acc" -v var1="$eve_source" ' BEGIN { FS=","; OFS="," }
{
sub(/ACC/, var);
sub (/EVE/,var1);
print $0
} ' > temp.txt

when using this script my temp.txt has this data

Code:
Version:V1.0
name:ACTION
Number Of Actions:1
A:abc,EVE,P1,1,1,ACC,2,ACC,3,1,5,20090228 10070800,6,20130628 10070800

i see only the first occurence of ACC is replaced and remaining "ACC",EVE are not being replaced...

Can anyone help me on this??
# 2  
Old 07-01-2011
Hi,

Try using function 'gsub(...)'. It works like 'sub(...)' but replaces all matches found in the line.

Regards,
Birei.
# 3  
Old 07-01-2011
use gsub instead of sub

The gsub() function is similar to the g option in sed: all occurrence are converted, and not just the first
# 4  
Old 07-01-2011
Have you set the variable $eve_source? Assuming you mean $var1 with the second awk variable:
Code:
$acc=abc
$var1=efg

awk -v var="$acc" -v var1="$var1" ' BEGIN { FS=","; OFS="," }
{
  gsub(/ACC/, var)
  gsub (/EVE/,var1)
  print $0
}' template.txt > temp.txt

BTW: The use of cat is redundant.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. UNIX for Beginners Questions & Answers

Replace using sed or awk

Hi, Need a help to replace a word if a pattern is found between the delimiters preferably using SED or AWK. below is the sample file that iam dealing with, need to match pattern 'application' if found replace the whole word between the delimiters and also print the lines that don't match.... (1 Reply)
Discussion started by: tech_frk
1 Replies

3. Shell Programming and Scripting

Find and Replace in awk

Friends, I have more the thousand lines like this. check.cloud1.port=342 check.cloud2.port=5456 check.cloud3.port-4564 But we need to transfer it to _CHECK.CLOUD1.PORT_=342 _CHECK.CLOUD2.PORT_=5456 _CHECK.CLOUD3.PORT_=4564 Any one could pls help of this. Thanks in Advance ... (1 Reply)
Discussion started by: jothi basu
1 Replies

4. Shell Programming and Scripting

Replace wc with awk

awk 'FNR<='${linenum}' && /'"${SEC1}"'/ && /'"${SEC2}"'/' | wc -l how can i use awk to count the lines instead of wc? (1 Reply)
Discussion started by: SkySmart
1 Replies

5. UNIX for Dummies Questions & Answers

Replace substrings in awk

Hi ! my input looks like that: --AAA-AAAAAAA---------AA- AAA------AAAAAAAAAAAAAA ------A----AAAA-----A------- Using awk, I would need to replace only the "-" located between the last letter and the end of the string by "~" in order to get: --AAA-AAAAAAA---------AA~... (7 Replies)
Discussion started by: beca123456
7 Replies

6. Shell Programming and Scripting

replace using awk

Hi, I am trying to replace 4th and 5th column data with the below code awk -F, '$1=="ABC"&&$2=="XYZ" {gsub(/ABC/, "TEST" ,$4,$5)}' infile.txt > 1.txt if the first column data is ABC and second column data is XYZ then replace ABC to test in 4th and 5th column, the above code is replacing the... (1 Reply)
Discussion started by: shruthidwh
1 Replies

7. Shell Programming and Scripting

awk incremental replace

Hello! I have the following lines in a file: 1|0|HEADER| 2|1|HEADER| 3|1|MAIN| 4|1|INFO| 5|1|INFO| 6|1|INFO| 7|2|INFO| 8|4|INFO| 9|55|FOOTER| 10|1|HEADER| 11|22|MAIN| 12|9|MAIN| and I want to convert it into: (7 Replies)
Discussion started by: TasosARISFC
7 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

Sed help to replace and then awk

Sed help echo "(200 rows affected)" | sed -e '/\(//p' | sed -e '/\)//p' | awk '{print $1}' I want output as "200" Please help me correct (2 Replies)
Discussion started by: pinnacle
2 Replies

10. Shell Programming and Scripting

Need to replace text in an awk

Hey guys: Running Solaris 5.5.1 and trying to write an easy /bin/sh script. Here is what I am doing... grep "Total" /workspace_4/local/reports/cust_calls_5ess.050510 | awk '{print $4 "\t" $7 $8 $9 $10 $11 $12 $13 $14}' It renders the following results... 315268 ... (2 Replies)
Discussion started by: cdunavent
2 Replies
Login or Register to Ask a Question