to insert some word somewhere in the line with shell (or perl)

hello mighty all
there is a line of 50 words and i need to take a random number of words from the beginning (20 words for example) then put my word then add other 10 words from the continue then add another my special word then add 20 words till the end..

my own knowledge base can say it is possible with making an array with 1 or maybe 2-3 words in each section then write back 5-10 sections then word then another sections etc..

but i'm sure there is more easier way :slight_smile:
can u show it to me?

---------- Post updated at 12:31 PM ---------- Previous update was at 09:00 AM ----------

up ^^

it would be easier to understand your requirement and assist you if you show it with sample file and desired output.

i will take your message and make it an example
i want it to looks like this:
it would be easier "HERE MY WORD" to understand your requirement and assist you if you show it with sample "HERE MY WORD TOO" file and desired output.

just putting my word somewhere inside your message

good sample file!!!

 
>cat file
it would be easier to understand your requirement and assist you if you show it with sample file and desired output.
>sed -e 's/easier/& "HERE MY WORD"/' -e 's/sample/& "HERE MY WORD TOO"/' file
it would be easier "HERE MY WORD" to understand your requirement and assist you if you show it with sample "HERE MY WORD TOO" file and desired output.

thanks
but hardest thing is
i need to put it in a random place of the line
and i need to put it twice or three times in 1 line
and it should be like 5-10 words after the beginning or previous one

i did it like that:

newfile1=$(basename $curfile)
newfile2=$(cat $dir3/pagestotal |grep $newfile1 |cut -f1).art
untilrnd1=$(expr $RANDOM % 6 + 5)
iuntil=$(seq 0 $untilrnd1)
artwords=($(tail -n1 $dir3$newfile2))
mywordhere="HERE IS MY WORD"
:>articlegogo
for j in $iuntil; do
echo -n "${artwords[$j]} " >>articlegogo
done
echo -n "$mywordhere " >>articlegogo

untilrnd2=$(expr $untilrnd1 + 1)
untilrnd3=$(expr $untilrnd2 + $untilrnd1)
iuntil=$(seq $untilrnd2 $untilrnd3)
for j in $iuntil; do
echo -n "${artwords[$j]} " >>articlegogo
done
echo -n "$mywordhere " >>articlegogo

untilrnd4=$(expr $untilrnd3 + 1)
untilrnd5=$(expr $untilrnd4 + $untilrnd3)
iuntil=$(seq $untilrnd4 $untilrnd5)
for j in $iuntil; do
echo -n "${artwords[$j]} " >>articlegogo
done

but i'm pretty sure there's shorter way..

If correctly understand what you are after, here is something to put you on track:

Sample file is on one line with no \n.

$ cat file
hello mighty all there is a line of words and i need to take a random number of words from the beginning (20 words for example) 
then put my word then add other 10 words from the continue then add another my special word then add words till the end

$ wc -w file
50 file

$ awk 'BEGIN{srand()}{for(i=1;i<=20;i++) printf "%s ",$(int(rand()*50)+1);print ""}' file
my the 10 words put words need add of then 10 number till number need my take number my words 

$ awk 'BEGIN{srand()}{for(i=1;i<=20;i++) printf "%s ",$(int(rand()*50)+1);print ""}' file
my word then the words number line words take (20 my my take beginning hello is till words line add 

$ awk 'BEGIN{srand()}{for(i=1;i<=20;i++) printf "%s ",$(int(rand()*50)+1);print ""}' file
is add (20 beginning end hello the words the my words add then add words word a a of mighty 

u did a shuffle..
which output is 20 very random words from the original message
and i need to take 10-20 first words from the original message, then put MY WORD then take another 10-20 next words and again put MY WORD and so on 3 times.. and write it all in 1 line

Without sample of input and desired output it is difficult to guess what you are looking for. However try this on the sample row (50 words) I posted above:

 awk -v RS=" " 'NR==20||NR==30 {$0="MYWORD_" NR " " $0} {printf "%s ", $0}' file

It's not clear to me where exactly you'd like the words to appear, but something like this should work:

#!/bin/ksh
i=1
insertpoints="$((10+RANDOM%10+1))|$((20+RANDOM%10+1))|$((30+RANDOM%10+1))"
mywordhere="HERE MY WORD"
cat infile|xargs -n1|while read inword; do
  case $((i++)) in
    $insertpoints) echo $mywordhere;;
  esac
  echo $inword
done|xargs

The script takes its input from the file "infile". Adjust insertpoints to suit your requirements...

yeah thats exactly what i need
thanks alot

---------- Post updated at 06:45 PM ---------- Previous update was at 06:42 PM ----------

not working for me at all in /bin/sh
dont have ksh

---------- Post updated at 07:03 PM ---------- Previous update was at 06:45 PM ----------

ops
sorry but that is very close to what i need but not exactly
the one last thing is "MYWORD_" string is different all 3 times
i mean it have fixed value but not similar to each other
its like MYWORD_A MYWORD_B
how to do it?

Ok, you can store your MYWORDS in a array in the BEGIN bloc and call them in turn each time you need to insert one of them.

awk -v RS=" " '
BEGIN{mywords[1]="MYWORD_A"; mywords[2]="MYWORD_B"; mywords[3]="MYWORD_C"}
NR==20||NR==30{
	i++
	$0=mywords " " $0
} 
{
	printf "%s ", $0
}' file

yes now its finaly what i need :slight_smile: thanks again

what do u think its better to understand awk/sed or start learning perl instead?

I don't know perl but I would think that, for simple tasks like this one, awk is more than enough and its learning curve less steep with perl. But if you need a full fledged language take perl or the increasing popular Python. There are enough languages out there that could fulfill your needs. Take the one you feel the most comfortable with.