Solved

Perl/UNIX shell command

Posted on 2013-07-01
11
303 Views
Last Modified: 2013-11-27
Is there any perl/UNIX shell command (single command) that will take an input file and output this (count the concurrence of "myword" in a line):

myword 2
myword 3
myword 1

Input files:
This is myword and another myword
There is no myword
This is myword and another myword another myword
There is no myword
This is myword only
0
Comment
Question by:toooki
  • 2
  • 2
  • 2
  • +3
11 Comments
 
LVL 23

Assisted Solution

by:nemws1
nemws1 earned 100 total points
Comment Utility
I'm sure ozo will have a one-liner for this, but I prefer more readable code.  Put this into a script (I name mine 'countword.pl'):

#!/usr/bin/perl
my $target = shift(@ARGV);
while (<STDIN>) {
    my $count = () = $_=~m/$target/gi;
    print "$target $count\n";
}

Open in new window


Run with:
./countword.pl myword < your_text_file

Open in new window

0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 100 total points
Comment Utility
I'm not ozo but here's a one-liner:
perl -ne "$word = 'myword'; print $word, ' ', scalar(()=m{\b$word\b}g), \"\n\"" your_text_file

Open in new window

0
 
LVL 23

Expert Comment

by:nemws1
Comment Utility
Yeah, but you're a pretty darn good perl expert as well, wilcoxon. :)
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
There is always awk:

awk '{W="myword"; for(i=1;i<=NF;i++) if($i==W) c++; if(c>0) print W, c; c=0}' inputfile
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
A really ugly one, just for fun:

W="myword"; sed "s/$W/@/g" inputfile | grep -w '@' | while read line; do echo  -en "$W\t"; echo $line |tr -dc '@' |wc -c; done

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
Comment Utility
Given the input file:
This is myword and another myword
There is no myword
This is myword and another myword another myword
There is no myword
This is myword only

shouldn't the counts be
myword 2
myword 1
myword 3
myword 1
myword 1
?
Or, if we are counting /(?<!\bno )\bmyword\b/
would that be
myword 2
myword 0
myword 3
myword 0
myword 1
?
Or, if there is a "no" on the line, would we ignore the entire line, or just the "myword" immediately following the "no"?
What would you want the count to be for
This is myword and no myword
?
0
 

Author Comment

by:toooki
Comment Utility
I am sorry for incorrectly writing the question:

Given the input file:
This is myword and another myword
There is no m y w o r d
This is myword and another myword another myword
There is no my word
This is myword only

The output that I am looking for is:
myword 2
myword 3
myword 1
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
Comment Utility
perl -lne 'BEGIN{$w=shift}print "$w ".@F if @F=/\b\Q$w\E\b/g' 'myword'  your_text_file
0
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
My solution only needs a simple change to work with your modified requirements:
perl -ne '$word = "myword"; $c = scalar(()=m{\b$word\b}g); print $word, " ", $c, "\n" if $c' your_text_file

Open in new window

0
 
LVL 26

Accepted Solution

by:
skullnobrains earned 100 total points
Comment Utility
i like woolmilkporks idea, so here is a simpler one

sed -n "s/MYWORD/@/g" inputfile | tr -c -d "@\n" | while read line ; do echo -n 'MYWORD : ' ; expr "$line" : '.*' ; done

Open in new window


i'm assuming the file does not contain '@' but you can use something like £ or µ as well

can't figure out one without a while but there should be a way to make it even simpler
0
 

Author Comment

by:toooki
Comment Utility
Many thanks:
perl -lne 'BEGIN{$w=shift}print "$w ".@F if @F=/\b\Q$w\E\b/g' 'myword'  your_text_file
perl -ne '$word = "myword"; $c = scalar(()=m{\b$word\b}g); print $word, " ", $c, "\n" if $c' your_text_file

Both the above worked for me. Could you kindly let me know a tutorial where I could learn such parsing commands so that I can try myself on similar parsings?

Many thanks!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now