Replace randomly occurrences bash


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Replace randomly occurrences bash
# 1  
Old 02-20-2020
Replace randomly occurrences bash

I want to replace only percent of occurrence of a pattern (not all, for example, half of occurrence of a pattern) in my text and it replaces randomly occurrences (or for example multiple of 3 occurrences).
for example, I have a text like below:
The text contains 20 a.
Code:
a a a a a a a a a a
a a a a a a a a a a

I want to replace half ofa (not whole of them) and randomly (not half of the first occurrences)

replace a to b and the result is something like below:
Code:
a b b b a b a b a a
b a b a b a a a b b

best regards

Last edited by vbe; 02-20-2020 at 05:14 AM..
# 2  
Old 02-20-2020
Welcome on board!
As you have seen our rules, we are here to help you not do the work for you, what have you tried so far?
We also need to know a minimum such as your OS and version and the shell you are using
# 3  
Old 02-20-2020
How "randomly" should those replacements occur? Exactly half? Half per line or per total replacements over all lines?
# 4  
Old 02-20-2020
1- I tried to turn text to one array and replace them something like this:
Code:
text=(
    a a a a a a a a a a
    a a a a a a a a a a
)
Get the number of items

number=${#text[@]}
Get half of it

half=$((number/2))
In a loop from 1 to the half of the text get random number and using it as index change item in text array

for i in $(seq $half); {
    rnd=$((RANDOM % (number-1)))
    text[$rnd]=b
}
Echo result

echo ${text[@]}

But this code doesn't keep the new line and echo all text in one line and my text is so big about 15 Gb and can't put it in one array.

2- Choose randomly from all occurrences of one word or token in the whole text.
simply can get number of repetition by something like this ( grep "a" | wc -l )

3- I can use python to do this but my text is huge and I want to use bash since it is faster than python. In python I use a set contains (a,b) and in replace function use a random function to choose (a or b) from that set.

4- I use Ubuntu 18.04, sed (GNU sed) 4.4, GNU Awk 4.1.4, API: 1.1 (GNU MPFR 4.0.1, GNU MP 6.1.2)

5- I simplify the problem, the main problem is that: I want to normalize a text corpus for training a tri-gram language model, in the language model, the sequence of words is important. I normalize the numbers to letters so for example, I convert all 30 to thirty but we use often half instead of thirty for reporting hour (e.g 8:30). I want to replace randomly things like this not whole of them.

best regards
# 5  
Old 02-20-2020
How about
Code:
awk '{for (i=1; i<=NF; i++) {$i=(int(.5+rand()))?$i:"b"; SUM+=x}} 1' file
b a b a b a b a b a
b b b b b a a b b b

# 6  
Old 02-20-2020
How can I set replace strings?
I meen how can I set "a" and "b" in that code?
# 7  
Old 02-20-2020
Code:
awk '
BEGIN{srand();}
{
fstr=":";
for (fld in fields) delete fields[fld];
while (gsub(":", ":", fstr) <= (NF*(pct/100.0))) {
rnd=rand();
sub("^[^.]*[.]", "", rnd);
fld=((rnd+1) % NF) + 1;
if (fstr !~ ":" fld ":") fstr=fstr fld ":";
}
sub("^:*", "", fstr);
sub(":*$", "", fstr);
n=split(fstr, arr, ":");
for (i=1; i<=n; i++) fields[arr[i]]=i;
for (i=1; i<=NF; i++) $i=(i in fields) ? repstr : $i;
print $0;
}
' pct=50 repstr="b" file


Last edited by rdrtx1; 02-20-2020 at 12:43 PM..
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