LinuxQuestions.org
Visit the LQ Articles and Editorials section
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, 08:41 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
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, 08:53 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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 08:56 PM.
 
Old 02-12-2011, 09:00 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Is it OK if the regex contains whitespace, are quotes still not needed?
 
Old 02-12-2011, 09:11 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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, 01:57 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
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 09:04 AM.
 
  


Reply

Tags
shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 12:30 PM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 09:05 AM
[SOLVED] BASH: how to substitute just one occurency of a pattern in a text file carolflb Linux - Newbie 8 11-19-2009 04:31 AM
Bash test if pattern match of file exists bioinformatics_guy Linux - Newbie 4 04-17-2009 07:42 AM
Bash Help: Check if file exists richinsc Programming 6 01-09-2009 01:27 PM


All times are GMT -5. The time now is 04:00 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration