counting lines and showing the output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers counting lines and showing the output
# 1  
Old 08-02-2004
Error counting lines and showing the output

First time poster -

I have a huge file and i want to sort and compress it to something more readable

Ex:
FUTNCA01-SL1 DMT8a4 5 3
FUTNCA01-SL1 DMT8a4 5 9
FUTNCA01-SL1 DMT8a4 5 21
FUTNCA01-SL1 DMT8a4 5 22
FUTNCA01-SL1 DMT8a4 5 23
FUTNCA01-SL1 DMT8a4 5 24
FUTNCA01-SL1 DMT8a4 6 2
FUTNCA01-SL1 DMT8a4 6 3
FUTNCA01-SL1 DMT8a4 6 7
ALBQNMMA-SL1 DMT8a4 3 1
ALBQNMMA-SL1 DMT8a4 3 2
ALBQNMMA-SL1 DMT8a4 3 3
ALBQNMMA-SL1 DMT8a4 3 4
ALBQNMMA-SL1 DMT8a4 12 7


Output 1 desired
FUTNCA01-SL1 DMT8a4 5 6 ---> count of 2nd to the last field
FUTNCA01-SL1 DMT8a4 6 2 --> count of last field
ALBQNMMA-SL1 DMT8a4 3 4
ALBQNMMA-SL1 DMT8a4 12 1

Output 2 desired
FUTNCA01-SL1 DMT8a4 8 --> total number lines
ALBQNMMA-SL1 DMT8a4 5

Last edited by jjoves; 08-02-2004 at 02:40 PM..
# 2  
Old 08-02-2004
See the man page for the sort and grep or egrep commands.

When grepping for the 2nd to last column, use quotes.

$ grep -c "FUTNCA01-SL1 DMT8a4 5" /your-file-name

Last edited by RTM; 08-02-2004 at 04:12 PM..
# 3  
Old 08-02-2004
I think i have to rephrase my question:

Input File
Field1 - Field2 - Field3 - Field4
A - B - C - 1
A - B - C - 2
A - B - C - 3
A - B - C - 4
A - B - C - 5
A - B - D - 1
A - B - D - 2
A - B - D - 3
A - B - D - 4
E - F - G - 1
E - F - G - 2

Output file
A - B - C -> 5 --- Since the third field appeared 5 times
A - B - D -> 4
E - F - G - > 2

My sort depends on the field - each field changes.

THANKS ALOT
# 4  
Old 08-02-2004
That was posted as one example.

You would have to grep for each individual string

grep -c "A - B - C"
grep -c "A - B - D"
grep -c "E - F - G"
# 5  
Old 08-02-2004
Assuming I got what you wanted ---
Code:
#!/bin/ksh
# assuming the file in question, test.dat,  is already sorted 
# adjust the columns to cut the 1-10 part of the cut statement

old_compare=`head -1 test.dat | cut -c 1-10`
let old_count=0
while read record
do   
     tmp=`echo $record | cut -c 1-10`
     if [ "$old_compare" = "$tmp" ] ; then
            let old_count=old_count+1
     else
            echo "$old_compare $old_count"
            let old_count=1
            old_compare="$tmp"   
     fi
done < test.dat                           
echo "$old_compare $old_count"
exit

# 6  
Old 08-02-2004
Here is an example of my input file:
===============
PLALCA12-SL2 2 1
PLALCA12-SL2 2 2
PLALCA12-SL2 2 3
PLALCA12-SL2 2 4
PLALCA12-SL2 2 5
PLALCA12-SL2 2 6
PLALCA12-SL2 2 7
PLALCA12-SL2 2 8
PLALCA12-SL2 2 9
PLALCA12-SL2 2 18

BDBKNJBD-SL1 2 1
BDBKNJBD-SL1 2 2
BDBKNJBD-SL1 2 3
BDBKNJBD-SL1 2 4
BDBKNJBD-SL1 2 5
BDBKNJBD-SL1 2 6
BDBKNJBD-SL1 2 7
BDBKNJBD-SL1 2 8

KHVLPAKU-SL1 2 1
KHVLPAKU-SL1 2 2
KHVLPAKU-SL1 2 3

PRVDRIWA-SL2 8 13
PRVDRIWA-SL2 8 14
PRVDRIWA-SL2 8 15
PRVDRIWA-SL2 8 16
PRVDRIWA-SL2 9 7
PRVDRIWA-SL2 10 7
PRVDRIWA-SL2 10 8
PRVDRIWA-SL2 11 7
PRVDRIWA-SL2 11 8
PRVDRIWA-SL2 12 13
PRVDRIWA-SL2 12 14
================

And here is the output that I need

PLALCA12-SL2 2 18 --> since there is 18 lines
BDBKNJBD-SL1 2 8
KHVLPAKU-SL1 2 3
PRVDRIWA-SL2 8 4
PRVDRIWA-SL2 9 1
PRVDRIWA-SL2 10 2
PRVDRIWA-SL2 11 2
PRVDRIWA-SL2 12 2

THANKS for all the help...
# 7  
Old 08-02-2004
Assuming that I fully understand the question,

Code:
#!/bin/ksh

# The first two fields, space seperated, 
# form the unique identifier, correct?

TMPFILE=./tmpfile
awk -v t=$TMPFILE '{print $1 " " $2 >> t}' infile

sort $TMPFILE | uniq | while read id
do
   echo "$id $( grep -c "$id" infile )"
done

rm $TMPFILE

exit 0

is the script for you.

Cheers
ZB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pgrep not showing desired output

I am searching for a process that should be up and running. Im using the following command ps -ef | grep elasticsearch to get elastic+ 1673 1 0 Jan29 ? 05:08:56 /bin/java -Xms4g -Xmx4g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC... (1 Reply)
Discussion started by: Junaid Subhani
1 Replies

2. Shell Programming and Scripting

Script showing incorrect output

Hello scripting geeks, I am new to scripting and facing some issues in writing the logic of the script. Request your kind help here Actually when i run a command i get o/p as below o/p : 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 these are hex values i guess...now i want to... (15 Replies)
Discussion started by: kulvant29
15 Replies

3. UNIX for Dummies Questions & Answers

Command showing no output!

Hi Folks, I have a situation here, where no command is giving any output, and it's not even showing any error message also. What could be the reason? (3 Replies)
Discussion started by: nixhead
3 Replies

4. Shell Programming and Scripting

Now showing the correct output

Hello I am working on one script where I am trying to display all the directories which is inside the workspace but somehow it is giving me weird output and this is occurring only with one directory other also having the result.html file inside the directory. for i in `ls -1 | egrep -iv... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

5. Solaris

Showing strange size in df output

Hi, This is Solaris-10 box and in few of file-system (root file-system of non global zones), usage/available is not showing correct size. I am not able to figure out, what is eating up this space. Global Server - bdrpod01 Non Global zone - bdrpod01-zputq01 root@bdrpod01:/root# df -h... (2 Replies)
Discussion started by: solaris_1977
2 Replies

6. UNIX for Dummies Questions & Answers

Counting # of lines

Counting number of lines: sp I am trying to figure out a script to count the number of text files in cywig and have it give me a number (as the answer) any help would be appreciated. I am new here, so be gentle :D (3 Replies)
Discussion started by: unicksjp
3 Replies

7. UNIX for Dummies Questions & Answers

HELP! showing output as a ratio in uniq

Hi, I have the following file called addresses, (it is a large file i have only copy and pasted few of the data below) and I am wanting to write a command so it will Find the ratio of mobile (07....) to land line (01....) telephone numbers? then find the most popular first name and list the... (1 Reply)
Discussion started by: tina_2010
1 Replies

8. Shell Programming and Scripting

Showing the first 4 lines of a file?

Is there a way to show the first 4 lines of a file without using head -4? In sed would it be sed '1,4d' ? What if I just wanted to display the 2nd line ONLY? How could this be done with AWK?...correctly with SED? (6 Replies)
Discussion started by: puttster
6 Replies

9. Shell Programming and Scripting

Counting lines for each application

Hi All, I have a output that suppose to be like this (see below please) App : Line counts === ================== AAA: 100 BBB: 201 CCC: 137 DDD: 32 EEE: 55 for i in `ps -ef | grep App`; do print $i; done This only shows App : === (12 Replies)
Discussion started by: Beginer0705
12 Replies

10. Shell Programming and Scripting

Help with showing the difference in two lines of input

I would like my script to be able to tell the difference between to lines of input, like: Input 1: 1 2 3 4 5 Input 2: 1 2 3 4 5 6 I want the script to tell me that the difference between the two lines is the 6. Is there anyway I can have it do this? Here's an example of what my script... (12 Replies)
Discussion started by: Kweekwom
12 Replies
Login or Register to Ask a Question