awk and gsub - how to replace only the first X occurrences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk and gsub - how to replace only the first X occurrences
# 8  
Old 09-24-2010
As I said, the file is very large, so I would stop the search soon after having found the first X occurrences. So, do you think that if I use the sub function instead of gsub, search will be faster? :

Code:
cat text.txt |
awk '{
  if ( count < 2 ){
    sub("CAR","REPLACE")
    count++
    }
  print
  }'



---------- Post updated at 05:17 PM ---------- Previous update was at 05:13 PM ----------

What do you think about this?

Code:
cat text.txt | awk '{if ( count < 3 ){ sub("CAR","REPLACE")} count++; print}'

# 9  
Old 09-24-2010
Hi.

sub replaces the first occurrence on a line, gsub replaces all occurrence on a line - not all occurrences in a file.

Code:
awk '/CAR/ {
  if ( count++ < 2 )
    gsub("CAR","REPLACE")
  }1' file

# 10  
Old 09-24-2010
if you want to exit from the code after X occurrence , you will get result till that record.
Code:
 awk '/CAR/ && count < 2  {gsub("CAR","REPLACE");count++} {print $0;if(count == 2) {exit}}'  infile

O/P
Code:
REPLACE sweet head
hat red yellow
REPLACE book brown

# 11  
Old 09-24-2010
Quote:
Originally Posted by bingel
I have a text (text.txt) and I would like to replace only the first 2 occurrences of a word
I havn't test the ruby version but all the solution above fail if you want to replace 2 occurence with a file like this:
Code:
CAR sweet CAR woman CAR
CAR red yellow
CAR book brown
tiger CAR cow CAR
CAR milk

Try something like this:
Code:
awk '{while(index($0,"CAR") && ++n < 3){sub("CAR","REPLACE")}}1' file

# 12  
Old 09-24-2010
Quote:
Originally Posted by Franklin52
I havn't test the ruby version
Yes, it does produce the correct output, since its splitting on a limit. However, need to change regex to \bCAR\b if boundary is required. another awk way assuming CAR is not bounded
Code:
 
$ awk 'BEGIN{RS="CAR";ORS="REPLACE"}NR>2{ORS="CAR"}1' file
REPLACE sweet REPLACE woman CAR
CAR red yellow
CAR book brown
tiger CAR cow CAR
CAR milk


Last edited by kurumi; 09-24-2010 at 10:32 PM..
# 13  
Old 09-24-2010
Yet another way of doing the same...
Code:
awk '{
  if (cnt < 2)
    for (i=1;i<=NF;i++)
      if ($i=="CAR" && cnt<2)
        cnt += sub("CAR","REPLACE",$i)
   print
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk gsub command to replace multiple spaces

Hi Forum. I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful. Original Data: 4365 monte des source rue,, ,<TAB>trevost<TAB>QC Desired Data:... (1 Reply)
Discussion started by: pchang
1 Replies

2. Shell Programming and Scripting

Replace characters in string with awk gsub

Hi I have a source file that looks like a,b,c,d,e,f,g,h,t,DISTI(USD),MSRP(USD),DIST(EUR),MSRP(EUR),EMEA-DISTI(USD),EMEA-MSRP(USD),GLOBAl-DISTI(USD),GLOBAL-MSRP(USD),DISTI(GBP), MSRP(GBP) I want to basically change MSRP(USD) to MSRP,USD and DIST(EUR) to DIST,EUR and likewise for all i'm using... (3 Replies)
Discussion started by: r_t_1601
3 Replies

3. Shell Programming and Scripting

Search & Replace content of files using gsub in awk

Hi,I have 2 files master.txt & reference.txt as shown below & i require o/p as mentioned in file 3 using awk but content is not replacing properlymaster.txt:... (15 Replies)
Discussion started by: siramitsharma
15 Replies

4. Shell Programming and Scripting

Search & replace content using awk/gsub

Hi, I have two files master.txt & reference.txt. Sample below Master.txt 2372,MTS,AP 919848001104,Airtel,DL 0819,MTS,MUM 919849788001,Airtel,AP 1430,Aircel MP,20 Reference.txt 2372,919848701430,46467 919848002372,2372,47195 2372,919849788001,59027 0819,028803,1 0819,029801,1... (2 Replies)
Discussion started by: siramitsharma
2 Replies

5. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

6. Shell Programming and Scripting

Using of gsub function in AWK to replace space by underscore

I must design a UNIX script to monitor files whose size is over a threshold of 5 MB in a specific UNIX directory I meet a problem during the for loop in my script. Some file names contain spaces. ls -lrt | awk '$5>=5000000 && length($8)==5 {gsub(/ /,"_",$9); print};' -rw-r--r-- 1 was61 ... (2 Replies)
Discussion started by: Scofield38
2 Replies

7. UNIX for Dummies Questions & Answers

Replace all occurrences of strings with parentheses

Hi, I tried to adapt bartus's solution to my problem, without success. I want to replace all the occurences of this: with: , where something can contain an arbitrary number of balanced parens and brakets. Any ideas ? Best, (1 Reply)
Discussion started by: ff1969ff1969
1 Replies

8. Shell Programming and Scripting

sed replace multiple occurrences on the same line, but not all

Hi there! I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance: I have a command which the output will be: 200.300.400.5 0A 0B 0C 01 02 03 being that the last 6 strings are actually one... (7 Replies)
Discussion started by: ppucci
7 Replies

9. Shell Programming and Scripting

awk gsub

Hi all I want to do a simple substitution in awk but I am getting unexpected output. My function accepts a time and then prints out a validation message if the time is valid. However some times may include a : and i want to strip this out if it exists before i get to the validation. I have shown... (4 Replies)
Discussion started by: pxy2d1
4 Replies

10. Shell Programming and Scripting

Help with AWK and gsub

Hello, I have a variable that displays the following results from a JVM.... 1602100K->1578435K I would like to collect the value of 1578435 which is the value after a garbage collection. I've tried the following command but it looks like I can't get the > to work. Any suggestions as... (4 Replies)
Discussion started by: npolite
4 Replies
Login or Register to Ask a Question