Visit Our UNIX and Linux User Community


Find a pattern in a single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a pattern in a single line
# 1  
Old 09-03-2009
Find a pattern in a single line

Hi

I have to find if a pattern is present in a line.
eq
Code:
line="dasdasd hello asdasdasd"

Have to find if "hello" is present in the line or not.
Which command to be used?

Thanks
# 2  
Old 09-03-2009
Quote:
Originally Posted by akashtcs
Hi

I have to find if a pattern is present in a line.
eq
Code:
line="dasdasd hello asdasdasd"

Have to find if "hello" is present in the line or not.
Which command to be used?

Thanks
Use grep, check the man page.

Regards
# 3  
Old 09-03-2009
Grep will search the full file.I want to search only 1 line.

Can grep be used without giving the file name and just giving the variable which has a line stored in it?
i searched but could not find.
# 4  
Old 09-03-2009
Quote:
Originally Posted by akashtcs
Grep will search the full file.I want to search only 1 line.

Can grep be used without giving the file name and just giving the variable which has a line stored in it?
i searched but could not find.
Code:
echo "$variable" | grep <something>

# 5  
Old 09-03-2009
ok thanks...
But this will return the whole line.How do we implement in a if condition?
Code:
if [ pattern is matching in the variable ]
then
do something
fi

# 6  
Old 09-03-2009
something like this :

Code:
var="rama raja roha"

if [ `echo "$var" | awk '$0 ~/rama1/ {print 1}'` ]
then
echo "sucess"
else
echo "failure"
fi

# 7  
Old 09-03-2009
Hello!

Your question(s) could easily be answered by searching the Internet with Google. Google is your friend Smilie

Per forum rules, and the benefit of all users, please search the network and the forums before posting a question.

You can easily search the forums using our internal Google search engine or our advanced internal search engine. You can also search our huge UNIX and Linux database by user generated tags or search the database for unanswered questions and provide an answer.

Thank you.

The UNIX and Linux Forums

===========================================================

Found with our internal Google search engine:

https://www.unix.com/unix-dummies-que...sult-grep.html

Previous Thread | Next Thread
Test Your Knowledge in Computers #474
Difficulty: Easy
Binary numbers are expressed in terms of a power of 2 instead of a power of 10.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 Replies

2. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

3. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

6. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

7. Shell Programming and Scripting

Search a pattern in a file with contents in a single line

Hi all I am searching for a pattern in a file . The file content is in a single line.If am doing a grep or sed for the a particular pattern am getting whole file. I want the result in different lines. attaching the file for reference search pattern "/xxxxxx/hhhh/tttttttt/sss/" and... (4 Replies)
Discussion started by: sparks
4 Replies

8. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

9. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

10. Shell Programming and Scripting

make multiple line containing a pattern into single line

I have the following data file. zz=aa azxc-1234 aa=aa zz=bb azxc-1234 bb=bb zz=cc azxc-1234 cc=cc zz=dd azxc-2345 dd=dd zz=ee azxc-2345 ee=ee zz=ff azxc-3456 ff=ff zz=gg azxc-4567 gg=gg zz=hh azxc-4567 hh=hh zz=ii azxc-4567 ii=ii I want to make 2nd field pattern matching multiple lines... (13 Replies)
Discussion started by: VTAWKVT
13 Replies

Featured Tech Videos