LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2018, 05:39 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
[bash] insert string randomly in the file?


Hi can someone help me how to insert string "insert this " in a random position/anywhere in the file xx?
Just once insert.

My sed insert it in each line at the beginning of the file.

Code:
cat xx |  sed 's/^/insert this /'
insert this sdgerger
insert this gjrth
insert this sdgesgA
insert this dfsdkfsdhfkjhsg
insert this Ber
insert this sgsergerhgierug
 
Old 05-01-2018, 06:01 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,148

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Probably not - if it is to be random, how can we possibly help.
Be exact, not hopelessly vague with your request.
- only at the beginning of line ?
- which line ? Is this known in advance ?.
 
Old 05-01-2018, 06:34 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Hi Syg,
I want to add string "insert this " anywhere/randomly but not at the beginning or end of the file.
Somewhere in the middle of the file.
 
Old 05-01-2018, 06:42 AM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,730

Rep: Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743
What you describe is interesting. There are ways to insert a string at an ARBITRARY location, but a "random" location is a complex concept. I doubt you can achieve random in this context.
 
Old 05-01-2018, 06:54 AM   #5
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Smile

Hi,
i think im getting closer.
Now I need to figure out how to add "insert this " only in 1, randomly chosen, line. Any hints?

Code:
random_position="$(shuf -i 0-10 -n 1)"
sed "s/^\(.\{"${random_position}"\}\)./\1insert this /" xx
sdgerger
gjrth
sdgesgA
dfsdkfsdinsert this fkjhsg
Ber
sgsergerinsert this gierug

Last edited by czezz; 05-01-2018 at 06:55 AM.
 
Old 05-01-2018, 07:04 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,343
Blog Entries: 3

Rep: Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754
If you are running bash or zsh you can use the pseudo-random environment variable $RANDOM to insert at a random line number:

Code:
#!/bin/zsh

file="czezz.data";

l=$(wc -l < $file);

let "r = $RANDOM % $l + 1";

sed -e "$r iInserted Line" < $file;
For more about arithmetic see https://ryanstutorials.net/bash-scri...arithmetic.php
Or "man bash" or "man zsh"

And be sure to read "man sed" about the i and a commands.

ksh might be an option but standard POSIX shells are out for this particular approach.
 
1 members found this post helpful.
Old 05-01-2018, 09:43 AM   #7
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Thanks for your effort.
It ADDs a new line whereas I want to add a string to already existing line (randomly chosen), somewhere in the middle of this line (random position).

What I managed to produce (based on yours) is: choose random line number and add STRING at the end of it.
That's close what I need.
If I only knew now how to add STRING somewhere in the middle (random position) of this line. (which I actual achieved in my previous post but dont know how to merge it with this one)


Code:
#!/bin/bash

file="czezz.data";
l=$(wc -l < $file);
let "r = $RANDOM % $l +1";
echo $r;

sed "${r}s/$/ insert this/" < $file;

Last edited by czezz; 05-01-2018 at 09:46 AM.
 
Old 05-01-2018, 10:04 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,343
Blog Entries: 3

Rep: Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754
Just add another read, maybe like this:

Code:
#!/bin/zsh

file="czezz.data";

l=$(wc -l < $file);

let "r = $RANDOM % $l + 1";

w=$(sed -e "$r!d" < $file | wc -m);

let "s = $RANDOM % $w + 1";

sed "s/^\(.\{"${s}"\}\)/\1insert this/" < $file;
The first pass picks a random line. The second pass picks a random character from that line. The third pass does the insertion.

It's inefficient, so maybe the knowledgeable people can say something.
 
Old 05-01-2018, 10:06 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
instead of using sed (which is line-centric), have you tried reading the file character by character, injecting your string at a random position, then writing it back?
seems like the most straightforward shell scripting alternative to me.
 
Old 05-01-2018, 10:44 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by czezz View Post
Thanks for your effort.
It ADDs a new line whereas I want to add a string to already existing line (randomly chosen), somewhere in the middle of this line (random position).

What I managed to produce (based on yours) is: choose random line number and add STRING at the end of it.
That's close what I need.
If I only knew now how to add STRING somewhere in the middle (random position) of this line. (which I actual achieved in my previous post but dont know how to merge it with this one)


Code:
#!/bin/bash

file="czezz.data";
l=$(wc -l < $file);
let "r = $RANDOM % $l +1";
echo $r;

sed "${r}s/$/ insert this/" < $file;
Merged:
Code:
random_position=$(shuf -i 0-10 -n 1)
l=$(wc -l $file);
let "r = $RANDOM % $l +1";

sed "${r}s/^\(.\{"${random_position}"\}\)./\1insert this /" < $file;

Last edited by keefaz; 05-01-2018 at 10:46 AM.
 
Old 05-01-2018, 10:51 AM   #11
xamaco
Member
 
Registered: Sep 2009
Location: Bastelicaccia, Corsica
Distribution: Crux
Posts: 48

Rep: Reputation: 7
Code:
shuf -n1 your-file
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to insert colon in a string with bash mia_tech Linux - General 6 02-13-2023 07:58 PM
re:bash - how would I test for a string from a file and do something based on $string slacker_ Programming 2 06-09-2014 03:35 AM
How to insert a string into a binary file xombboxer Linux - Newbie 2 11-07-2012 08:59 AM
How to insert leading 0 into a string within bash patje999 Programming 4 10-02-2009 05:29 AM
Script to insert string in first line of a file minil Programming 13 01-02-2006 11:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration