The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check line by line in a file loperam Shell Programming and Scripting 6 11-04-2008 12:24 AM
how to check by line ooilinlove Shell Programming and Scripting 4 08-18-2008 10:35 AM
Check last line with gawk? sjf Shell Programming and Scripting 3 10-04-2006 02:20 AM
Dynamic includes file for tar command kadishmj UNIX for Dummies Questions & Answers 1 03-09-2005 06:48 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-19-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 122
..to check if a line includes a subline..?

What would be the best way to check if a particular line (in variable) has an another line (more than 1 char)?

I understand it could be done with 'grep', 'sad', 'awk', by parameter expansion, but all these seems to me havy, worldy, not elegant and overhead in processing

For so common task I would expect something from shell, maybe, close to:
Code:
>a="string with piece to search"
>${a??"piece"} && echo "the string has a searched substring" || echo "not found"
... I used here a '??' as a, kind of, search operator (as '?' is used for another action.)
But I do not know anything like that. - Maybe I just do not know?
Otherwise,

what would be your best aproach to perform that simple action?

Thank you!
  #2 (permalink)  
Old 12-19-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
With all Bourne-type shells* you can use case and wildcards pattern matching (*I believe the csh family uses a different syntax):

Code:
a="string with piece to search"

case $a in
  *piece*) printf "OK\n";;
        *) printf "KO\n";;
esac
With ksh93, zsh and bash:

Code:
[[ $a == *piece* ]]&&printf "OK\n"||printf "KO\n"
With some versions of the above shells you can even match using regular expressions with the =~ operator.

Last edited by radoulov; 12-19-2008 at 04:40 PM..
  #3 (permalink)  
Old 12-19-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 122
Thank you, it is good: last 2 are pretty nice, the 'case' is interesting, too, but little wordy.
Neither one way did come to my mind.

In my bash the match operator works; so it is best:
Code:
> a="string with piece to search"
> [[ $a =~ piece ]] && ec da || ec net
da
>
Appreciate your addvice!
Sponsored Links
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0