The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inserting a String in a file header. Hari123 Shell Programming and Scripting 3 05-14-2008 06:00 AM
Inserting a column in a file dhanamurthy Shell Programming and Scripting 7 05-11-2008 07:29 AM
Inserting New Line in File using Sed ?? Mary_xxx Shell Programming and Scripting 4 02-27-2008 01:50 PM
inserting into a data file paul1s UNIX for Dummies Questions & Answers 4 10-12-2006 11:47 PM
Inserting new line after match of a fixed string sunil_neha Shell Programming and Scripting 6 04-13-2004 08:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-27-2006
Registered User
 

Join Date: Mar 2006
Posts: 2
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.
Reply With Quote
Forum Sponsor
  #2  
Old 03-27-2006
mph mph is offline
Registered User
 

Join Date: Mar 2006
Posts: 44
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
Reply With Quote
  #3  
Old 03-28-2006
Registered User
 

Join Date: Mar 2006
Posts: 2
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
Reply With Quote
  #4  
Old 03-28-2006
mph mph is offline
Registered User
 

Join Date: Mar 2006
Posts: 44
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 08:27 PM.
Reply With Quote
  #5  
Old 04-11-2006
csaha's Avatar
Registered User
 

Join Date: Feb 2005
Posts: 81
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.

Thanks in advance
C Saha
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0