To take line count from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To take line count from a file
# 1  
Old 01-03-2014
To take line count from a file

Hi guys,

I am having a file which where i need to take line count based on searching a particular string from a list say list_file.txt which occurs in 2nd column of my main file and to take the line count which doesnot exist in list file say list_file.txt

for eg: my main file looks like this
Code:
1|US|xx|yy
2|AUS|yy|zz
11|NZ|aaa|qqq
12|UK|aa|qq
12|AUS|yyy|z11z
.
.
.

i am having a list file say list_file

Code:
AUS
NZ

i need the out put of line count of mail file like this output_file
Code:
AUS - 2
NZ -1
REST - 2


I have tried something like this

Code:
grep NZ main_file|wc -l
grep AUS mail_file|wc -l

but struck for rest of which is not present in list_file.txt
# 2  
Old 01-03-2014
hope this 'll help work for you
Code:
 cut -d'|' -f2 file |sort | uniq -c

--Lohit
# 3  
Old 01-03-2014
Here are a couple of other ways to get what you requested:
Code:
Echo "Using shell while read loop:"
rest=$(wc -l < main)
while read cc
do      ccc=$(grep '|'"$cc"'|' main|wc -l)
        printf "%s - %d\n" "$cc" $ccc
        rest=$(($rest - $ccc))
done < list_file
printf "REST - %d\n" $rest
printf "\nUsing awk script:\n"
awk -F'|' '
FNR == NR {
        ccc[$2]++
        rest++
        next
}
{       printf("%s - %d\n", $1, ccc[$1])
        rest -= ccc[$1]
}
END {   printf("REST - %d\n", rest)
}' main list_file

This should work with any POSIX-conforming shell (e.g., ksh or bash). If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
The output from this script with your two sample files as input is:
Code:
Using shell while read loop:
AUS - 2
NZ - 1
REST - 2

Using awk script:
AUS - 2
NZ - 1
REST - 2

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-03-2014
Code:
awk -F'|' 'FNR==NR{a[$0];next} {if ($2 in a) a[$2]++; else a["REST"]++} END { for(i in a) print i, a[i]}' list_file main OFS=' - '

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 01-04-2014
if you have Ruby
Code:
# get list_file to array
list_file  = File.open("list_file").readlines.map(&:chomp)

line_count = Hash.new { |h,k| h[k] = 0 }  # create dictionary to store counts   
f          = File.new("main_file")
f.each  do |line|
  tag = line.split("|")[1]                # get the tag
  if list_file.include? tag               # if inside line_count array
     line_count[ tag ] += 1               # increment
  end  
end
f.close                                   # file close
line_count.each_pair do |x,y|
  puts "#{x} : #{y}"                      # display result
end

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

2. Shell Programming and Scripting

Perl - line count of a file

Hi, I am quite new to perl scripting. I have a dat file (datFile) from which I am pulling only first column and saving the output to a new file (f). From that file (f) I am removing blank lines and saving it to new file (datFile1). I am able to get the count of $f file in variable $cnt. But... (4 Replies)
Discussion started by: Neethu
4 Replies

3. Shell Programming and Scripting

FASTEN count line of dat file and compare with the CTRL file

Hi All, I thinking on how to accelerate the speed on calculate the dat file against the number of records CTRL file. There are about 300 to 400 folder directories that contains both DAT and CTL files. DAT contain all the flat files records CTL is the reference check file for the... (3 Replies)
Discussion started by: ckwan
3 Replies

4. Shell Programming and Scripting

Split csv file by line count.

I have a very large csv file that I sort by the data that is in the second column. But what I need to do next is split the file in groups of say around 30,000 lines but don't split the data while there is still like data in the in the second column. Here is some of the data. ... (2 Replies)
Discussion started by: GroveTuckey
2 Replies

5. UNIX for Advanced & Expert Users

How to take file line count in AIX

HI All, I am trying to take file line count in UNIX and AIX Unix command to take file line count-- working fine count=`wc -l /apps/hgford/sorted/E.testing.DLY|cut -f1 -d " "` 1355Same command when run in aix --having issue count=`wc -l /apps/hgford/sorted/E.testing.DLY|cut -f1 -d " "`... (5 Replies)
Discussion started by: Perlbaby
5 Replies

6. Shell Programming and Scripting

Count per line in txt file

In a txt file called, eso.txt, I have: ...... 3 where process_status_flag = 70 and LISTENER_ID in (930.00, 931.00, 932.00, 933.00, 934.00) 4 group by LISTENER_ID 5 order by LISTENER_ID; LISTENER COUNT ----------... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

7. Shell Programming and Scripting

appending the count of line in each file at head of each file

hello everybody, I have some files in directory.each file contain some data. my requirement is add the count of each line of file in head of each file. any advice !!!!!!!! (4 Replies)
Discussion started by: abhigrkist
4 Replies

8. Shell Programming and Scripting

File Line Count

Hi, Came across a weird problem today. I was just trying to write this small script which would read the number of lines in a file. Depending on the count some further processing would be done. I used wc -l in order to get that done. But since it depends on the number of new line characters, if... (1 Reply)
Discussion started by: King Nothing
1 Replies

9. 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

10. Shell Programming and Scripting

Get the line count from 2nd line of the file ?

Hi, I want to get the line count of the file from the 2nd line of the file ? The first line is header so want to skip that. Thanks. (8 Replies)
Discussion started by: smc3
8 Replies
Login or Register to Ask a Question