..to check if a line includes a subline..?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users ..to check if a line includes a subline..?
# 1  
Old 12-19-2008
..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  
Old 12-19-2008
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  
Old 12-19-2008
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

2. Shell Programming and Scripting

How to pipe a filename that includes a date?

How do I pipe the output of something to a filename that includes the date, in a specific date format? Here's the goal. Output a script to a file periodically during the day: ./script.sh >>logname_yyyy-mm-dd.logAnd when the next day comes, it starts logging to a new filename because the date... (2 Replies)
Discussion started by: nbsparks
2 Replies

3. Shell Programming and Scripting

grep regex, match exact string which includes "/" anywhere on line.

I have a file that contains the 2 following lines (from /proc/mounts) /dev/sdc1 /mnt/backup2 xfs rw,relatime,attr2,noquota 0 0 /dev/sdb1 /mnt/backup xfs rw,relatime,attr2,noquota 0 0 I need to match the string in the second column exactly so that only one result is returned, e.g. > grep... (2 Replies)
Discussion started by: jelloir
2 Replies

4. Programming

Makefile includes and shell environment during compile

Below is the top of my Makefile. On one machine, I have mysql_config5, and another, I have mysql_config. In my .bashrc file of one UNIX machine, I added an alias so that that mysql_config5 is mysql_config, however, when I do make, it doesn't use that environment and I get compile errors, unless I... (1 Reply)
Discussion started by: pyramation
1 Replies

5. Programming

Makefile + #includes

Dear all I try to write a makefile for my code which have many source files, 1 header file that i make and exist into same folder with other source files and some other header files (by libraries witch i use) that found in other folders. In my header file i have put all other #includes for... (3 Replies)
Discussion started by: panou
3 Replies

6. Shell Programming and Scripting

SFTP STDERR includes the acknowledgement

I have this problem on my SFTP script. When you direct the standard error to a file, it also contains the acknlowledgement. See below: sftp.log Connecting to xxx.xxx.xxx This computer is property of x company and is intended on... Connecting to xxx.xxx.xxx This computer is property... (0 Replies)
Discussion started by: The One
0 Replies

7. Programming

Multi-platform includes?

I know that <cstudio> can also be <stdio> and can be written different ways on Linux then with windows. I've see some code doing a IFDEF __APPLE__ (I'm guessing, if compiled on a mac do whats between this) Is there one for Linux/Window? (3 Replies)
Discussion started by: james2432
3 Replies

8. Shell Programming and Scripting

passing string which includes metacharacters

I'm trying to create a bash script that takes a URL as one of its arguments like this: ./script.sh http://url.cfm?asdf&asdf=234 variable=$1 echo $variable I'm having trouble storing the URL because it contains the meta character "&" which is being interpreted... thus when I run the... (4 Replies)
Discussion started by: kds1398
4 Replies

9. UNIX for Dummies Questions & Answers

Dynamic includes file for tar command

I'm writing a script to tar a list of log files. The tar command calls an includes file, which lists all of the log files that I want. My apache web server, however, creates a log file every day with the datestamp (mmddyy) at the end of it. I want the includes file to get the previous day's... (1 Reply)
Discussion started by: kadishmj
1 Replies
Login or Register to Ask a Question