Help with listing file name containing particular text and count of lines with 10 characters.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with listing file name containing particular text and count of lines with 10 characters.
# 1  
Old 02-02-2015
Help with listing file name containing particular text and count of lines with 10 characters.

Hi,
I've 2 queries.

I need to list files which doesn't contain a particular text in the content. For example say, I need to list files which doesn't contain string "abc" from all files ending with *.bad. How can I do that?

Also, I want to display number of lines in a file which has atleast 10 characters in a line.

For Example: File1.txt
The Terminal
Blood Diamond
Gravity

Now the count of lines in File1.txt should return only 2.

Thanks in advance.

Regards.
# 2  
Old 02-02-2015
Hello Gangadhar Reddy,

Could you please try following code and let me know if this helps, I have used BASH and it worked fine for me.
Code:
ls -lhtr | awk -vs="abc" -vs1="'" -vs2="\"" '{if($NF ~ /.*bad/ && $NF !~ s){print "awk " s1 "{if(length($0)>=10){A++}} END{{if(A){print FILENAME " s2 " has " s2 " A " s2 " lines more than 10 characters"s2  ";A=0} else {print FILENAME " s2 " has 0 lines with 10 char or more " s2 "}}}" s1 " " $NF}}' | sh

I have made some some test files and it gave me following results.
Code:
test1.bad has 0 lines with 10 char or more
TEST.bad has 0 lines with 10 char or more
test2.bad has 4 lines more than 10 characters

Where I have created testabc.bad, test12abc.bad and they were NOT considered as per your requirements.

Hope this helps.

EDIT: Adding non oneliner form of solution.
Code:
ls -lhtr | awk -vs="abc" -vs1="'" -vs2="\"" '
                                                {if($NF ~ /.*bad/ && $NF !~ s){
                                                                                print "awk " s1 "{ \
                                                                                                        if(length($0)>=10){ \
                                                                                                                                A++ \
                                                                                                                          } \
                                                                                                 } \
                                             END{ \
                                                                              {if(A)             { \
                                                                                                        print FILENAME " s2 " has " s2 " A " s2 " lines more than 10 characters"s2  ";A=0 \
                                                                                                 } \
                                                                               else              { \
                                                                                                        print FILENAME " s2 " has 0 lines with 10 char or more " s2 " \
                                                                                                 } \
                                                                              } \
                                                                              }" s1 " " $NF
                                                }
                                                }
                                            ' | sh


Thanks,
R. Singh

Last edited by RavinderSingh13; 02-02-2015 at 02:22 AM.. Reason: Added a non oneliner form of solution
# 3  
Old 02-02-2015
First question: As you specify you want the content checked, not the file name itself, try
Code:
grep -lv abc *.bad

. Read man grep for further info.
Second qu.: Try
Code:
awk 'length >= 10 {CNT[FILENAME]++} END {for (c in CNT) print c, CNT[c]}' *.bad

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Programming

Listing Option Menu Choices from Text File

Hello, I am starting up a tool and one of the initial steps is to select a site/location which is being read from a text file. Here is the text file contents: site1 site2 site3 Here is the code: #!/usr/bin/python from Tkinter import * (3 Replies)
Discussion started by: tattoostreet
3 Replies

3. Shell Programming and Scripting

Checking a pattern in file and the count of characters

I am having a zipped file which has the following URL contents - 98.70.217.222 - - "GET /liveupdate-aka.symantec.com/1340071490jtun_nav2k8enn09m25.m25?h=abcdefgh HTTP/1.1" 200 159229484 "-" "hBU1OhDsPXknMepDBJNScBj4BQcmUz5TwAAAAA" "-" In this line here is we only need to consider the... (4 Replies)
Discussion started by: Naks_Sh10
4 Replies

4. Shell Programming and Scripting

Count number of free lines in a text file

Hi Everybody I want to write a script to count the number of lines in a file that don't ahve any thing on it, the free lines, i try to do it with fgrep "" which means to grep on the spaces but it does not work. help me please? (3 Replies)
Discussion started by: hard_revenge
3 Replies

5. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies

6. Shell Programming and Scripting

Include special system characters in file count

Hi, I have a script that checks the length of each record/line in file - This seems to be working when there are no special systems character that are invisible or hidden. awk -v file=$file '{ if (filename==file) { k+=$5 if (length() <= 10 ){print size length(), "bytes " k} }... (2 Replies)
Discussion started by: asemota
2 Replies

7. Shell Programming and Scripting

Adding text to file on certain lines with(special characters)

I need to add "new lines" of text with special characters, to specific lines in the file. There are 3 modifications needed. Been testing 2 here without success. #!/usr/bin/perl use FileHandle; $file=FileHandle->new; $FILENAME="/opt/etc/usr/file.txt"; $file->open ("<$FILENAME") or die... (13 Replies)
Discussion started by: A4ron4perl
13 Replies

8. Shell Programming and Scripting

awk help needed in trying to count lines,words and characters

Hello, i am trying to write a script file in awk which yields me the number of lines,characters and words, i checked it many many times but i am not able to find any mistake in it. Please tell me where i went wrong. BEGIN{ print "Filename Lines Words Chars\n" } { filename=filename + 1... (2 Replies)
Discussion started by: salman4u
2 Replies

9. UNIX for Dummies Questions & Answers

Listing full file names with the exact length of 3 characters

This is what I have to do: Display the full file name (including the full path) and file size of all files whose name (excluding the path) is exactly 3 characters long. This is the code I have: find / -printf "Name: %f Path: %h Size: %s (bytes)\n" 2>/dev/null | grep -E "Name: .{3,} Path" |... (7 Replies)
Discussion started by: Joesgrrrl
7 Replies

10. Shell Programming and Scripting

how to count characters by line of file ?

Hello, Member or professional need help how to count characters by line of file Example of the file is here cdr20080817164322811681txt cdr20080817164322811txt cdr20080817164322811683txt cdr20080817164322811684txt I want to count the characters by line of file . The output that I... (4 Replies)
Discussion started by: ooilinlove
4 Replies
Login or Register to Ask a Question