puni
August 26, 2010, 2:53am
1
Hi,
I have many scripts in particular directory. And few of the scripts have exit 0 in second line. Now i wanted to list out the scripts name which has the exit 0 in its second line
I tried many options , but i can not get the filename along with the nth line pattern match . Can anyone please help on this?
Thanks,
Puni
awk 'NR==2&&/exit 0/ {print FILENAME}' infile
zaxxon
August 26, 2010, 2:57am
3
You can try something like:
awk 'NR==2 && /exit 0/ {print FILENAME}' *.sh
If the scripts are scattered over some subdirectories you might want to feed awk with a find or recursive ls.
puni
August 26, 2010, 3:14am
4
Hi rdcwayx and zaxxon,
Thank you. If i specify the input file name its working fine. But if i give *.sh its not giving any output
I want to search in all the scripts, so i cannot specify the input filename
Thanks,
Puni
Try:
awk '/exit 0/&&FNR==2{print FILENAME}' *.sh
puni
August 26, 2010, 3:26am
6
Hi Scrutinizer,
Thanks a lot. Now its working fine. But in some of the scripts has #exit 0. I want the scripts which has exit 0 only.That line should not have commented.
Is there anyway to omit the scripts which has #exit 0?
Thank you,
Puni
How about this,
grep -n "exit 0" *.sh | perl -nle 'if (/(.*\.sh):2:/) {print $1}'
puni:
Hi Scrutinizer,
Thanks a lot. Now its working fine. But in some of the scripts has #exit 0. I want the scripts which has exit 0 only.That line should not have commented.
Is there anyway to omit the scripts which has #exit 0?
Thank you,
Puni
awk '/exit 0/&&FNR==2&&!/^#/{print FILENAME}' *.sh
puni
August 26, 2010, 3:35am
9
Hi Pravin,
Thank you. This command also works fine. But this also gives the script which has #exit 0. Here we cannot use -v option also right
Thank you,
Puni
Try this,
grep -nx "exit 0" *.sh | perl -nle 'if (/(.*\.sh):2:/) {print $1}'
puni
August 26, 2010, 3:40am
11
Hi Pravin,
Thank you.This command also works fine. But this also gives the scripts which has #exit 0. Here i can not use -v option also right
Thanks,
Puni
There are a number of ways in which exit 0 could be commented out. But this might work for simple cases, see if this works for you:
awk '/exit 0/&&!/^[[:space:]]*#/&&FNR==2{print FILENAME}' *.sh
puni
August 26, 2010, 4:42am
13
Hi Scrutinizer,
Thanks a lot. It gives me the perfect result. If i want to excute this command from root.
I execute the command like below
find / -print 2> /dev/null | xargs awk '/exit 0/&&!/^[[:space:]]*#/&&FNR==2{print FILENAME}' 2> /dev/null
But its not working. I am using AIX 5.3. Is there any idea to execute this command from root
Thanks,
Puni
Hi puni,
This will not work since the argument xargs produces will be way to large and it will not work for file names that contain special characters...
try this slower option instead :
find / -name '*.sh' 2> /dev/null -exec awk '/exit 0/&&!/^[[:space:]]*#/&&FNR==2{print FILENAME}' 2> /dev/null {} \;
puni
August 26, 2010, 5:11am
15
Hi Scrutinizer,
Thank you very much. It gives me the perfect result. Thank you again
Regards,
Puni