How to check line existence in shell ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check line existence in shell ?
# 8  
Old 06-17-2014
Try:
Code:
PATTERN='*.debug @vxhgt-hskhng02'
awk -v p="$PATTERN" 'BEGIN{split(p,F)} 1; {for(i in F) if($i!=F[i] || $i~/^#/) next; f=1} END{if(!f) print p}' file


---
On Solaris use /usr/xpg4/bin/awk rather than awk

Last edited by Scrutinizer; 06-17-2014 at 03:31 AM..
# 9  
Old 06-17-2014
Hi Scrutinizer,

I appreciate your quick response.

As of now I have used grep command as mentioned below and it is working fine.

Code:
myfile
uucp,news.crit                                          /var/log/spooler
local7.*                                                /var/log/boot.log
*.debug         @vxhgt-hskhng02

Code:
#!/bin/bash
PATTERN="\*\.debug[[:blank:]]*\@vxhgt-hskhng02"
myfile=/etc/testfile
if grep -q $PATTERN $myfile
then
echo "pattern found"
else
echo "not found"
fi


In the same code I just want to add a logic to append the entry to the file, if the entry is present but it is commented.

We dont want to change the whole script again and use awk command. I hope you can understand mine point of view. Would you mind to suggest a solution that use grep command.

Regards,
Litu
# 10  
Old 06-17-2014
I understand. How about:
Code:
PATTERN="^[[:blank:]]*\*\.debug[[:blank:]]*@vxhgt-hskhng02"


Last edited by Scrutinizer; 06-17-2014 at 06:22 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 06-17-2014
While testing I found it is working. I have one doubt here ...how ^[[:blank:]]* is able read whether the line begins with # or not.

Code:
PATTERN="^[[:blank:]]*\*\.debug[[:blank:]]*\@vxhgt-hskhng02"

Can u just explain the command here.

Thanks,
Litu

Last edited by Scrutinizer; 06-17-2014 at 06:14 AM.. Reason: code tags
# 12  
Old 06-17-2014
It matches the pattern if preceded by only space ([[:blank:]]*) and only if that is at the beginning of the line (^). So if there is a comment character there, then there will be no match...

The old pattern that you were using wasn't right anyway, because if would have also matched something like tst*.debug @vxhgt-hskhng02 which is not right. Using ^[[:blank:]]* at the beginning fixes that as well (with the awk suggestion this precaution is not necessary since it uses string matching and discards leading space)..

Note that I removed the backslash before the @ since that is not necessary..
# 13  
Old 06-17-2014
Thanks for your nice explanation and the way you handle and fix one more bug in my old logic. Appreciate your timely and quick response. I will have couple of tests again in our prod and will update thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check the Files existence

Hi I have a requirement to check whether the files exists, then it will call other steps in shell script. I did ls *.csv|wc -l if then checking the count of the files should be more than 1 then it will call other steps. I am getting the error that too many arguements as there n... (13 Replies)
Discussion started by: cnrj
13 Replies

2. Shell Programming and Scripting

one line command to check file existence and FTP it

Hi all, I need a batch script to Check for existence of file say i check for files with extension xml.done in C:\Myfile.(This folder contains .xml file and its corresponding .done files) If the .done file exists then it should FTP the corresponding .xml file to UNIX. Is this possible to do... (6 Replies)
Discussion started by: Codesearcher
6 Replies

3. Shell Programming and Scripting

check existence of the path

Hi How can I check if the path exist or not? echo "Enter path:"; read my_path; ##I should check whether my_path exists or not.... (5 Replies)
Discussion started by: tjay83
5 Replies

4. Shell Programming and Scripting

shell :: check directory existence

Hi All, I have shell script and I need to check if some directory exist. I'm don't have the information if that directory is written in upper case or lowcase or mixed. Is there anyway to check the existence of that directory by ignoring case senestive? Thanks (3 Replies)
Discussion started by: Alalush
3 Replies

5. AIX

Check for File Existence

I have requirement where i need to search for files which start with SALESORDER and PURCHASEORDER. i need to process the files with SALESORDER first and then PURCHASEORDER. If SALESORDER files are not there i dont want to process PURCHASEORDER and i want to come out of script. I have written a code... (4 Replies)
Discussion started by: dsdev_123
4 Replies

6. AIX

check for file existence

Hello I am having a requirement like if there is no file in the directory then i need a message to pop on after the execution of the script. My script basically does for File in `ls -t $DIRECTORY | tail -1`; if there is no file the DIRECTORY then the script is simply exiting with out... (2 Replies)
Discussion started by: dsdev_123
2 Replies

7. AIX

how to check the existence of a file using korn shell?

we have tranferred an ear from local server to remote server using ftp.consider, we have an ear file named a.ear in remote server,again if we transfer the same file named a.ear from local server to remote server.we need the kshell to check the existence of the ear file in remote server,and if the... (3 Replies)
Discussion started by: karthikprasathk
3 Replies

8. AIX

how to check the existence of a file during ftp using korn shell?

i can able to transfer a file from build server(AIX)to webserver using ksh through ftp.my query is to check the existence of file while transfering from one server to other .i.e i need some command or script that checks the existence of file with same name in both server,within ftp syntax. ... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

9. UNIX for Advanced & Expert Users

Check existence of a login

Hi everybody, I need to check in C program wether a given login is known on the system. Is there any system function that could do this ? So far, all I could find is getpwnam(), which answers my problem by parsing the local password database. But won't work if a user is authenticated by... (10 Replies)
Discussion started by: xavier054
10 Replies

10. Solaris

How to check the file existence using shell scripting in Solaris-10

Hi, I have a script which will check the fiel existence, the lines are as below if !(test -d ./data) then mkdir data fi In the first line error occurs as below generatelicense.sh: syntax error at line 2: `!' unexpected Where as this script works fine in linux OS. How to solve... (2 Replies)
Discussion started by: krevathi1912
2 Replies
Login or Register to Ask a Question