Hi,
Im looking for a script which will calculate the unique strings column 2 & 3 values in a log as mentioned in example
eg:-
bag 12 12
bag 18 15
bags 15 13
bags 15 14
blazer 24 24
blazer 33 32
boots 19 15
Result should be:-
bag 30 27
bags 30 27
blazer 57 56
boots 19 15
Please help me on this!!!
Regards
wintech
ctsgnb
February 14, 2012, 10:07am
2
awk '{a[$1]+=$2;b[$1]+=$3}END{for (i in a) print i,a,b}' yourfile
Note that this code assume your file is already sorted on its first-column
If you want the output sorted :
awk '{a[$1]+=$2;b[$1]+=$3}END{for (i in a) print i,a,b}' yourfile | sort
1 Like
Thank You!!! Hurray it works perfect..... Thank you so much
---------- Post updated at 11:46 PM ---------- Previous update was at 11:34 PM ----------
Hi,
i have another small request, need to remove double/triple space in between a content to a single space in a file as below example.
File
bags 15 13
bags 15 14
blazer 24 24
blazer 33 32
boots 19 15
Result should be single space between the content
bags 15 13
bags 15 14
blazer 24 24
blazer 33 32
boots 19 15
Thanks
wintech
ctsgnb
February 15, 2012, 1:41pm
4
sed 's/ */ /g' filemanyspaced >filesinglespaced
or
tr -s ' ' <filemanyspaced >filesinglespaced
Hi,
Sorry!! double space is not removed to single space.
ctsgnb
February 15, 2012, 2:47pm
6
It should work
please not that there is a double space in the given sed rule :
s/ */ /g
is s/<space><space>*/<space>/g
Please copy paste exactly what you have tried because for me it works fine :
$ echo ': :'
: :
$ echo ': :' | sed 's/ */ /g'
: :
$ echo ': :' | tr -s ' '
: :
1 Like
Thank you!!! mistake is in my side sorry!!! command which you gave works fine..
Hi ctsgnb ,
The command "awk 'p!=$1\{if\(p\)print p,s,t;s=t=x;p=$1\}\{s\+=$2;t\+=$3\}END\{print p,s,t\}' $FILE/ban.txt > $FILE/banresult.txt" is working fine for first 6 lines and for rest of the line it shows 0.
I have 100 lines in a file, please let me know if im doing any mistake!!!
Prd c1 c2
bag 12 12
bag 18 15
bags 15 13
bags 15 14
blazer 24 24
blazer 33 32
boots 19 15
cardigan 12 11
cardigan 31 30
cardigan 32 27
chino 12 10
clearance 11 10
clearance 11 9
coat 22 20
coat 29 24
coats 15 13
cologne 18 18
cologne 19 17
dress 63 56
dress 71 59
dresses 14 12
dresses 94 86
dresses 96 80
fragrance 11 11
handbags 11 9
handbags 18 17
hats 13 9
jackets 16 14
jeans 24 21
jeans 28 21
leather%20jacket 20 20
leather%20jacket 28 27
leggings 19 19
leggings 29 26
mad%20men 104 89
mad%20men%20collection 14 14
mad%20men 21 18
mad%20men 99 89
man 11 10
maternity 15 15
maternity 21 18
men 26 24
men 28 21
men 75 67
men 88 75
mens%20shoes 13 13
monogram 11 11
order%20status 34 33
order%20status 42 36
palisades%20tote 14 13
palisades%20tote 16 13
pants 11 10
pants 12 11
perfume 12 11
perfume 47 45
perfume 53 44
petite 13 13
petite 21 18
polka%20dot 14 14
purse 12 8
purse 13 11
sale 40 39
sale 48 41
scarf 18 18
scarf 20 17
shirts 19 16
shoes 42 42
shoes 49 43
shorts 29 28
shorts 31 29
shorts 37 33
shorts 42 37
size%20chart 14 13
size%20chart 18 18
skirt 14 14
skirts 11 9
Sloan 13 11
suits 17 16
suits 18 17
sweater 24 24
sweater 30 26
sweaters 14 10
swim 12 11
swim 12 12
swim 14 14
swim 17 14
ties 12 6
tops 11 9
tote 11 11
trench%20coat 11 11
trench%20coat 17 16
trench 35 33
trench 47 41
vest 30 30
vest 33 32
vest 41 36
vest 49 41
women 11 9
women 44 37
women 57 49
And also please let me know, how to remove a double/triple space between c1 & c2 and make it single space line.
Regards
Wintech
jpachar
February 22, 2012, 1:19am
9
I am working in centos unix os. My sort command is not working. Consider the following file
1008 Vijay 40
1009 Rekha 34
1010 Shreyas 40
1011 Sanjay 40
sort emp is not working
Can you send the options in sort command
ctsgnb
February 22, 2012, 3:05am
10
What are your requirement ?
I already answered to your questions in some previous posts
Did your requirements change ?
If so, please create a new thread with a full description of what input you have and what output you expect.
Hi ctsgnb ,
No!! my requirement is not changed
Yes you have answered my requirement, but the command which you gave works only for first 6 lines and rest shows 0..... Can i know why it shows 0 from 7th line till 100th line.
Regards
Wintech
ctsgnb
February 22, 2012, 3:51am
12
You want to sort per employee name ?
If they are in column 2
then use
... | sort -k 2
---------- Post updated at 09:49 AM ---------- Previous update was at 09:43 AM ----------
Otherwise :
upload your input file
please copy paste exactly the command you have tried
please describe what is the problem
---------- Post updated at 09:51 AM ---------- Previous update was at 09:49 AM ----------
the command
awk 'p!=$1{if(p)print p,s,t;s=t=x;p=$1}{s+=$2;t+=$3}END{print p,s,t}' $FILE/ban.txt > $FILE/banresult.txt
IS NOT the command i suggested in my previous posts ...
Or are you mixing this thread with another one ?
@ctsgnb : The user who posted a question in post #9 (jpachar) is different from the user who created this thread (Paulwintech)
@jpachar : Please create a new thread.
1 Like
jpachar
February 22, 2012, 4:40am
14
There is no error as such, but the records are not sorted. I gave the command sort emp.
Hi ctsgnb ,
Please let me know any update on this!!!!
Thanks
Wintech
Perl, ok?
perl -ane '$x{$F[0]}+=$F[1]; $y{$F[0]}+=$F[2]; END {for(sort keys %x){print "$_ $x{$_} $y{$_}\n"}}' inputfile
1 Like
ctsgnb
February 22, 2012, 2:59pm
17
@jpachar
Please open a new thread (not a new post...)
Thanks
---------- Post updated at 08:59 PM ---------- Previous update was at 08:54 PM ----------
@Paulwintech
paulwintech:
Hi ctsgnb ,
No!! my requirement is not changed
Yes you have answered my requirement, but the command which you gave works only for first 6 lines and rest shows 0..... Can i know why it shows 0 from 7th line till 100th line.
Regards
Wintech
upload your input file
please copy paste exactly the command you have tried
Thanks in advance
Hi balajesuri ,
Thank you so much it works fine........ Can you please let me know how remove double/triple space between columns in a file. As per forum advice i used
sed 's/ */ /g' filemanyspaced >filesinglespacedcommand, but no luck... Can you please help on this.
Regards
Paulwintech
$ cat one.txt
Can you please let me know
how remove double/triple space
between columns in a file
$ perl -lane '$_=~s/\s+/ /g;print $_' one.txt
Can you please let me know
how remove double/triple space
between columns in a file
@Paulwintech : Try this: sed 's/[ ][ ]*/ /g'