Append no of times a column is repeated at the end


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Append no of times a column is repeated at the end
# 1  
Old 10-03-2014
Append no of times a column is repeated at the end

Hi folks,

Iam working on a bash script, i need to print how many times column 2 repeated at the end of each line.

Input.txt
Code:
COL1 COL2 COL3 COL4
 1 XX 45 N
 2 YY 34 y
 3 ZZ 44 N
 4 XX 89 Y
 5 XX 45 N
 6 YY 84 D
 7 ZZ 22 S

Output.txt
Code:
COL1 COL2 COL3 COL4 COL5
 1 XX 45 N 3
 2 YY 34 y 2
 3 ZZ 44 N 2
 4 XX 89 Y 3
 5 XX 45 N 3
 6 YY 84 D 2
 7 ZZ 22 S 2

i.e in column 2 the repeatation of XX is 3 , YY is 2 and ZZ is 2, which has to appended at the end of each line.

appreciate your help.
# 2  
Old 10-03-2014
Quote:
Originally Posted by tech_frk
Hi folks,

Iam working on a bash script, i need to print how many times column 2 repeated at the end of each line.

Input.txt
Code:
COL1 COL2 COL3 COL4
 1 XX 45 N
 2 YY 34 y
 3 ZZ 44 N
 4 XX 89 Y
 5 XX 45 N
 6 YY 84 D
 7 ZZ 22 S

Output.txt
Code:
COL1 COL2 COL3 COL4 COL5
 1 XX 45 N 3
 2 YY 34 y 2
 3 ZZ 44 N 2
 4 XX 89 Y 3
 5 XX 45 N 3
 6 YY 84 D 2
 7 ZZ 22 S 2

i.e in column 2 the repeatation of XX is 3 , YY is 2 and ZZ is 2, which has to appended at the end of each line.

appreciate your help.
Hello tech_frk,

Following may help you in same.

Code:
 awk 'NR==1{$(NF+1)="COL5";print $0} FNR==NR && NR>1 {X[$2]++;Y[$2]=$0 OFS X[$2];next} ($2 in Y){print Y[$2]}' Input_file Input_file

Output will be as follows.

Code:
COL1 COL2 COL3 COL4 COL5
 5 XX 45 N 3
 6 YY 84 D 2
 7 ZZ 22 S 2
 5 XX 45 N 3
 5 XX 45 N 3
 6 YY 84 D 2
 7 ZZ 22 S 2

Thanks,
R. Singh
# 3  
Old 10-03-2014
singh,

thank you but it is not printing anything with this command
# 4  
Old 10-03-2014
Code:
awk 'FNR==NR{ A[$2]++; if(NR==1)print $0,"COL5"; next}FNR>1{print $0,A[$2]}' infile infile

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 10-03-2014
Quote:
Originally Posted by tech_frk
singh,

thank you but it is not printing anything with this command
Hello tech_frk,

EDIT: Chaged the code by reading one file.

Following should work.

Code:
awk 'NR==1{$(NF+1)="COL5";print $0} NR>1 {X[$2]++;Y[++i]=$0 OFS X[$2];} (i in Y){print Y[i]}'   Input.txt

EDIT: Above solution is not giving OP's correct output, working on it to provide correct solution, Akshay's solution works fine.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-03-2014 at 04:21 AM.. Reason: Adding reason for user not to user this solution and working on making a new solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 10-03-2014
simply genious... thanks guys
# 7  
Old 10-03-2014
Quote:
Originally Posted by RavinderSingh13
Hello tech_frk,

Following may help you in same.

Code:
 awk 'NR==1{$(NF+1)="COL5";print $0} FNR==NR && NR>1 {X[$2]++;Y[$2]=$0 OFS X[$2];next} ($2 in Y){print Y[$2]}' Input_file Input_file

Output will be as follows.

Code:
COL1 COL2 COL3 COL4 COL5
 5 XX 45 N 3
 6 YY 84 D 2
 7 ZZ 22 S 2
 5 XX 45 N 3
 5 XX 45 N 3
 6 YY 84 D 2
 7 ZZ 22 S 2

Thanks,
R. Singh
Hi R.Singh did you notice because of the use of array Y with index $2, you are saving last line read with some index as you can see there are duplicate keys it just replaces array contents if saved before, I highlighted with red color that is why your output is wrong, and there is actually no need of using 2 array.

---------- Post updated at 01:48 PM ---------- Previous update was at 01:19 PM ----------

Quote:
Originally Posted by RavinderSingh13
Hello tech_frk,

EDIT: Chaged the code by reading one file.

Following should work.

Code:
awk 'NR==1{$(NF+1)="COL5";print $0} NR>1 {X[$2]++;Y[++i]=$0 OFS X[$2];} (i in Y){print Y[i]}'   Input.txt

Thanks,
R. Singh

I see you edited your code, still your code fails

Last edited by Akshay Hegde; 10-03-2014 at 04:05 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate lines which has been repeated 4 times

Remove duplicate lines which has been repeated 4 times attached test.txt below command tried and not getting expect output. for i in `cat test.txt | uniq` do num=`cat test.txt | grep $i | wc -l` echo $i $num done test.txt ... (17 Replies)
Discussion started by: Kalia
17 Replies

2. UNIX for Beginners Questions & Answers

Export lines that have first entry repeated 5 times or above

Dears i want to extract lines only that have first entry repeated 3 times or above , ex data : -bash-3.00$ cat INTCONT-IS.CSV M205-00-106_AMDRN:1-0-6-22,12-662-4833,intContact,2016-11-15 02:32:16,50 M205-00-106_AMDRN:1-0-23-17,12-616-0462,intContact,2016-11-15 02:32:23,50... (5 Replies)
Discussion started by: is2_egypt
5 Replies

3. Shell Programming and Scripting

To append new data at the end of each line based on substring of last column

Hi guys, I need to append new data at the end of each line of the files. This new data is based on substring (3rd fields) of last column. Input file xxx.csv: U1234|1-5X|orange|1-5X|Act|1-5X|0.1 /sac/orange 12345 0 U5678|1-7X|grape|1-7X|Act|1-7X|0.1 /sac/grape 5678 0... (5 Replies)
Discussion started by: null7
5 Replies

4. Homework & Coursework Questions

Accepting a phrase and counting the number of times that it is repeated in a specific website

1. The problem statement, all variables and given/known data: Develop a shell script that accepts a phrase and counts the number of times that it is repeated in a specific website. Note: Im not sure if it's the whole website, or just a specific page but im guessing its thewhole website. ... (2 Replies)
Discussion started by: Zakerii
2 Replies

5. Shell Programming and Scripting

How to print the lines which are repeated 3 times in a file?

Hello All, I have a file which has repeated lines. I want to print the lines which are repeated three times. Please help. (3 Replies)
Discussion started by: ailnilanjan
3 Replies

6. UNIX for Dummies Questions & Answers

Extracting column if above certain values and repeated over a number of times continuously

Hi I am new to the forum and would like to ask: i have a file in form with thousands of column id.1 A01 A01 A68 A68 id.2 A5 A5 A3 A3 1001 0 0 0.136 0.136 1002 0 0 0.262 0.183 1003 0 0 0.662 0.662 1004 0 0 ... (9 Replies)
Discussion started by: newbeeuk
9 Replies

7. UNIX for Dummies Questions & Answers

To get the total of how many times the pattern is repeated.

Hi, I need the total of how many times the pattern is being repeated in a particular file. I tried with grep as below, but no go. grep -c best sample Contents of Sample file: ----------------------- This is the best site I have never come across. The best one indeed. Very best... (8 Replies)
Discussion started by: venkatesht
8 Replies

8. Shell Programming and Scripting

Append some text to a file multiple times

Hi, I have a text file like Version=abc Tab=1 URL GOTO=www.abc.com/board=1 some text... I want to run a loop x no of times and append to the text file above text but URL GOTO should be www.abc.com/board=2 then 3,4...etc till x. Kindly help (2 Replies)
Discussion started by: krabu
2 Replies

9. Shell Programming and Scripting

Append char to the end of string from Nth column

I'm sure this is easy to do but I can't find a one line command with awk or sed to append a char to the end of the string from Nth column. Any sugestion please? Thanks (2 Replies)
Discussion started by: cabrao
2 Replies

10. UNIX for Dummies Questions & Answers

how many times does this repeated sequence exist

need a script to determine daily how many times does the below repeated sequence exist in a log file, and if it shows failure to connect in the same file 200 PORT Command successful. 150 Opening BINARY mode data connection for rgr016.daily.0305. 226 Transfer complete. local: rgr016.daily.0306... (3 Replies)
Discussion started by: lichento
3 Replies
Login or Register to Ask a Question