Check content of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check content of file
# 15  
Old 11-23-2012
buddy
if i gave the code like this
Code:
if [ [ ! -s TCM_HC_ERR_Report.txt ] 
then
cat TCM_HC_ERR_Report.txt
else
echo "Empty or not exist"
fi

its throwing error<< test: unknown operator !>>>

if i gave the code like this
Code:
if [[ ! -s TCM_HC_ERR_Report.txt ]]
then
cat TCM_HC_ERR_Report.txt
else
echo "Empty or not exist"
fi

i m getting error like this..
cool.sh: [[: not found
Empty or not exist
but i have that file in that dirrectory....
pls help

Last edited by navsan; 11-23-2012 at 04:01 AM..
# 16  
Old 11-23-2012
If you are using sh you have to use:
Code:
if [ ! -s "TCM_HC_ERR_Report.txt" ]  
then    
   cat TCM_HC_ERR_Report.txt 
else    
    echo "Empty or not exist" 
fi

# 17  
Old 11-23-2012
thx fr quick reply...
My file(TCM_HC_ERR_Report.txt) has content but the output is showing 'Empty or not exist"
# 18  
Old 11-23-2012
Yes, you have to change the condition.
Code:
[ ! -s "TCM_HC_ERR_Report.txt" ] && echo "file empty" || cat TCM_HC_ERR_Report.txt

# 19  
Old 11-23-2012
Replace
Code:
if [ ! -s "TCM_HC_ERR_Report.txt" ]  
then    
   cat TCM_HC_ERR_Report.txt 
else    
    echo "Empty or not exist" 
fi

with
Code:
if [ -s TCM_HC_ERR_Report.txt ]  
then    
   cat TCM_HC_ERR_Report.txt 
else    
    echo Empty or not exist 
fi

.
The double-quotes aren't necessary when you are having a constant file name. However, these may be required if the file name has spaces,new-lines,etc.
This is assuming that you have a file by that name in the directory which is the working directory just before the test.

Last edited by elixir_sinari; 11-23-2012 at 05:14 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 20  
Old 11-23-2012
Quote:
Originally Posted by navsan
thx fr quick reply...
My file(TCM_HC_ERR_Report.txt) has content but the output is showing 'Empty or not exist"
Also, give the absolute path (from the root) for the file.
This User Gave Thanks to itkamaraj For This Post:
# 21  
Old 11-26-2012
can i get the code using While loop?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check specific content from log file

Hi all, i have a logfile which is continuously being updated. I have built a script to check for a specific content and if it is found, it sends a string into a file. Here's the current script: #!/bin/bash logfile=/opt/jboss-eap-6.3/standalone/log/server.log tail -fn0 $logfile | \... (7 Replies)
Discussion started by: nms
7 Replies

2. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

Check content

I would like to write a shell script to do the following , would advise how to make it ? very thanks 1. Connect LDAP server ( Id : user , password : pass ) 2. check the field "user_account_create_date" in the LDAP server , the format of data in this field in LDAP is ABC20130922 ( 22 Sep ,... (3 Replies)
Discussion started by: ust3
3 Replies

4. Shell Programming and Scripting

How to check the file size and content of the file

Hi, I have a text file in the below format. $cat my_paramater_file.txt ' $$ETL_EXTRACT_DATE=07/17/2011 17:55:05.000000 ' $ Would like to write script based on the below requirements. 1. The file should not be 0 byte. 2. The content of the file should be 4 lines and the... (1 Reply)
Discussion started by: Alex_Smith
1 Replies

5. AIX

check web content - Ksh

Hi everybody! I wonder if I could see some text content of a website, via command line... That could be very helpful, in order to automatize a lot of projects im working on. thx a lot (2 Replies)
Discussion started by: Datinsky
2 Replies

6. Shell Programming and Scripting

Script to Check & Edit Content of a file (Addition of comma in each lines of code)

Hi all, I need to write an automated bash shell script which performs such operations: 1. Grep the header of everyline with the initial of "T" in "FILE_A" 2. Perform a for loop, Count the numbers of comma in the line of code, if (no. of comma < 17) ADD the comma until 17; ... (2 Replies)
Discussion started by: big_nutz
2 Replies

7. Shell Programming and Scripting

Check file content type

I have a shell script that takes a file and uses "syncsort" to sort contents. I want to add a condition to check whether the input file is textual or binary format. If textual, the "syncsort" will be used to sort the files contents. Otherwise, the sorting process will be skipped. Note that the... (3 Replies)
Discussion started by: synthea
3 Replies

8. Shell Programming and Scripting

Bash : Check aphanumeric content in variable

Hello everyone, I'm trying the best way to implement a check on a variable ... in particular I need to assess the content of characters and numbers , I tried on various manuals bash scripting but I could not figure out how to do ... any help? (3 Replies)
Discussion started by: ionral
3 Replies

9. UNIX for Dummies Questions & Answers

check length content existence of variable

hi guys, im learning so be gentle... i'm wanting to write a script to read in a customer number. in order that the code is robust i want to check 1) the length of the value entered (4 characters) 2) that all characters entered are numeric between the values 1 to 3 3) that a value is... (1 Reply)
Discussion started by: skinnygav
1 Replies

10. Shell Programming and Scripting

To check the content of one file in another

Hi , I have a file called "X" . the content of X are X -- abc def and i have a file called "Y" , the content of Y are Y -- erty sdss s abc sfs def I need to check if the content of file X is contained in Y.Only unix (7 Replies)
Discussion started by: giri_luck
7 Replies
Login or Register to Ask a Question