LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2011, 07:41 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Bash check file for pattern


How do I write an if statement in Bash so that it executes only if the first line in a file matches or doesn't match a regex?
 
Old 02-12-2011, 07:53 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Do you mean something like this
Code:
if [[ "$(head -n1 file)" =~ RegEx ]];then
...
else
...
fi
Notice, that the RegEx is not enclosed in braces, quotes or whatsoever.
Alternatively,
Code:
read var < file
if [[ "$var" =~ RegEx ]];then
...
else
...
fi

Last edited by crts; 02-12-2011 at 07:56 PM.
 
Old 02-12-2011, 08:00 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Is it OK if the regex contains whitespace, are quotes still not needed?
 
Old 02-12-2011, 08:11 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MTK358 View Post
Is it OK if the regex contains whitespace, are quotes still not needed?
Nope, but you have to escape the spaces. Bash does "funny" things with your RegEx if you enclose it in quotes. Go ahead and see for yourself what happens:
Code:
set -x
if [[ $(read ...) =~ "some space .*" ]]; ...
Notice, how '.*' gets mangled. Maybe there is a shopt or setting to control this behavior. Not sure.
 
Old 02-13-2011, 12:57 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
If you use sh or older bash versions without [[ support, you can always use sed:
Code:
if sed -ne '1 { /pattern/ q0 } ; q1' file ; then
    echo file contains pattern on the first line
fi
When invoked like this, sed only reads the first line of file.

Hope this helps,
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 08:04 AM.
 
  


Reply

Tags
shell script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash read each file in a directory and match with pattern threeonethree Programming 25 11-29-2010 11:30 AM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
[SOLVED] BASH: how to substitute just one occurency of a pattern in a text file carolflb Linux - Newbie 8 11-19-2009 03:31 AM
Bash test if pattern match of file exists bioinformatics_guy Linux - Newbie 4 04-17-2009 06:42 AM
Bash Help: Check if file exists richinsc Programming 6 01-09-2009 12:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration