Wanna Swap #(comment) between 2 lines.

November 21, 2010 at 00:32:16
Specs: Windows XP
Kindly help me to swap a line with #.The secenario is like this in the file.


com.mobily.creditcontrol.hu.activecustomer.threads=1
#com.mobily.creditcontrol.hu.activecustomer.threads=9

I wanna change to as shown below


#com.mobily.creditcontrol.hu.activecustomer.threads=1
com.mobily.creditcontrol.hu.activecustomer.threads=9


See More: Wanna Swap #(comment) between 2 lines.

Report •

#1
November 22, 2010 at 11:27:02
Since you posted in the Unix forum, I am assuming you want a Unix solution. This awk script removes any # sign at the beginning of the line, and adds a # sign at the beginning if doesn't exist:
#!/bin/ksh

awk '
{
if (!match($0,"^#"))
   print "#"$0
else
   {
   sub("^#","")
   print $0
   }
} ' datafile.txt


Report •

#2
November 22, 2010 at 20:46:59
Named your file "sample.txt"

exec 3<sample.txt
while read -u3 line
do
if [[ $line == "#"* ]]
then
print $line | tr -d '#'
else
print "#$line"
fi
done


Report •
Related Solutions


Ask Question