how to find a particular string from a set of string output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find a particular string from a set of string output
# 1  
Old 08-17-2009
how to find a particular string from a set of string output

Hi ,

I have a line by line output as follows,for example output of ls

sample1
sample2
sample

i need to check if this output contains the exact string sample.If i use grep , it will find out all strings that contain sample as a part of their word.I dont want to do a pattern matching here but check if one of the line is equal to "sample" (a string comparison).How can i achieve this in shell scripting?

Thanks,
Padmini
# 2  
Old 08-17-2009
uh, so maybe
Code:
ls | grep -e "^sample$"

?
andom
# 3  
Old 08-17-2009
Try this,



Code:
 

cat filename | while read input

do 

 if [[ "$input" = "string to be searched" ]]

 then

     #do some action if string match

 else

    # do some action if string does not match

 fi

done

# 4  
Old 08-17-2009
Quote:
Originally Posted by padmisri
I dont want to do a pattern matching here but check if one of the line is equal to "sample" (a string comparison).
Code:
# grep --version
grep (GNU grep) 2.5.1-FreeBSD

#grep -w sample file

# 5  
Old 08-17-2009
Hi,

what is "^sample$" ? am not able to understand this.
# 6  
Old 08-17-2009
That's a regex, it tells:
Code:
^       #matches the null string at the beginning of a line
sample
$       #matches the null string at the end of a line

literally, search sample, nothing else.
# 7  
Old 08-19-2009
Hi,

But am not getting the correct output if i use ls | grep "^sample$"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find all files containing string not following symlinks CAT (modified) output content to /filename

This should recursively walk through all dirictories and search for a specified string in all present files, if found output manicured content (eg some regex) with CAT into a specified directory (eg /tmp/) one by one, keeping the original names This is what I have so far, which seems to... (1 Reply)
Discussion started by: lowmaster
1 Replies

2. Shell Programming and Scripting

How to find previous string based on an input string?

Hi, I did some research but cannot find the right solution so hopefully someone can help me here. I have a long string format like: VAR=111:aaaa,222:bbb,333:ccc it could be VAR=111:aaa,222:bbb,333:ccc,444:ddd, etc what I looking for is eg. if I give ccc, it will return me 333... (2 Replies)
Discussion started by: netbanker
2 Replies

3. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

4. Shell Programming and Scripting

Regex find first 5-7 occurrences of a set of digits within a string

Using these strings as an example: <a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=145148&amp;playTogether=True',960,540,943437);return false;" title=""> <a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=1451486&amp;playTogether=True',960,540,94343);return false;" title=""> <a... (12 Replies)
Discussion started by: metallica1973
12 Replies

5. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

6. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

7. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

8. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

9. Shell Programming and Scripting

Search and find the string and output to a new file.

Hi I want to search the following in the entire file and put them in a diff file. file 1 contains : Non error probabilities are not decreasing in curve 1GEORGE_SGD_SUB Mid Point RESCAP_SGD_SNU curve have errors Non default probabilities are not decreasing in curve ABF_JPY_SUB Mid Point... (3 Replies)
Discussion started by: szchmaltz
3 Replies

10. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies
Login or Register to Ask a Question