Testing for one word in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Testing for one word in a file
# 1  
Old 12-18-2012
Testing for one word in a file

I am trying to test the output of a file. What I have so far is this:

Code:
if [[ `cat /tmp/filex` == "space - Compacted" ]]; then echo "yes";fi

The problem with this is it works but I only want it to print out if the file contains the word "Compacted." The == sign means identical or equal to so it won't work. I tried ~ but that doesn't work. Is there another option I am missing?

Any help is appreciated!

Last edited by Scott; 12-18-2012 at 07:10 PM.. Reason: Code tags, please...
# 2  
Old 12-18-2012
Why don't you use grep?
Code:
if [ $( grep -c "Compacted" /tmp/filex ) -ne 0 ]
then
      echo "Yes"
fi

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-18-2012
You're right

I got hung up on the file's contents rather than scanning for this one word! Thanks! It works. If you do know though just for additional information, if there is a character similar to ~ that I could have used in that test command that would be great. If not, or if there is no such character it does not matter; the code works all the same
# 4  
Old 12-18-2012
Using '~' to search for string with awk:

Code:
awk '{if ($0 ~ /Compacted/) print "yes" }' /tmp/filex

This User Gave Thanks to mjf For This Post:
# 5  
Old 12-19-2012
Can be written like this
Code:
awk '/Compacted/ {print "yes"}' /tmp/filex

This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. UNIX for Dummies Questions & Answers

File and directory testing

original post -- I have a korn shell script that does some things that depend on creating and writing a file in a directory. I'm looking for a more elegant/efficient way to do the check than what I'm using now: if ] then print "Creating ${STGDIR}/${SHOW}" mkdir... (3 Replies)
Discussion started by: Dalej
3 Replies

5. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

6. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies

7. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

8. Shell Programming and Scripting

testing file permissions.....

script name: filetest.sh if ; then echo " You didn't enter any argument" elif ; then echo " file not exist" elif ; then echo " file not readable" elif ; then echo " file not writable" else echo " file both readable and writable" fi running like... $ ./filetest filename ... (3 Replies)
Discussion started by: ani83_pune
3 Replies

9. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question