Count Column and Print at last


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count Column and Print at last
# 1  
Old 03-21-2013
Count Column and Print at last

HI Guys,

Count base on column A


Code:
Input:-

U00393 6 000100000
U00393 7 000100001
U00393 8 000100002
U00393 9 000100003
U00393 10 000100004
U00393 11 000100005
U00928 6 000100000
U00928 7 000100001
U00964 6 000100000
U00964 7 000100001
U00964 8 000100002
U00972 6 000100000
U00972 7 000100001
U00984 6 000100000
U00984 7 000100001
U00984 8 000100002

Output:-

U00393 6 000100000 1
U00393 7 000100001 2
U00393 8 000100002 3
U00393 9 000100003 4
U00393 10 000100004 5
U00393 11 000100005 6
U00928 6 000100000 1
U00928 7 000100001 2
U00964 6 000100000 1
U00964 7 000100001 2
U00964 8 000100002 3
U00972 6 000100000 1
U00972 7 000100001 2
U00984 6 000100000 1
U00984 7 000100001 2
U00984 8 000100002 3

# 2  
Old 03-21-2013
Try:
Code:
awk '
$1 != prev {c=1; prev=$1}
{       print $0,c++}
' Input

If you are using a Solaris/SunOS system use /usr/xpg4/bin/awk or nawk instead of awk.
# 3  
Old 03-21-2013
Code:
awk '{A[$1]++;print $0,A[$1]}' file

OR
Code:
awk '{A[$1]++;$0=$0 FS A[$1]}1' file

# 4  
Old 03-21-2013
Code:
$ cat temp.x
U00393 6 000100000
U00393 7 000100001
U00393 8 000100002
U00393 9 000100003
U00393 10 000100004
U00393 11 000100005
U00928 6 000100000
U00928 7 000100001
U00964 6 000100000
U00964 7 000100001
U00964 8 000100002
U00972 6 000100000
U00972 7 000100001
U00984 6 000100000
U00984 7 000100001
U00984 8 000100002

Code:
$ cat temp.sh
A_PREV=xxxxxx N=1
while read A B C; do
  if [ "$A" != "$A_PREV" ]; then
    N=1;
  fi
  echo $A $B $C $N
  N=`expr $N + 1`
  A_PREV=$A
done < temp.x

Code:
$ ./temp.sh
U00393 6 000100000 1
U00393 7 000100001 2
U00393 8 000100002 3
U00393 9 000100003 4
U00393 10 000100004 5
U00393 11 000100005 6
U00928 6 000100000 1
U00928 7 000100001 2
U00964 6 000100000 1
U00964 7 000100001 2
U00964 8 000100002 3
U00972 6 000100000 1
U00972 7 000100001 2
U00984 6 000100000 1
U00984 7 000100001 2
U00984 8 000100002 3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Read first column and count lines in second column using awk

Hello all, I would like to ask your help here: I've a huge file that has 2 columns. A part of it is: sorted.txt: kss23 rml.67lkj kss23 zhh.6gf kss23 nhd.09.fdd kss23 hp.767.88.89 fl67 nmdsfs.56.df.67 fl67 kk.fgf.98.56.n fl67 bgdgdfg.hjj.879.d fl66 kl..hfh.76.ghg fl66... (5 Replies)
Discussion started by: Padavan
5 Replies

3. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

10. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies
Login or Register to Ask a Question