How to comment a specific line of a file in Solaris 10?

Hi Folks,

sed -i '3s/^/#/' abc.txt is commenting the line number 3 in LINUX

Not Working in Solaris 10 or higher

sed -i sed -i '3s/^/#/'   abc.txt
sed: illegal option -- i

i have a file as below

cat abc.txt
bc
vdv
shhsd
cdc
skdk

Please advise

Hello abhaydas,

I don't have Solaris so couldn't test it, should work but. Could you please try following.

sed '3s/^/#/'  abc.txt > temp && mv temp abc.txt

Thanks,
R. Singh

The following is a "safe" use of a tmp file - the && only on success will continue with the following command, and the cp leaves the inode/owner/group/permissions/linkage intact.

file=abc.txt
ftmp=$file.tmp.$$
sed '3s/^/#/' $file >$ftmp &&
cp $ftmp $file &&
rm $ftmp

Or use perl - like sed it will alter inode and linkage.

perl -i -pe '$.==3 and s/^/#/' abc.txt

Thanks Ravinder and MadeInGermany , That helped

Your Welcome. On unix.com we Thank people for useful posts by hitting THANKS button at each post's last left side, you could do so too, cheers and happy learning.

Also don't forget to put code tags for your samples/codes too.

Thanks,
R. Singh

Below I am hard-coding the value of line number .How to pass it with variable ?

In my script (Solaris) say in abc.txt ,it's 3rd line I have to comment

sed '3s/^/#/'  abc.txt > temp && mv temp abc.txt
cat abc.txt 
bc
vdv
shhsd
cdc
skdk

Script

#!/bin/bash
#set -x
grep -n cdc abc.txt | cut -f1 -d: | sort -u >>a.txt

filename="a.txt"
while read line
do
    # $line variable contains current line read from the file
    # display $line text on the screen or do something with it.

    echo "$line"
sed $lines^/#/" abc.txt  > temp && mv temp abc.txt
done < $filename

A bit complex, no? And a waste of resources, if you change more than one line, as you open/read your file, create a temp file, and mv it once per change!

If you know the pattern, why the detour via grep (and the needless sort )? Why not have sed do it all for you, like

sed '/cdc/s/^/#/' file

? That will comment out any line that has cdc in it, at any position. Anchor the pattern at begin-of-line with ^ , if need be.

For your syntax problem (I presume you have, but you don't post any error message), use a leading double quote, and enclose the variable name in braces. man bash :

Thanks Rudic for your valuable comments

Say in abc.txt as below i have below entries

cat abc.txt

bc
cdcggd
vdv
shhsd
cdc
skdk
cdc45ggd

and when i do

grep -n cdc abc.txt
2:cdcggd
5:cdc
7:cdc45ggd

How to handle such then? where i have to disable all 3 entries where these 3 line numbers is to be passes as variable to a sed command in a script?

Again, why the numbered grep detour, opening/reading the file more frequently than necessary? Do you need the line numbers elsewhere?

For numbers, how about

sed -n '/cdc/=' file | sed 's-$-s/^/#/-' | sed -f- file

Hi Rudic,

tried executing your command in SOLARIS but end-up with below

bash-3.2$

sed -n '/cdc/=' abc.txt | sed 's-$-s/^/#/-' | sed -f- abc.txt
Cannot open pattern-file: -

My requirement is as below.
i have to check for any entry say cdc in a file abc.txt and from their i will comment the one which i need.

Example

I am getting the line numbers where cdc occurred ,as below and i am interested in commenting line 2 and 7 only not 5 in single go by passing the line numbers i have to comment it,Please suggest how this can be done in Solaris or do you have any other approach,kindly suggest

grep -n cdc abc.txt 
2:cdcggd
5:cdc
7:cdc45ggd
cat abc.txt
bc
cdcggd
vdv
shhsd
cdc
skdk
cdc45ggd

Thanks
Abhay

That means you need to interact with the script to select / deselect the relevant lines. Which would make your approach in post #6 moot. You'd need to find the lines, propose them to the user, read the y/n answer, and then go on commenting them out.

If you have a rule like " cdc must appear at the beginning of the line and followed by a character",
then a simple RegularExpression does it:

perl -pe '/^cdc./ and s/^/#/' abc.txt

A -i option will write back to the abc.txt file.
If you want to manually select certain lines from grep -n output, then

perl -i -pe 2'==$. and s/^/#/' abc.txt
perl -i -pe 7'==$. and s/^/#/' abc.txt

If you want to use a shell variable, e.g. from a loop:

for num in 2 7; do perl -i -pe ${num}'==$. and s/^/#/' abc.txt; done
1 Like

Hi Folks,

Need help on below code, where i am trying to redirect the output of grep command in a file /tmp/a.txt for each server with this below code but it's only getting generated on server1 why not in server2 and 3 ? please advise

ssh -t "$2@$1" "grep -n '"$env"' $A | cut -f1 -d: | sort -u >>/tmp/a.txt"

Error getting is + ./test.sh: line xx: /tmp/a.txt: cannot open [No such file or directory]

#!/bin/bash
set -x

echo INFO :  Please type the word to search
read env
A=/opt/abc.txt
set -- serbver1 User1 serbver2 User2 serbver3 User3
while [ "$#" -gt 0 ]

do

ssh -t "$2@$1" "cp /opt/abc.txt /opt/abc.txt_bkp"
ssh -t "$2@$1" "grep -n '"$env"' $A | cut -f1 -d: | sort -u >>/tmp/a.txt"
filename="/tmp/a.txt"
while read line
do
var=$line;var1=$A;sed "${var}s"/^/#/ "${var1}" > /opt/temp && mv /opt/temp  /opt/abc.txt
done < $filename
rm -rf /tmp/a.txt
     shift 2
done

Won't it divert the output of below in each server ?

ssh -t "$2@$1" "grep -n '"$env"' $A | cut -f1 -d: | sort -u >>/tmp/a.txt"

HI Geek's

Please advise..if anything i am missing

Hello abhaydas,

Always try to following rule of 1 thread ==> 1 question please.

Geeks have really helped you in this thread, so for future users who want to get reference from this thread, will get confuse if you keep adding questions to a same post, kindly keep post's Original question ALIVE through out the threads and open new threads for new questions, cheers and happy learning on this GREAT forum UNIX.com

Thanks,
R. Singh

1 Like