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)
# 8  
Old 09-19-2009
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:
Code:
 awk -v RS=" " 'NR==20||NR==30 {$0="MYWORD_" NR " " $0} {printf "%s ", $0}' file

# 9  
Old 09-19-2009
It's not clear to me where exactly you'd like the words to appear, but something like this should work:
Code:
#!/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...

Last edited by Scrutinizer; 09-19-2009 at 11:27 AM..
# 10  
Old 09-19-2009
Quote:
Originally Posted by ripat
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:
Code:
 awk -v RS=" " 'NR==20||NR==30 {$0="MYWORD_" NR " " $0} {printf "%s ", $0}' file

yeah thats exactly what i need
thanks alot

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

Quote:
Originally Posted by Scrutinizer
It's not clear to me where exactly you'd like the words to appear, but something like this should work:
Code:
#!/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...
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 ----------

Quote:
Originally Posted by ripat
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:
Code:
 awk -v RS=" " 'NR==20||NR==30 {$0="MYWORD_" NR " " $0} {printf "%s ", $0}' file

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

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

# 12  
Old 09-20-2009
yes now its finaly what i need Smilie thanks again

what do u think its better to understand awk/sed or start learning perl instead?
tip78
# 13  
Old 09-20-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question