konark
1
file.txt contains
------------------
sat1 1300
#sat2 2400
sat3
sat4 500
sat5
I need to write a shell script that will output like the below
#output
sat1.ksh 1300
sat3.ksh
sat4.ksh 500
sat5.ksh
my try
-------
#!/bin/ksh
while read x y
do
echo "${x}.ksh ${y}"
done < file.txt
issues:
1) It doesnt ignore the 2nd line with # in the beginning.how to do it here.
2) Can we use awk or sed for the file reading? if yes then how?
Hi,
Try this one:
awk '{
if (index($1,"#")==0)
print $1".ksh"" "$2
}' filename
Lakris
3
try using
instead. Not the most elegant way perhaps...
veer
4
OK..chk this cmdline out
sed '/^#/d;s/\(.\) \(.\)/\1.sh \2/' file.txt
The above is the best fix with sed cmd.
Looping with while or for is not recommended unless u feel the need for the same. But if still u insist with while cmd then it goes as below
while read x y
do
echo "$x $y" | grep "^#" > /dev/null 2>&1
[ $? -ne 0 ] && echo "${x}.sh $y"
done < file.txt
With both sed and while u will get the similar output. But while is timeconsuming and too many cmds involved.
aigles
5
#!/bin/ksh
while read x y
do
[[ "$x" != \#* ]] && echo "${x}.ksh ${y}"
done < file.txt
Jean-Pierre.