how to check for pattern in other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to check for pattern in other file
# 1  
Old 03-06-2007
how to check for pattern in other file

i hav file1
with contents:

welcome
this is first file
file name is file 1
last line of the file

now i want to check if the second line has sentence "this is first file" through
"if" statement from another file ...
is it possible if so how!!!
# 2  
Old 03-06-2007
Am not sure if i get ur question correctly.pls let me know else.

You can try like this.

if [[ $(grep -c "If you have the pattern" FILE ) -gt 0 ]] ; then
echo "Has the pattern"
fi
# 3  
Old 03-06-2007
try this:

Code:
sed '2s/"your search pattern/replacing patter/g" filename

# 4  
Old 03-06-2007
In ennstate's post, I believe -c option will just return the count of occurance of the pattern.But here we need to find out if the pattern is present in the 2nd line...[if my understanding is correct].

I thnk the below approch will work well...

[[ $(grep -n 'this is first file' new1 | grep '2:this is first file') ]] && echo "Pattern found"

Also, we can use awk...

awk '{if ((NR==2) && ($0 == "this is first file" )) print "Pattern present" }' new1
# 5  
Old 03-06-2007
this will return the line if the pattern is present in file new1 using sed
sed -n '/this is first file/{p;2q}' new1
# 6  
Old 03-07-2007
more appropritate could be to quit from line 3, Something like this:

Code:
sed -n '3q; /search pattern/p' filename

Please let me know if you think otherwise....
# 7  
Old 03-07-2007
Quote:
Originally Posted by suri
i hav file1
with contents:

welcome
this is first file
file name is file 1
last line of the file

now i want to check if the second line has sentence "this is first file" through
"if" statement from another file ...
is it possible if so how!!!
If it is always the second line that you need to check, this will be much faster than using an external program such as sed or awk:

Code:
{
 IFS= read -r LINE
 IFS= read -r LINE
} < FILENAME

case $LINE in
  "this is first file") true ;;
   *) false ;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check first specific string after a pattern

Hi, I've a file with content as below first_block_list{ a:5 b:3 c:8 } new_store_list( a:1000 b:200 c:3343 ) first_item_list{ a:10 b:20 c:30 } second_item_list{ (1 Reply)
Discussion started by: ratheeshp
1 Replies

2. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

3. Shell Programming and Scripting

How to check 2 log files for a common pattern?

hi! im new here and to unix. I want to do something with our log files. to compare two log files for a certain pattern. sample: file1.log contains all the "successful" run of a procedure. file2.log contains all the "current" running procedures. sample line from file1.log... (5 Replies)
Discussion started by: cabs_14
5 Replies

4. UNIX for Dummies Questions & Answers

Check for entry of specific pattern

Hey Guys and Gals, I am having trouble with what I thought shouldn't be hard.. In a script I am working on there is a need to enter a MAC address. MAC addresses are formatted ; XX:XX:XX:XX:XX:XX where X can be 0-9, a-f or A-F So in the sample script the query is something... (4 Replies)
Discussion started by: TAPE
4 Replies

5. Shell Programming and Scripting

Check for Pattern if exists write to file

Hi ! All I just want to search and write to new file if pattern is found in text file following are my text files by which I want to search Month and last column number my text file1 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25... (3 Replies)
Discussion started by: nex_asp
3 Replies

6. Shell Programming and Scripting

Unix file pattern check and replacement

HI Guys , Using UNIX ,I intend to check with correct file pattern Access_file_Record.YYYYMM in path /tmp If the file exist in correct format come out from code . If not found check with different file patterns for same month period YYYYMM ( Like Access_file_Record_YYYYMM.txt or... (8 Replies)
Discussion started by: Perlbaby
8 Replies

7. Shell Programming and Scripting

Check the file if the search pattern is there or not.

I want to check the files if the below pattern is there or not. I have to scan thousands of files. It should start search with "Group" (Case insensitive) and followed by first semicolon ";" . In between these two there should be "partition"(case insensitive). File1.txt a) update... (1 Reply)
Discussion started by: smolakal
1 Replies

8. Shell Programming and Scripting

check if file exists with pattern matching

Hello friends, I am writing a simple shell script which will copy one particular type of files to backup folder if files exists. If files doesn't exists, mv command should not be executed. My file pattern is like wcm-spider-maestro.log.2009-07-15, wcm-spider-maestro.log.2009-07-16 etc.. I... (6 Replies)
Discussion started by: sreenu.shell
6 Replies

9. Shell Programming and Scripting

Check for a pattern

Hi, I have a script where I supply a parameter externally. Now when the script picks up the parameter, I want to verify if that parameter has a pattern such as "tag" in it. I know that after reading the param the script can push it to a file and grep for the pattern, but I am looking for a... (2 Replies)
Discussion started by: sumeet
2 Replies

10. Shell Programming and Scripting

Check whether the pattern is present or not?

I want to determine whether a specific pattern is present within a line or not e.g. The whole line is in a varaible called VALUE VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)" i want to set a flag to 1 if i find the presence of ABC in the above variable. Please... (8 Replies)
Discussion started by: skyineyes
8 Replies
Login or Register to Ask a Question