Sponsored Content
Top Forums UNIX for Dummies Questions & Answers counting a list of string in a list of txt files Post 302225006 by drl on Thursday 14th of August 2008 10:16:10 AM
Old 08-14-2008
Hi.

Most versions of grep can handle a file of patterns, so that standard *nix utlities can be used:
Code:
#!/bin/bash -

# @(#) s3       Demonstrate string count total from files.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) grep sort uniq
set -o nounset
echo

echo " strings file:"
cat strings

echo
echo " data files" data* ":"
cat -n data*

echo
echo " Results:"
grep -h -f strings data* |
sort |
uniq -c

exit 0

Producing:
Code:
% ./s3

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
grep (GNU grep) 2.5.1
sort (coreutils) 5.2.1
uniq (coreutils) 5.2.1

 strings file:
dog
horse
cat

 data files data1 data2 data3 data4 :
     1  File 1
     2  monkey
     3  cat
     4  dog
     5  dog
     6  File 2
     7  horse
     8  sawhorse
     9  Files 3
    10  cat
    11  horse
    12  witch
    13  seven
    14  File 4
    15  spider
    16  hoarse
    17  horse
    18  horse
    19  horse
    20  cat

 Results:
      3 cat
      2 dog
      5 horse
      1 sawhorse

The files are filtered for the lines that contain strings of interest. Then, in order to count with uniq, we need to sort the result.

If you need better filtering, you may need to change the patterns in the strings file, or -- in some versions of grep -- use the "word" option "-w".

Adjust as necessary for your environment according to your man pages ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to List and copy the files containing a string

:confused: I have more than 8000 files in a dir, I need to copy to other dir which containing the "sample" I tried grep -il "1189609240791-1268115603299237276@216.109.111.119 ' | cp /tmp/inv Nothing is happening for long time for 100 file dir too, Any one can help me? (11 Replies)
Discussion started by: redlotus72
11 Replies

2. Solaris

list files .txt and .TXT in one command

Dear experts, In a directory i have both *.TXT and *.txt files. I have a script- for file in `ls *.txt`; do mv $file /tmp/$file How to list both *.txt and*.TXT file in one command so that script will move both .txt or .TXT whatever it find. br//purple (4 Replies)
Discussion started by: thepurple
4 Replies

3. Shell Programming and Scripting

command to list .txt and .TXT file

Hi expersts, in my directory i have *.txt and *.TXT and *.TXT.log, *.txt.log I want list only .txt and .TXT files in one command... how to ?? //purple (1 Reply)
Discussion started by: thepurple
1 Replies

4. UNIX for Dummies Questions & Answers

List all files except *.txt in a directory

I have many types of files (Eg: *.log, *.rpt, *.txt, *.dat) in a directory. I want to display all file types except *.txt. What is the command to display all files except "*.txt" (9 Replies)
Discussion started by: apsprabhu
9 Replies

5. Shell Programming and Scripting

List the files with string

I want to list all the files which are having today's date in its header... Please let me know if this can be achveived by a single command (8 Replies)
Discussion started by: anandapani
8 Replies

6. Solaris

list of files in a txt file from sftp location

I want equivalent of ftp in sftp for listing of files into local machine from sftp location. ftp>ls -l list.txt the above creates a file list.txt in the local machine's current directory. sftp>ls -l list.txt it is giving Couldn't stat remote file: No such file or directory is there... (1 Reply)
Discussion started by: megh
1 Replies

7. Shell Programming and Scripting

Script to list files not present in audio.txt file

I am having following folder structure. /root/audios/pop /root/audios/jazz /root/audios/rock Inside those pop, jazz, rock folders there are following files, p1.ul, p2.ul, p3.ul, j1.ul, j2.ul, j3.ul, r1.ul, r2.ul, r3.ul And I have a file named as "audio.txt" in the path /root/audios,... (11 Replies)
Discussion started by: gopikrish81
11 Replies

8. UNIX for Dummies Questions & Answers

Creating a column based list from a string list

I have a string containing fields separated by space Example set sr="Fred Ted Joe Peter Paul Jean Chris Tim Tex" and want to display it in a column format, for example to a maximum of a window of 100 characters And hopefully display some thing like Fred Ted Joe ... (3 Replies)
Discussion started by: kristinu
3 Replies

9. Shell Programming and Scripting

How to list the files having particular string in it ?

I want to list the name of files with path having perticular string in it. search must be start from root (/) dir. Thanks:) (3 Replies)
Discussion started by: anandgodse
3 Replies

10. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies
PHAR.EXTRACTTO(3)							 1							 PHAR.EXTRACTTO(3)

Phar::extractTo - Extract the contents of a phar archive to a directory

SYNOPSIS
public bool Phar::extractTo (string $pathto, [string|array $files], [bool $overwrite = false]) DESCRIPTION
Note This method requires the php.ini setting phar.readonly to be set to 0 in order to work for Phar objects. Otherwise, a PharException will be thrown. Extract all files within a phar archive to disk. Extracted files and directories preserve permissions as stored in the archive. The optional parameters allow optional control over which files are extracted, and whether existing files on disk can be overwritten. The sec- ond parameter files can be either the name of a file or directory to extract, or an array of names of files and directories to extract. By default, this method will not overwrite existing files, the third parameter can be set to true to enable overwriting of files. This method is similar to ZipArchive.extractTo(3). PARAMETERS
o $pathto - Path within an archive to the file to delete. o $files - The name of a file or directory to extract, or an array of files/directories to extract o $overwrite - Set to TRUE to enable overwriting existing files RETURN VALUES
returns TRUE on success, but it is better to check for thrown exception, and assume success if none is thrown. ERRORS
/EXCEPTIONS Throws PharException if errors occur while flushing changes to disk. EXAMPLES
Example #1 A Phar.extractTo(3) example <?php try { $phar = new Phar('myphar.phar'); $phar->extractTo('/full/path'); // extract all files $phar->extractTo('/another/path', 'file.txt'); // extract only file.txt $phar->extractTo('/this/path', array('file1.txt', 'file2.txt')); // extract 2 files only $phar->extractTo('/third/path', null, true); // extract all files, and overwrite } catch (Exception $e) { // handle errors } ?> SEE ALSO
PharData.extractTo(3). PHP Documentation Group PHAR.EXTRACTTO(3)
All times are GMT -4. The time now is 07:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy