Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print line not starting with a space or # 1

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
US
Hi Gurus,

Can someone help me here. I have a file with following entry and need to print the lines not starting with a space or # using perl.

Network 1
75.26.2.20
87.34.9.23
#73.90.23.12
Network 2
75.26.2.22
87.34.9.21
#73.90.23.11

I need the output as

Network 1
75.26.2.20
Network 2
75.26.2.22

Thank You,
dbadmin
 
Hi feherke,

Thank you so much for the quick reply. I am getting an error when running your command

"Can't find string terminator "'" anywhere before EOF at -e line 1."

I am running the perl command on DOS. Does it have anything to do with it?

Thanks,
dbadmin
 
Hi

dbadmin said:
I am running the perl command on DOS. Does it have anything to do with it?
Certainly.

Try replacing the single quotes ( ' ) with double quotes ( " ).

If that DOS in reality is Windows XP or newer's [tt]cmd.exe[/tt], then is possible to need to escape the carets ( ^ ) somehow. No idea how.

Feherke.
 
Hi Feherke,

That worked.
Does it remove tab character also?

Thanks,
dbadmin
 
Hi

dbadmin said:
Does it remove tab character also?
No. There was no word about tabs until now.

You mean to also exclude lines starting with tab character ?

Just add it to the set of forbidden characters :
Code:
perl -ne 'print if /^[^ #[red]\t[/red]]/' /input/file

[gray]# or[/gray]

perl -pe 's/^[ #[red]\t[/red]].*//s' /input/file
Or you can change it to exclude whitespace characters ( space, horizontal tab, carriage return ) :
Code:
perl -ne 'print if /^[^#[red]\s[/red]]/' /input/file

[gray]# or[/gray]

perl -pe 's/^[#[red]\s[/red]].*//s' /input/file

Feherke.
 
Hi Feherke,

Thank You! That works like a charm.

regards,
dbadmin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top