merging line and adding number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging line and adding number
# 1  
Old 07-25-2009
merging line and adding number

I having file below o/p

ibapp311dg,,20480,s,,,,,,,,,
test,,20480,s,,,,,,,,,
test,,20480,s,,,,,,,,,
ibapp311dg,,20480,s,,,,,,,,,

I want to to chk unique word line in the first field seperated by , as well as addup corressponding the number in field for each unique word like

ibapp311dg 40960
test 40960

please help
# 2  
Old 07-25-2009
Try this...

Code:
awk -F, '{arr[$1]= $3 + $3}END {for(i in arr) {print i" "arr[i]}}' yourfile

# 3  
Old 07-25-2009
Quote:
Originally Posted by malcomex999
Try this...

Code:
awk -F, '{arr[$1]= $3 + $3}END {for(i in arr) {print i" "arr[i]}}' yourfile

I'm afraid that won't give the answer. How about adding to arr[$1] - like
Code:
arr[$1] = arr[$1] + $3

instead.

Regards,

pen
# 4  
Old 07-25-2009
Quote:
Originally Posted by pen
I'm afraid that won't give the answer. How about adding to arr[$1] - like
Code:
arr[$1] = arr[$1] + $3

instead.

Regards,

pen

yea, you are right...that was my bad!!!
# 5  
Old 07-30-2009
thxs in load fixed my prob......

regards
TaD
# 6  
Old 08-01-2009
u guys nevermind if tell me that wat does it mean, this some specfic syntax

arr[$1] = arr[$1] + $3}END
# 7  
Old 08-01-2009
that code creates an array with name arr[$1] with subscript of the first column of ur file. And , = arr[$1] + $3}END , this will add the third column till the end of the line. I hope u got it!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging and Adding colon to columns

Hello, I have a tab delim file that looks like this CHROM POS ID REF ALT ID HGVS_C HGVS_P 1 17319011 rs2076603 G A NM_022089.3,NM_001141973.2,NM_001141974.2 c.1815C>T,c.1800C>T,c.1800C>T p.Pro605Pro,p.Pro600Pro,p.Pro600Pro 1 20960230 rs45530340 ... (3 Replies)
Discussion started by: nans
3 Replies

2. Shell Programming and Scripting

Adding user name to file, and then displaying new line number

Hi all - I'm completely stumped by a script I'm working on... The short version is I have a file called 'lookup' and in it are hundreds of names (first and last). I have a script that basically allows the user to enter a name, and what I need to have happen is something like this: Record... (8 Replies)
Discussion started by: sabster
8 Replies

3. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

4. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

5. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

Merging two files with merging line by line

Hi, I have two files and i want to merge it like, file1.txt --------- abc cde efg file2.txt ------- 111 222 333 Output file should be, -------------- abc 111 (2 Replies)
Discussion started by: rbalaj16
2 Replies

7. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

8. Shell Programming and Scripting

merging files and adding special columns

Hi everyone, I got a problem with merging files and hoped one of you would have an idea how to approach this issue. I tried it with awk, but didn't get far. This is what I have: I got 40 files looking like the ones below. All have three columns but the number of rows differs (20000 to 50000).... (6 Replies)
Discussion started by: TuAd
6 Replies

9. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

10. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies
Login or Register to Ask a Question