ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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 ?.
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.
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
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)
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.