Number of Lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of Lines in a file
# 1  
Old 12-30-2015
IBM Number of Lines in a file

Hi All,

This is my Scenario:
I wanted to check if a particular name or pattern is present in a file based of that rest of the program should proceed.
I want to print '0' if no matching found.

Code:
v_File_Count=`grep -i "$v_Name_Pattern" $File_Path/Master_File_List.txt | wc -l`

The above command is giving an output where a tab space is present due to which I am not able to do a condition.

Code:
 
 if [[ $v_File_Count == 0]]; then
 xyz...
 fi

Tried to remove the "<Tab Space>" as below but no use Smilie
Code:
 v_File_Count=`grep -i "$v_Name_Pattern" $File_Path/Master_File_List.txt | wc -l | tr "   " ""`
  
  
 ## Below code is not print any value if there is no record -- I want '0' if no record
 v_File_Count=`grep -i "$v_Name_Pattern" $File_Path/Master_File_List.txt | awk '{print NR}'`
 v_File_Count=`grep -i "$v_Name_Pattern" $File_Path/Master_File_List.txt | sed -n '$='`



Sample Input:
File = Master_File_List.txt
Code:
 abcd.txt
 xyz.txt
 lmno.txt

Command:
Code:
v_File_Count=`grep -i "$v_Name_Pattern" $File_Path/Master_File_List.txt | sed -n '$='

** Anycode

Expected Output: ( If there are no matching pattern, 0 should be returned i.e., v_File_Count should have value '0')

Do share your ideas and suggestion

Last edited by Scrutinizer; 12-30-2015 at 09:07 AM.. Reason: quote tags -> code tags
# 2  
Old 12-30-2015
Removing tabs

perhaps try
Code:
tr -d '\t'

# 3  
Old 12-30-2015
Not working.

Code:
Code:
grep -i "avc" $DNA_PARM/Master_Graph_List.csv | wc -l | tr -d '\t'

Ouptut:
Quote:
0
Number of Lines in a file-errorjpg
# 4  
Old 12-30-2015
Hi, try:
Code:
grep -ic "avc" $DNA_PARM/Master_Graph_List.csv

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 12-30-2015
Quote:
Originally Posted by TechGyaann
I wanted to check if a particular name or pattern is present in a file based of that rest of the program should proceed.
I want to print '0' if no matching found.
These are actually two scenarios.

If you want to return the number of times a string is found within a file you take the "-c" ("count") option of grep:

Code:
# grep -c "text" /path/to/file_with_text_in.it
2

This would mean "text" is occurring in two lines of the file. To use it in a script:

Code:
iFoundNr=$(grep -c "<searchtext>" /path/to/file)

if [ $iFoundNr -eq 0 ] ; then
     echo "<searchtext>" was not found"
else
     echo "<searchtext> was found in $iFoundNr lines"
fi

But i suspect that you created the variable just because you weren't able to imagine a simpler way. Basically you want to do one thing if the text is found (however often) and another thing if not, right? Well, here it goes: the grep utility returns an "error level", which you can use. "0" means the search text was found, different than 0 means it was not found. Because in shell programming "0" equals to "true" and non-"0" equals to "false" you can use the "-q" (quiet) option of grep to suppress all output of it and directly write:

Code:
if grep -q "<searchtext>" /path/to/file ; then
     do_found
else
     do_notfound
done

You haven't asked the question, but an additional warning: when using variable contents as search patterns for grep (or similar tools: awk, sed, ...) take into account that some characters have a special meaning (they are so-called "meta-characters). The first of the following example will work as expected, the second one won't:

Code:
# search="abcd"         # this will work
# grep "$search" /path/to/file

# search="a[bc]d"       # this will work too, but not in the expected way
# grep "$search" /path/to/file

If you want to search for the string "a[bc]d" you need to "escape" metacharacters. To make the second example working (that is: searching for the fixed string "a[bc]d") you need to do that (notice the change in quoting too):

Code:
# search='a\[bc\]d'
# grep "$search" /path/to/file

I hope this helps.

bakunin

Last edited by bakunin; 12-30-2015 at 10:06 AM.. Reason: typo
This User Gave Thanks to bakunin For This Post:
# 6  
Old 12-31-2015
Thanks., OOO, I shall check and get back to you if further discussion/clarification required on this.

---------- Post updated 12-31-15 at 11:04 AM ---------- Previous update was 12-30-15 at 10:19 PM ----------

@ disedorgue @bakunin thanks, Working good.

@bakunin, additional information was helpful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Number of lines in file

How can I find the number of lines in a file excluding lines starting with #? (4 Replies)
Discussion started by: kristinu
4 Replies

2. UNIX Desktop Questions & Answers

How to display only the lines from a file which do not contain a given number

a. How do I display the content of the file containing what Ive merged using a filter which would display only the lines of the file which don't contain number, for example 3 or 6. (3 Replies)
Discussion started by: herberwz
3 Replies

3. Shell Programming and Scripting

How to read n number of lines from a file

Hiii I am very new to shell scripting.This is my data file a.txt: 56 45.78 1000 11.23 76.89 45 34.56 23 3400 100 .......... Now i am must use shell scripting to read n number of lines from the file & from ts n number of lines i need to find greatest number among them & so on for... (44 Replies)
Discussion started by: varsha
44 Replies

4. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

5. UNIX for Dummies Questions & Answers

Count of Number of Lines in a File

Dear Members, I want to count the number of lines in a file; for that i am using the following command : FILE_LINE_COUNT=`wc -l $INT_IN/$RAW_FILE_NAME` if i do an echo on FILE_LINE_COUNT then i get 241 /home/data/testfile.txt I don't want the directory path to be displayed. Variable... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

6. Shell Programming and Scripting

Displaying number of lines from file

Hi, I am using below command to display the number of line, but its returning no of lines along with file name. But i want only no of line in the variable p. Please help me on this? p=`wc -l "text file"` echo "$p" (6 Replies)
Discussion started by: shivanete
6 Replies

7. Shell Programming and Scripting

How to find number of lines in a file

Hi How do I find number of lines of a file? Below commands returned 0. But, the file is showing 20 lines when I open it in editplus tool. Each line contains 601 columns. Please advise I want to incorporate some word at the begining of each of those 20 lines -Somesh $ wc -l <... (2 Replies)
Discussion started by: somesh_p
2 Replies

8. Shell Programming and Scripting

total number of lines in a file

Hi , How about find the total number of lines in a file ? How can i do that with the "grep" command ? (9 Replies)
Discussion started by: Raynon
9 Replies

9. Shell Programming and Scripting

cut a number of lines out of a file

Hi, I have a text file contaning around 150 lines, each line is a hostname. I want to read 4 lines/hostnames and save those 4 lines to a seperate file. say the big file is /files/bigfile and I want to have a lot of files in /files named /files/smallfile.1 , /files/smallfile.2 and so on... ... (1 Reply)
Discussion started by: networkfre@k
1 Replies

10. UNIX for Dummies Questions & Answers

Need ls to show number of lines in each file

Hi, I need a command that would let ls show number of lines in each file rather than file size in KBs. I tried using wc -l as a source of input to ls but I found a problem cutting the file name since wc generates a space delimited list. Any suggestions? Thanks. GmMike. (1 Reply)
Discussion started by: GMMike
1 Replies
Login or Register to Ask a Question