Sponsored Content
Top Forums Shell Programming and Scripting awk and gsub - how to replace only the first X occurrences Post 302456526 by quirkasaurus on Friday 24th of September 2010 12:01:10 PM
Old 09-24-2010
oops. sorry. didn't notice that.

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

Code:
output:
REPLACE sweet head
hat red yellow
CAR book brown
tiger CAR cow CAR
CAR milk

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Cache::Ref(3pm) 					User Contributed Perl Documentation					   Cache::Ref(3pm)

NAME
Cache::Ref - Memory only cache of live references SYNOPSIS
# this class is just a base class and a documentation start point # just use the various algorithms directly use Cache::Ref::CART; my $cache = Cache::Ref::CART->new( size => 1024 ); # add a cache value or set an existing key to a new value $cache->set(foo => $some_object); # get a value $cache->get("foo"); # also takes a list of keys # remove a key before it has normally expired $cache->remove("foo"); # remove all cached data $cache->clear; # 'hit' is like 'get' without the overhead of obtaining the value # it's useful for keeping values from expiring when you already have # the values $cache->hit("foo"); # also takes a list of keys DESCRIPTION
Unlike CHI which attempts to address the problem of caching things persistently, this module implements in memory caching, designed primarily for shared references in memory. This collection of classes implements a number of semi related algorithms. METHODS
get @keys Fetch entries from the cache. hit @keys Promote @keys in the cache. Same effect as "get" except it doesn't actually return anything. set $key => $value Adds an entry to the cache. compute $key, sub { ...; return $value } Calls "get" with $key. If there's a hit the value is returned. Otherwise the code block is executed to compute the value, and the result is stored in the cache using "set". remove @keys Remove specific entries from the cache. expire $x Remove $x many entries from the cache. Hopefully the entries removed are the most useless ones. $x defaults to 1. clear Empty the cache. ALGORITHMS
FIFO This is a simple FIFO queue where a "set" places the element on the head of a queue, and if the size is too big an element will be discarded from the tail of the queue. Cache::Bounded provides similar behavior, but flushing happens periodically and in bigger numbers. Therefore, performance will be better on very high cache usage, when hits don't matter that much. This implementation has the lowest memory overhead, due to the simplicity of its data structures (just a hash and an array). Its expiry policy is appropriate for when the data set has a high locality of reference, and random access is generally confined to neighbors, as a part of some larger scan. For truly random access cache hit rates will suffer. Long term utility of cache entries is not considered at all, so scans will poison the cache. This is the only algorithm for which "get" (and "hit") has no side effects. LRU This implementation uses an LRU list of entries (two implementations are provided for trading off memory for speed). Long term utility of cache entries is not considered at all, so scans will poison the cache. Cache::Ref::Util::LRU::List Uses a doubly linked list to perform MRU propagation. Faster than Array. Cache hits and LRU removal is O(1). Cache::Ref::Util::LRU::Array Generally slower for a cache size bigger than about 10 elements, but uses less memory due to the compact layout. Cache hits are O(cache size). LRU removal is O(1). CLOCK This is an implementation of second chance FIFO, using a circular buffer. Second chance FIFO is a very simple approximation of LRU. The CLOCK algorithm has its origins in Multics' virtual memory paging implementation. It's slightly more general purpose than FIFO when dealing with random access. Long term utility of cache entries is not considered at all, so scans will poison the cache. Using values of "k" bigger than 1 (the default), more accurate approximations of LRU can be made, at the cost of more complicated expiry. GCLOCK Tries to approximate LFU instead of LRU. Cache hits increment a counter by one, instead of resetting it to the constant "k". Cache replacement decays existing counters just like CLOCK. CAR CLOCK with Adaptive Removal. A self tuning cache that varies between approximations of LRU and LFU expiry. Has the highest memory overhead of all the implementations due to the extent of the metadata it maintains. However, this overhead is still small for when sizeable objects are involved. Resistent to cache poisoning when scanning. CART CAR with temporal filtering. Like CAR but does not promote a cache entry to the long term usefulness set due to frequent successive access. This is probably the most general purpose algorithm. SEE ALSO
CHI Appropriate for persistent caching of data with complex expiry. Cache::Cascade Can be used to layer Cache::Ref over other caches (e.g. CHI). Cache::Bounded A simpler implementation with similar goals (memory only caching), designed for when cache misses are not very high cost, so cache hits have an extremely low overhead and the policy is very simplistic. Cache::Weak Caches shared references for as long as there is some other reference to those objects. Cache::Profile Designed to help choose an appropriate cache layer. Algorithm information <http://en.wikipedia.org/wiki/Cache_algorithms> <http://en.wikipedia.org/wiki/Page_replacement_algorithm> <http://www.almaden.ibm.com/cs/people/dmodha/clockfast.pdf> VERSION CONTROL
http://github.com/nothingmuch/Cache-Ref <http://github.com/nothingmuch/Cache-Ref> AUTHOR
Yuval Kogman COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.12.4 2010-09-23 Cache::Ref(3pm)
All times are GMT -4. The time now is 10:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy