add line and remove comment in some script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add line and remove comment in some script
# 1  
Old 12-27-2011
add line and remove comment in some script

Hi,

i need some help. i am not sure about my idea.

I have a script directory under my home directory,which has a lot of scripts in it.
These are some names of the scripts in /axxhome/prdv/script
Code:
aly300.sh
axt300.sh
arv300.sh
clp300.sh
ctth300.sh
aly400.sh
axt400.sh
arv400.sh
clp400.sh
ctth400.sh
aly500.sh
axt500.sh
arv500.sh
clp500.sh
ctth500.sh

Those scripts are calling a very important script(~/bin/chk_env.sh) an the very last line. In some script it is present, in some it is commented (a "#" in the beginning) and in some its not there.

My requirements are to check for a particulate version (e.g300/400/500) and
Code:
1. If commented, then remove the comment(remove the "#")
2. If Not present, add the line to the last line of the script 
  (The line should be ~/bin/chk_env.sh)

There are around 2 to 3 hundred scripts of each versions like 300, 400, 417, 500 so its not possible to check all manually.

What i achieved so far is just to check the comment and line not present part, and send mail to the Team.
# 2  
Old 12-27-2011
What have you tried so far?
# 3  
Old 12-27-2011
Please see the last line that's what i achieved so far. Smilie
I am new to this part of the world.
Not getting any any direction how to go about this.Smilie

If you have some, please share.Smilie
# 4  
Old 12-27-2011
This is inefficient, I know, but just delete the string, delete the string with a comment, and then add it to the end of the file.

Code:
 
#!/bin/ksh
 
cd /axxhome/prdv/script
if [[ $? -ne 0 ]]
then
   echo "cd error"
fi
for i in *.sh
do
   # look at only 300, 400, and 500 scripts
   if echo "$i" |egrep '300|400|500' > /dev/null
   then
      # delete the string
      sed 's|^\~/bin/chk_env.sh||g' $i > $i.tmp
      # delete the commented string
      sed 's|^#\~/bin/chk_env.sh||g' $i.tmp > $i.tmp1
      # add the string
      echo "~/bin/chk_env.sh" >> $i.tmp1
      # rename when it works
      #mv $i.tmp1 $i
   fi
done

You probably can get by without using the sed command that deletes the string with a comment.
# 5  
Old 12-27-2011
Thanks for replying.

But is there any way, so that i don't need to delete the existing lines.
As in
Code:
# delete the string
      sed 's|^\~/bin/chk_env.sh||g' $i > $i.tmp

Just to check if line not present then add, and if commented then remove the comment.
# 6  
Old 12-28-2011
Just a thought...

What's wrong with.....


Code:
 
for i in *300.sh *400.sh *500.sh
do
   grep -v "/bin/chk_env.sh" $i > ${i}.tmp
   echo '~/bin/chk_env.sh' >> ${i}.tmp
   mv $i ${i}.old && mv ${i}.tmp $i
done

???
# 7  
Old 12-28-2011
I hope this will work...

Code:
cat replace.sh
sed -i 's|#/bin/chk_env.sh|/bin/chk_env.sh|g' $1
grep -l '/bin/chk_env.sh' $1
if [[ $? != 0 ]]
then
        echo "/bin/chk_env.sh" >> $1
fi

Code:
find . -type f -name "*300.sh" -exec ./replace.sh \;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove Comment Lines From Script/Input File

Hello, I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines. /******************************************************************** *** This Is a Comment... (4 Replies)
Discussion started by: arooonatr
4 Replies

2. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Add comment on last line if found match

Hi All, totally new on it , normally use it for just 1 line. i'm looking for help. i'm have 2 file. file 1 : -------------------------------------------------- c12 c1 c3 -------------------------------------------------- file 2: other content ... (10 Replies)
Discussion started by: kttan
10 Replies

4. Shell Programming and Scripting

Need perl regular expression to remove the comment

I need a perl substitution to remove only the comment in the line . That line may have '#' with in double quotes .I used the following , s/(^.*\".+?#.+?\".+?)(#.*)/$1/g It works for , print " not a comment # not a comment " . "not a comment # not a comment" ; # It is a comment ... (3 Replies)
Discussion started by: karthigayan
3 Replies

5. Shell Programming and Scripting

Help to Add and Remove Records only from first line/last line

Hi, I need help with a maybe total simple issue but somehow I am not getting it. I am not able to etablish a sed or awk command which is adding to the first line in a text and removing only from the last line the ",". The file is looking like follow: TABLE1, TABLE2, . . . TABLE99,... (4 Replies)
Discussion started by: enjoy
4 Replies

6. Shell Programming and Scripting

comment and uncomment a line with Shell Script

Requirement is: 1. comment and uncomment the line with Shell Script: /opt/admin/fastpg/bin/fastpg.exe -c -=NET (using fastpg.exe as a search option) 2. display = "Commented" (when its commented) and display = "Uncommented" (when its uncommented) Its urgent, please let me asap!!! Thanks in... (2 Replies)
Discussion started by: anthonyraj75
2 Replies

7. Shell Programming and Scripting

Remove the comment symbol ' from a file.

I want to remove the commented lines in a file identified by ' symbol at the start of each ine. A sample example will be like: Input ----- 'IFerr_flag=0THEN iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then Output -------... (3 Replies)
Discussion started by: joyan321
3 Replies

8. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

9. Programming

remove single-line comment

Does anyone knows how to write a program to remove single-line comment in C program? that means it don't read anything behind // (3 Replies)
Discussion started by: Icy002
3 Replies

10. Shell Programming and Scripting

Need to add a comment line in a text file

Hi I need to add a comment line at the begining of a text file. The scenario is given below. 1. The number of servers that needs to be updated is around 80 2. The location of the text file in all the servers are the same including the file name. 3. The comment has to be added at the very... (2 Replies)
Discussion started by: orakhan
2 Replies
Login or Register to Ask a Question