Using AWK and regex


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using AWK and regex
# 1  
Old 08-24-2011
MySQL Using AWK and regex

Hi can you suggest in this regard


The sample.txt conatins the data
Code:
name lines type
sam   12    txt
sam   24    xls
sam   36    pdf
ram   32      txt
ram   45     sxls
ram   58      word
sam   92     jpeg
sam   21     gif
sam   22     ltf

from the data i need to sum all line columns having same name column and update in another column.I need to do this using awk .are there any alternatives.
suggest me in this regard.
Thanks in advance

Moderator's Comments:
Mod Comment Thread moved. Please don't post technical questions in the "contact us" forums. Thanks.

Last edited by Scott; 08-24-2011 at 01:22 PM.. Reason: Please use code tags...
# 2  
Old 08-24-2011
search in this forum

https://www.unix.com/shell-programmin...#post302549794

you will get lot of related questions and answers
# 3  
Old 08-24-2011
Quote:
Originally Posted by krashraj
...and update in another column.
You need to explain more about this. If you could show a sample of what you wanted your output to look like, that'd be good.
# 4  
Old 08-25-2011
The input is the following file
name lines type
Code:
sam   1    txt
sam   2    xls
sam   3    pdf
ram   3    txt
ram   4    sxls
ram   5    word
tom   9    jpeg
tom   5    gif
tom   2    ltf

I need to sum all columns with matching first column and update same in the second column.
I need to get output as below

Code:
 
name lines type   sum
sam   1    txt      6  
sam   2    xls      6
sam   3    pdf      6
ram   3    txt      12 
ram   4    sxls     12
ram   5    word     12
tom   9    jpeg     15
tom   5    gif      15
tom   2    ltf      15

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Thanks in advance

Last edited by zaxxon; 08-25-2011 at 03:12 AM.. Reason: code tags
# 5  
Old 08-25-2011
Code:
awk 'NF {a[$1]+=$2; b[$1]=$0} END {for (i in a) {print b[i], a[i]}}' file

This will give output as..
Code:
sam   3    pdf 6
tom   2    ltf 16
ram   5    word 12

Sorry, I could not help you much on how to get the output in the format that you mentioned Smilie

Hope someone in this forum would help you for sure. You may query again.

Last edited by royalibrahim; 08-26-2011 at 02:21 AM..
This User Gave Thanks to royalibrahim For This Post:
# 6  
Old 08-27-2011
Can anyone help to get the format asked by krashraj?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex within IF statement in awk

Hello to all, I have: X="string 1-" Y="-string 2" Z="string 1-20-string 2"In the position of the number 20 could be different numbers, but I'm interest only when the number is 15, 20,45 or 70. I want to include an IF within an awk code with a regex in the following way. ... (12 Replies)
Discussion started by: Ophiuchus
12 Replies

2. Shell Programming and Scripting

wildcard in regex for awk

Hello I have a file like : 20120918000001413 | 1.17.163.89 | iSelfcare | MSISDN | N 20120918000001806 | 1.33.27.100 | iSelfcare | 5564 | N .... I want to extract all lines that have on 4th field (considering "|" the separator ) something other than just digits. I want to do this using a... (5 Replies)
Discussion started by: black_fender
5 Replies

3. Shell Programming and Scripting

awk regex- include text

Hi I am trying to filter some data using awk. I have a statement- awk 'BEGIN { FS = "\n" ; RS = "" } { if ( $6 = "City: " ) { print "City: Unknown" } else { print $6 } }'` The $6 values are City: London City: Madrid City: City: Tokyo This expression seems to catch all the lines... (4 Replies)
Discussion started by: jamie_123
4 Replies

4. Shell Programming and Scripting

awk equivalent of regex

Hi all, Can someone tell me what's the (g)awk equal of this simple regex to find ip addresses in urls: egrep "^http://{1,3}\.{1,3}\.{1,3}\.{1,3}(:{1,5})?/"Input: http://10.0.0.1/query.exe http://11y10x09w:80/howaboutme http://192.168.100.190:1234/takeme.gpg Output:... (8 Replies)
Discussion started by: r4v3n
8 Replies

5. Shell Programming and Scripting

awk with multiple regex and substring

Hi Experts, I have a file on which i want to print the line which should match following criterias. Line should not start with 0 or 9 and Line should start with 1 and ( 576th character should not be 1 or 2 or 576-580 postion should not be NIPPF or CDIPB or 576-581 postion should... (2 Replies)
Discussion started by: millan
2 Replies

6. Shell Programming and Scripting

awk regex problem

hi everyone suppose my input file is ABC-12345 ABCD-12345 BCD-123456 i want to search the specific pattern which looks like - in a file so i used this command cat $file | awk ' { if ($0 ~ /-/) { print } }' so it gives me the result as ABCD-12345 BCD-12345 BCD-12345 ... (31 Replies)
Discussion started by: aishsimplesweet
31 Replies

7. Shell Programming and Scripting

awk variables in regex expression ?

Hello, Could someone explain why this one returns nothing: $ x=/jon/ $ echo jon | awk -v xa=$x '$1~xa {print}' $ while the following works fine: $ x=jon $ echo jon | awk -v xa=$x '$1==xa {print}' $ jon and the following works fine: $ echo jon | awk '$1~/jon/ {print}' $ jon ... (3 Replies)
Discussion started by: vilius
3 Replies

8. Shell Programming and Scripting

AWK regex to find only numbers

Hi guys I need to find both negative and positive numbers from the following text file. And i also dont need 0. 0 8 -7 -2268 007 -07 -00 -0a0 0a0 -07a0 7a00 0a0 Can someone please give a regex to filter out the values in red. I tried a few things in awk but it didnt work... (9 Replies)
Discussion started by: sridanu
9 Replies

9. Shell Programming and Scripting

Extracting a regex with awk

I have a regexp that I wish to match against every line of a file using awk. But I do not want to substitute it or select the line. I want to pull the matched text out and put it in a different file, line by line. What is the correct awk usage to *extract* a regexp and put it in another... (11 Replies)
Discussion started by: Enobarbus37
11 Replies

10. Shell Programming and Scripting

awk or regex

Hi! I want to made a program that will generate code like this: {{Navedi XYZ |avtor=XYZ1 |naslov=XYZ2 |leto_izzida=XYZ3 |zalozba=XYZ4 |kraj=XYZ5 |isbn=XYZ6 |cobiss_id=XYZ7 }} from input like this: <b> ODGOVORNOST............. : <a... (5 Replies)
Discussion started by: smihael
5 Replies
Login or Register to Ask a Question