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 ?
# 1  
Old 06-16-2014
How to check line existence in shell ?

Hi,

We have some config file and there we are looking to append a line if it is not found.
Code:
abc.conf
authpriv.*                                              /var/log/secure
mail.*                                                  -/var/log/maillog
*.debug  @vxhgt-hskhng02
cron.*                                                  /var/log/cron

Till now I have tried as below but no luck. Every time the line is appending to the file, even if the line is already present.
Note : there could be space or multiple tab between *.debug and @vxhgt-hskhng02. To handle the same I have used [:blank:] in my code.
Code:
PATTERN="*.debug[:blank:]@vxhgt-hskhng02" FILE=/etc/abc.conf if grep -q $PATTERN $FILE; then
echo "given pattern already found."

else
echo "pattern not found."
echo "*.debug  @vxhgt-hskhng02" >> /etc/abc.conf
fi

kindly suggest where I am going wrong here.

---------- Post updated at 04:09 PM ---------- Previous update was at 03:35 PM ----------

After searching in google....I got something like below
Code:
if grep -Fxq $PATTERN $FILE; 
then
echo "found"
else
echo "notfound"
fi

Still no luck.

Regards,
Litu

Last edited by Scrutinizer; 06-16-2014 at 09:14 AM.. Reason: additional code tags
# 2  
Old 06-16-2014
Try:
Code:
PATTERN= "\*\.debug[[:space:]]*\@vxhgt-hskhng02[[:space:]]*"

# 3  
Old 06-16-2014
Thanks for quick reply.
I tried your way...but no luck. It is still appending the entry even if it already present there.
# 4  
Old 06-16-2014
small modification to PATTERN makes it work:
Code:
PATTERN="*.debug[[:blank:]]*@vxhgt-hskhng02"
grep  $PATTERN file
*.debug  @vxhgt-hskhng02

This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-16-2014
Quote:
Originally Posted by chacko193
Try:
Code:
PATTERN= "\*\.debug[[:space:]]*\@vxhgt-hskhng02[[:space:]]*"

There is a typo in my last post. There should have been no space in the assignment operation.

Code:
# cat testIn
abc.conf
authpriv.*                                              /var/log/secure
mail.*                                                  -/var/log/maillog
*.debug  @vxhgt-hskhng02
cron.*                                                  /var/log/cron
# PATTERN="\*\.debug[[:space:]]*\@vxhgt-hskhng02[[:space:]]*"
# if grep -q $PATTERN testIn
> then
> echo "Pattern found"
> else
> echo "Pattern Not found"
> fi
Pattern found


Last edited by chacko193; 06-16-2014 at 09:26 AM.. Reason: typo
This User Gave Thanks to chacko193 For This Post:
# 6  
Old 06-16-2014
Try:
Code:
awk -v s='*.debug  @vxhgt-hskhng03' 'BEGIN{split(s,F)} 1; {for(i in F) if($i!=F[i]) next; f=1} END{if(!f) print s}' file

# 7  
Old 06-17-2014
Smilie Thanks for your help. It worked out for me.Smilie

---------- Post updated 06-17-14 at 11:38 AM ---------- Previous update was 06-16-14 at 07:51 PM ----------

I found one disconnect in my logic while appending the entry to the file.

If file conatins an entry for *debug @vxhgt-hskhng02 but it is commented, then we must append the entry to the file.

Code:
abc.conf

# *debug @vxhgt-hskhng02

In the above case we must append the entry.
Please suggest.
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