Replace randomly occurrences bash


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Replace randomly occurrences bash
# 8  
Old 02-20-2020
Hi
Code:
awk -F '' ' #for multiple of 3 occurrences
BEGIN   {srand(); split("a a b", ab, " ")}
        {for(i=1; i<=NF; i++)
                if($i == "a") $i = ab[int(rand()*10%3)+1]
        } 1' OFS='' file

Code:
a a a a a a a a a b
a b a a a b a a b b
the table stands at the window bnd
the picture hang on the wbll.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Randomly create time in UNIX

Hey, How can i create randomly create time N times. Suppose i want to create data for a particualr date 5 times... Mon Jan 19 11:42:50 Mon Jan 19 19:16:40 Mon Jan 19 12:12:33 Mon Jan 19 14:26:27 Mon Jan 19 12:29:53 Mon Jan 19 13:30:31 I want the script to create N times randome... (2 Replies)
Discussion started by: jaituteja
2 Replies

2. UNIX for Dummies Questions & Answers

BASH - Counting word occurrences in a Web Page

Hi all, I have to do a script bash (for university) that counts all word occurrences in a specific web page. anyone can help me?. Thanks :) (1 Reply)
Discussion started by: piacentero
1 Replies

3. Shell Programming and Scripting

Get 20% of lines in File randomly

Hello, This is my code: nb_lignes=`wc -l $1 | cut -d " " -f1` for i in $(seq $nb_lignes) do m=`head $1 -n $i | tail -1` //command done Please how can i change it to get Get 20% of lines in File randomly to apply "command" on each line ? 20% or 40% or 60 % (it's a parameter) Thank you. (15 Replies)
Discussion started by: chercheur857
15 Replies

4. Programming

Java application dying randomly

Hi, (First post, please be gental!) I have a java app that I am running on unix (centos) But it keeps dying randomly. The times seem random from anything between 3 hours and 3 days. I have a cronjob running to restart it when ever it dies but I would rather this happened less often. ... (2 Replies)
Discussion started by: sm9ai
2 Replies

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

6. Shell Programming and Scripting

Cron job randomly once a day

I want to create a cron job randomly once a day for my site's registration. The responsible file for registrations is a config file and I need to change the contents twice on day (on and off) I know the way for random cron job for example */n * * * * /usr/local/bin/php... (6 Replies)
Discussion started by: lucker
6 Replies

7. Shell Programming and Scripting

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

I have a text (text.txt) and I would like to replace only the first 2 occurrences of a word (but I might need to replace more): For example, if text is this: CAR sweet head hat red yellow CAR book brown tiger CAR cow CAR CAR milk I would like to replace the word "CAR" with word... (12 Replies)
Discussion started by: bingel
12 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. UNIX for Dummies Questions & Answers

randomly renaming files

I have a directory of files that look like filename 001.ext, filename 002.ext, etc. I'd like to rename the files with unique random numbered names, so that the original filenames are stripped and the files are given a new, random number name. I'm not super new to UNIX, but I don't often use it for... (2 Replies)
Discussion started by: platz
2 Replies

10. Shell Programming and Scripting

how to select a value randomly

on my desktop i am using the kde rotating desktop image option. this rotates images randomly every half hour. now, i would like to write an html file which will have an inline frame with some text, maybe system messages, or my friends live journal thati read alot, or unix.com! however, i dont want... (1 Reply)
Discussion started by: norsk hedensk
1 Replies
Login or Register to Ask a Question
NETSED(1)							      NetSED								 NETSED(1)

NAME
netsed - a network stream editor. SYNOPSIS
netsed {proto} {lport} {rhost} {rport} {rule} [rule ...] DESCRIPTION
netsed is a small and handy utility to alter, in real time, the contents of packets forwarded in a network stream, or in a datagram connection. When called with a set of replacement rules, these rules are tested for applicability to each packet entering in either direction. ARGUMENTS
proto Determines the protocol for the desired connection: "tcp", "TCP", "udp", or "UDP". lport The local listening port for the connection. A service name, or a numerical port value, is acceptable. rhost The remote host with whom the connection is desired. Resolvable host names and IPv4/IPv6 addresses are equally usable. As a special case, assigning "0" to rhost will insert the kernel's knowledge of the targeted host address, in a situation where a netfilter rule is redirecting traffic. This happens when running a transparent proxy service. rport The remote port to connect to. A service name, or a numerical port value, is acceptable. Also here a value "0" will be acceptable to arrange a transparent proxy service, as the kernel's tracking will provide the intended remote port number. rule At least one replacement rule is mandatory. The general syntax for this is: s/pat1/pat2[/num] The effect is to replace the text that matches pat1 with the expansion of pat2. The optional parameter num is a numerical value limiting the maximal number of times the rule can be applied. One could say that the rule expires after num occurrences. The rules are applied in succession to all passing packets, flowing in either direction. As soon as a rule has been expired, it is removed from the collection of active rules for the current connection. Observe that any counter is started as the connection is initiated, running as long as the connection is alive. This holds directly for TCP connections, whereas for UDP a connection is considered to consist of incoming data on fixed address and fixed port together with any response from a remote server. When no datagrams have been transmitted for a period of 30 seconds, the UPD connection is seen as closed. A single rule is limited to act on individual packets; a pattern can not match across packet boundaries. Using HTTP-like escape sequences for hexadecimal values, all eight-bit characters are viable in the patterns. Thus the standard character pair CRNL would code as "%0a%0d". In a pattern, the percentage sign itself must be escaped by duplication. Thus a string "%%" is interpreted in a pattern as a literal percentage sign. EXAMPLES
A handful replacement rules are handy as examples. s/andrew/mike Replace every occurrence of the string "andrew" with "mike", in every passing packet. s/andrew/mike/1 Replace only the first occurrence of the string "andrew" for "mike" in each packet. Any repetition is unaltered, unless a further rule specifies some replacement. s/andrew/mike%00%00 Replace in each packet every occurrence of the string "andrew" with "mikex00x00" . The padding with two null bytes ensures an unaltered packet length, which might be essential at times. s/%%/%2f/20 Replace the first twenty occurrences of the percentage character '%' with slashes '/'. AUTHOR
This text was initially compiled by Mats Erik Andersson as a Docbook source from the usage printout. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Public License, version 2, or of a later version. COPYRIGHT
Copyright (C) 2010 Mats Erik Andersson NetSED 1.00b June 19th, 2011 NETSED(1)