Sponsored Content
Operating Systems Linux To find multiple strings count in a file Post 302147284 by drl on Monday 26th of November 2007 07:49:35 AM
Old 11-26-2007
Hi, salaathi.

You didn't say anything about the input file. Here's a script that allows a general input file. The file is broken apart, sorted, counted, and searched according to your list:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate counting strings in arbitrary text file.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash tr sort uniq grep

echo

FILE=data1
STR=strings1

echo " Input file:"
cat data1

echo
echo " Results of carving, sorting, counting, searching pipeline:"
tr ' ' '\n' <$FILE |
sort |
uniq -c |
grep -f $STR

exit 0

The input file has a number of your words scattered through a file of randomly generated strings. The script then produces:
Code:
% ./s1

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

 Input file:
Ekinsud myel irradiate persona chufte spumsuds diagnosable
esplanade dregh.  Minoan victor ighent intellectual otem
ensodsil.  Swibsik exclusion cereal transpacific loin florican
flagellate charm snick.  bulkftp cctuneftp cso bulkftp Adoptive
launch marrow gridiron heredity sulfa cultivable mutate sterile
partisan chamois.  Testamentary miraculous otsonsev silverware
cso bulkftp cctuneftpd meridian?  Scev quarterback hare hood
spectral adrift bourbon bulkftp cctuneftp cso cctuneftp
employed.  Doctrine win benevolent ethic plumbago trailhead men
Mbabane diagnoses incorrect Claire.  Assess yoke summate variate
Freddy fluid Rwanda confine Albany southbound origin.  Keel
Chapman prep blueback thirteen Halpern.  Piss affricate AZ
fanfold Lebanese rollick topocentric butte honorific pretext cso
cso bedevil?  Ers douce Wilhelm deniable seater scherzo.

 Results of carving, sorting, counting, searching pipeline:
      4 bulkftp
      3 cctuneftp
      1 cctuneftpd
      5 cso

If your input file does not need to be broken up, then you can eliminate the first step, the tr. See man pages for details ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages. I have used below command but it is not working grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt ... (5 Replies)
Discussion started by: sushmab82
5 Replies

2. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

3. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

4. Programming

Perl:how to find multiple strings

Hi , i'm trying to find lines in a file that have 2 matches,but i have trouble with regex -it doesn't search for a second string-please help #!/usr/bin/perl -w use strict; use warnings; my $log = "log"; my $log_string = "grep '^8=' $log |"; usage (); chomp (my $TAB_1 = $ARGV); chomp (my... (2 Replies)
Discussion started by: auto_w
2 Replies

5. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

6. Shell Programming and Scripting

How to find multiple strings within files under a directory

Hi guys, I need to find multiple strings within files under a directory and secondly, to count how many files are there with these strings. At present, i am able to do this in order to find one string (for example "abc"): find <path> -exec grep "abc" {} /dev/null \; Now for example, i... (2 Replies)
Discussion started by: frum
2 Replies

7. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

8. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

9. Solaris

How to find multiple strings on different lines in file?

Hello, I have spent considerable amount of time breaking my head on this and reached out here. here is the back ground. OS - Solaris 10 There are two strings '<Orin>sop' and '<Dup>two' which I wanted to look for in a file without the quotes on different lines and ONLY if both strings are... (5 Replies)
Discussion started by: keithTait309875
5 Replies

10. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies
STRVERSCMP(3)						     Linux Programmer's Manual						     STRVERSCMP(3)

NAME
strverscmp - compare two version strings SYNOPSIS
#define _GNU_SOURCE #include <string.h> int strverscmp(const char *s1, const char *s2); DESCRIPTION
Often one has files jan1, jan2, ..., jan9, jan10, ... and it feels wrong when ls orders them jan1, jan10, ..., jan2, ..., jan9. In order to rectify this, GNU introduced the -v option to ls(1), which is implemented using versionsort(3), which again uses strverscmp. Thus, the task of strverscmp is to compare two strings and find the "right" order, while strcmp only finds the lexicographic order. This function does not use the locale category LC_COLLATE, so is meant mostly for situations where the strings are expected to be in ASCII. What this function does is the following. If both strings are equal, return 0. Otherwise find the position between two bytes with the property that before it both strings are equal, while directly after it there is a difference. Find the largest consecutive digit strings containing (or starting at, or ending at) this position. If one or both of these is empty, then return what strcmp would have returned (numerical ordering of byte values). Otherwise, compare both digit strings numerically, where digit strings with one or more leading zeroes are interpreted as if they have a decimal point in front (so that in particular digit strings with more leading zeroes come before digit strings with fewer leading zeroes). Thus, the ordering is 000, 00, 01, 010, 09, 0, 1, 9, 10. RETURN VALUE
The strverscmp() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be earlier than, equal to, or later than s2. CONFORMING TO
This function is a GNU extension. SEE ALSO
rename(1), strcasecmp(3), strcmp(3), strcoll(3) GNU
2001-12-19 STRVERSCMP(3)
All times are GMT -4. The time now is 07:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy