inserting a String in the file(s)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting inserting a String in the file(s)
# 1  
Old 03-27-2006
inserting a String in the file(s)

Hi,

I'm a newbee to Unix shell scripting.
I want to write a shell script that inserts a new String(name&value pair) into a file(s) at a particular place.I willl have to write one script which when executed should insert a new variable in all the files in that particular directory.
Say for eg:
I have one variable called PATH in the file and next to this PATH variable in the new line I have to insert/add a new variable.

PATH=jdk
test=value //i will have to add this variable after path .

I have tried doing this with 'sed' but am unsuccessful.

This is what I have written(this might be wrong and also is incomplete).
i="PATH"
x=`basename $0`
for FILE in `grep -l ${i} * | grep -v $x`
do
//here the code for inserting the new variable should come
done

Please help me in this.

Thanks.
# 2  
Old 03-27-2006
Try somthing like this.

the example file is tmp.txt it contains the following lines.
Line 1
Line 2
New 3
Line 4
Line 5
the code is as follows.
Code:
i=New
filename=tmp.txt
for x in `grep -n $i $filename | awk 'BEGIN {FS="[:]"}{print $1}'`
do
  echo ''$x'a\nInserted Line\n.\n\nwq' | ex $filename
done

This will find the instance of New in tmp.txt and insert a line after it with the text Inserted Line. Using ex (which is built in to vi) will eliminate the need for using sed and creating tmp files, moving the tmp file to the original. etc...

MPH
# 3  
Old 03-28-2006
Hi
Thanks for the reply but the script sent by you works only when the file (here it is temp.txt) has numbers at the end of each line.
I have done this in another manner:

#!/bin/ksh
PATTERN="server"
FILE=`basename $0`

for FILE in `grep -l ${PATTERN} * | grep -v $FILE`
do
findline=`grep -n "$PATTERN" $FILE|cut -d: -f1`
find_line=`expr $findline + 1`


sed -e "${find_line},\$d" ${FILE} > /tmp/file_header
f_minus_one=`expr $find_line - 1`
sed -e "1,${f_minus_one}d" ${FILE} > /tmp/file_footer
cat /tmp/file_header > ${FILE}
echo "cookie=value" >> ${FILE}

cat /tmp/file_footer >> ${FILE}

echo "Removing the temporary files "
#rm -rf /tmp/srch.tmp /tmp/srch1.tmp /tmp/file_header /tmp/file_footer
done

I have got whatever I wanted to do but it would be good if I get the desired result by using awk or any other command.Basiclly I want to shorten the code .
Please let me know if there is some thing like that.

Thanks
# 4  
Old 03-28-2006
It works fine with or without numbers at the end. Change the value of "i" to the text your looking for. The major flaw is that it would only catch one instance even with the for loop. Also it could only use a single space delimeted string. Try this and change to your needs.

tmp.txt contains

this is a line of text
this is another line
that is yet another line
this is a line that's not the same
that line above is different!
this is the last line

Code:
#! /bin/ksh
cnt=0
i="this is a line"
insline="Inserted Line"
filename=tmp.txt
grep -n "$i" $filename | awk 'BEGIN {FS="[:]"}{print $1}' | while read x 
do
  echo ''$cnt+$x'a\n'$insline'\n.\n\nwq' | ex $filename
  cnt=`echo $((cnt+1))`
done


Last edited by mph; 03-28-2006 at 11:27 PM..
# 5  
Old 04-11-2006
Still not sure after reading the positings, if I need to append a line after a particular line, then how to do it?

For example -
Code:
My out-put is like this 

$ p4 group -o lpth-everyone 

# A Perforce Group Specification.
#
#  Group:       The name of the group.
#  MaxResults:  A limit on the results of operations for users in
#               this group, or 'unlimited'.  See 'p4 help maxresults'.
#  MaxScanRows: A limit on the data scanned during operations for users
#               in this group, or 'unlimited'.  See 'p4 help maxresults'.
#  Timeout:     A time (in seconds) which determines how long a 'p4 login'
#               session ticket remains valid (default is 12 hours).
#  Subgroups:   Other groups automatically included in this group.
#  Users:       The users in the group.  One per line.
 
Group:  lpth-everyone
 
MaxResults:     unlimited
 
MaxScanRows:    unlimited
 
Timeout:        43200
 
Subgroups:
 
Users:
        aparna
        hiqbal
        kamalesh
        nverma
        rchitu
        rmookherjee
        rsarmiento


Now for example if I need to add "xxxxx" just below "nverma" then how to do it? Which means the output should be like this


Code:
My out-put required to be like this 

$ p4 group -o lpth-everyone | <some unix stream appending operation>

# A Perforce Group Specification.
#
#  Group:       The name of the group.
#  MaxResults:  A limit on the results of operations for users in
#               this group, or 'unlimited'.  See 'p4 help maxresults'.
#  MaxScanRows: A limit on the data scanned during operations for users
#               in this group, or 'unlimited'.  See 'p4 help maxresults'.
#  Timeout:     A time (in seconds) which determines how long a 'p4 login'
#               session ticket remains valid (default is 12 hours).
#  Subgroups:   Other groups automatically included in this group.
#  Users:       The users in the group.  One per line.
 
Group:  lpth-everyone
 
MaxResults:     unlimited
 
MaxScanRows:    unlimited
 
Timeout:        43200
 
Subgroups:
 
Users:
        aparna
        hiqbal
        kamalesh
        nverma
        xxxxx
        rchitu
        rmookherjee
        rsarmiento


NOTE: I need to maintain the space before "xxxxx" as well. Smilie

Thanks in advance
C Saha
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Issue in inserting null string in array

I am getting some values from a file and putting them in an array..but the null strings are not getting passed to the array. So during printing the elements ,the null string is not showing in the output. during array size calculation it is also excluding null.Please let me know how to do it. # cat... (2 Replies)
Discussion started by: millan
2 Replies

2. Shell Programming and Scripting

Inserting a long string (readable in sed)

Hi everyone, I am trying to insert a single very long string as the first line of a file, So the following sed commands does what I want; sed '1i\"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"' file.txt Think that all the... (3 Replies)
Discussion started by: hayreter
3 Replies

3. Shell Programming and Scripting

Inserting into long delimited string using perl.

Hi, I have a very long pipe delimited string. The length of the string could vary. For example: START|one|two|three|four|five|six|seven START|one|two|three|four|five|six|seven|eight|nine START|one|two|three|four I want to replace in the third occurence of string with another... (9 Replies)
Discussion started by: som.nitk
9 Replies

4. Shell Programming and Scripting

Inserting blank lines after string change

My input data looks like this ... -150 120 8 -150 122 7 -150 124 11 -150 126 8 -150 128 19 -150 130 13 -150 132 26 -150 134 38 -150 136 45 -150 138 62 -150 140 75 -150 142 110 -150 144 139 -150 146 138 -150 148 158 -150 150 173 -150 152 217 (5 Replies)
Discussion started by: chrisjorg
5 Replies

5. Shell Programming and Scripting

Inserting string in between field in comma separated file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me to... (8 Replies)
Discussion started by: krsnadasa
8 Replies

6. Shell Programming and Scripting

Inserting a string in another sting

Hi Experts, I need to insert a sting into another string at a specified position. Like the below. Regards, Tin-Tin (3 Replies)
Discussion started by: tinufarid
3 Replies

7. Shell Programming and Scripting

inserting a string to a text file

Hello Can somebody please help me with the following script? I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it... (4 Replies)
Discussion started by: goude
4 Replies

8. Shell Programming and Scripting

inserting a character between string

i have a file contains like this: i want to create a script that will insert a comma "." after the 10th character so it would be look like this thanks in advance (5 Replies)
Discussion started by: dakid
5 Replies

9. Shell Programming and Scripting

Inserting a String in a file header.

Dear all, I have a file created in the name sample.txt in UNIX with header and footer. How to insert a required string (for example "FILE1") in the header part after the file has been created. What kind of command can i use to do the same. Thanks in advance Hari (3 Replies)
Discussion started by: Hari123
3 Replies

10. Shell Programming and Scripting

Inserting new line after match of a fixed string

Hi, I have a file which contains many occurances of a string say "hellosunil". I want to insert a newline charcater after all the "hellosunil" strings in the file. trying to use sed, sed -e 's/hellosunil/\\nhellosunil/g' file1 sed help says u cannot substitute a regular expression... (6 Replies)
Discussion started by: sunil_neha
6 Replies
Login or Register to Ask a Question