Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to insert some word somewhere in the line with shell (or perl)
# 1  
Old 09-19-2009
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 Smilie
can u show it to me?

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

up ^^
tip78
# 2  
Old 09-19-2009
it would be easier to understand your requirement and assist you if you show it with sample file and desired output.
# 3  
Old 09-19-2009
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
tip78
# 4  
Old 09-19-2009
good sample file!!!

Code:
 
>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.


Last edited by malcomex999; 09-19-2009 at 08:57 AM..
# 5  
Old 09-19-2009
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:
Code:
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..

Last edited by tip78; 09-19-2009 at 10:40 AM..
tip78
# 6  
Old 09-19-2009
If correctly understand what you are after, here is something to put you on track:

Sample file is on one line with no \n.

Code:
$ 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

# 7  
Old 09-19-2009
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
tip78

Previous Thread | Next Thread
Test Your Knowledge in Computers #791
Difficulty: Easy
Logic gates cannot be used to store data.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort a line and Insert sorted word(s) in a line

Hello, I am looking to automate a task - which is updating an existing access control instruction of a server and making sure that the attributes defined in the instruction is in sorted order. The instructions will be of a specific syntax. For example lets assume below listed is one of an... (6 Replies)
Discussion started by: sanjayroc
6 Replies

2. Linux

How to insert new line in perl

HI, I have a text file in which I have removed all new lines as I would like to introduce a new line at the end of each record in the file. There is no common end line for all the records. A new record will start by *RECORD*. So I want to introduce a new line before this line *RECORD*. So Can... (2 Replies)
Discussion started by: kaav06
2 Replies

3. Shell Programming and Scripting

perl: replace multiple word on a line

Hi All, If I have a line as following: ( MA "vertical" ) How can I convert it to as below: ( BC "horizontal" ) Thanks, --Michael (6 Replies)
Discussion started by: mxn731
6 Replies

4. Shell Programming and Scripting

Perl:How to insert a line to a file.

Hi, Perl is new to me. I am trying to insert a line to a file. Example: I have a file (trial.txt), content: ZZZZ AAA DDDD I am trying to insert CCC below AAA. MY perl command: open (FILE,"+>>C:\\Documents and Settings\\trial.txt\n")|| die "can't open file"; while(<FILE>) { ... (6 Replies)
Discussion started by: SSGKT
6 Replies

5. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

6. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

7. Shell Programming and Scripting

Need to replace the first word of a line if it occurs again in the next line(shell)

Hi folks, have a look into the attachment, i am not familiar with unix, can you please help me in this regard. thanks in advance, :) regards, Geeko (4 Replies)
Discussion started by: geeko
4 Replies

8. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

9. Shell Programming and Scripting

how to insert text before first line in perl

Hello all im doing simple parsing on text file , but now I need to insert string before the first line of the text file , how can I do that in perl? (3 Replies)
Discussion started by: umen
3 Replies

10. Shell Programming and Scripting

insert word in each line of a file

can someone tell me how can I insert a word in front of each line in a file. i tried with sed but didn't managed yet. Is there another command or this one(sed) works? 10x anyway. (7 Replies)
Discussion started by: atticus
7 Replies

Featured Tech Videos