1833198 Members
2731 Online
110051 Solutions
New Discussion

Shell script

 
SOLVED
Go to solution
Leif Halvarsson_2
Honored Contributor

Shell script

Hi
I want to write a shell script that read a file and searches for lines beginning with # . I think I must use som kind of "pattern matching" (regular expression) but how do I get this work.
7 REPLIES 7
Sebastian Galeski_1
Trusted Contributor

Re: Shell script

cat /tmp/file.txt |grep "^#"
H.Merijn Brand (procura
Honored Contributor

Re: Shell script

1. grep '^#' file
2. perl -ne 'm/^#/&&print' file
Enjoy, Have FUN! H.Merijn
Chris Wilshaw
Honored Contributor

Re: Shell script

Leif,

cat | grep ^#

should be what you need.
John Palmer
Honored Contributor
Solution

Re: Shell script

Hi,

grep will do this for you...

grep "^#"

will print all lines in which start with #.

If you want to process the lines in a script then you can write a shell script like this...

grep "^#" | {
while read LINE
do

done
}

Regards,
John

U.SivaKumar_2
Honored Contributor

Re: Shell script

hi,
you can try
#grep "#" filename
if you want more logic you can use awk
#man awk

regards,
U.SivaKumar
Innovations are made when conventions are broken
John Carr_2
Honored Contributor

Re: Shell script

Hi

these first options will return every line containing a # you need to test the first character of each line to get it right.

!#/usr/bin/ksh

for eachline in filename ;
do

if [[ eachline[0] -eq "#" ]] then
cat $eachline >> newfilename
fi

cant remember the syntax on the if statement can someone correct it please.

John.
John Carr_2
Honored Contributor

Re: Shell script

please ignore my last comment of course the ^ will do it.

John.