one-liner for my script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers one-liner for my script
# 1  
Old 04-25-2010
one-liner for my script

Hi all,

Happy weekend.

I have the following sample.txt file which contains the students name and their marks of different subjects.

Code:
kamaraj@kamaraj-laptop:~/Desktop/testing$ cat sample.txt
kamaraj 34
kamaraj 35
kamaraj 56
raj 32
raj 324
raj 93
raj 93
test 1
test 1
test 1
test 1
test 1
test 1
one 23
one 23
one 23
one 23
one 23
one 23
one 23

my shell script is

Code:
#/bin/sh -x
cat sample.txt | awk ' /./ {print $1}' | sort | uniq > output.txt

for i in `cat output.txt`;do 
    tot=0;
    num=`grep $i sample.txt|awk '{print$2}'`; 
        for j in $num;do 
            tot=`expr $j + $tot`;
        done;
    echo "$i --> $tot"; 
done

The above script is giving the output as

Code:
kamaraj@kamaraj-laptop:~/Desktop/testing$ ./sample.sh 
kamaraj --> 125
one --> 161
raj --> 667
test --> 6

It is working as expect. But is there any way to do it very quickly ? ( one-liner )

thanks
kamaraj
# 2  
Old 04-25-2010
If you don't care about the order the output is printed...

Code:
awk '{_[$1]+=$2}END{for(i in _) print i" --> "_[i]}' infile

# 3  
Old 04-25-2010
Quote:
Originally Posted by malcomex999
If you don't care about the order the output is printed...

Code:
awk '{_[$1]+=$2}END{for(i in _) print i" --> "_[i]}' infile


Works great. But can you explain the logic please

thanks
kamaraj
# 4  
Old 04-25-2010
Quote:
Originally Posted by itkamaraj
Works great. But can you explain the logic please

thanks
kamaraj
Code:
awk '{arr[$1]+=$2}

It creates an array arr with $1 as index and adding the value of $2.
So, when ever it finds the same name in $1, it sums up $2 values.

Code:
END{for(i in arr) print i" --> "_[i]}' infile

When it finishes processing the file, i loops in the arr array and prints the result. I hope i explained it good!!!Smilie
# 5  
Old 04-25-2010
thanks a lot malcomex,

but where can i study about the arrays in shell ? ( how to apply in awk )

can you guide me.

thanks
kamaraj
# 6  
Old 04-25-2010
Quote:
Originally Posted by itkamaraj
thanks a lot malcomex,

but where can i study about the arrays in shell ? ( how to apply in awk )

can you guide me.

thanks
kamaraj
Check out his...

The GNU Awk User's Guide
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl one liner in bash script not replacing hours and minutes [HH:MM]

Hi I want to replace time stamp in the following line PROCNAME.Merge.exchMon.CODE.T_QSTART 08:45 read assuming the new time stamp is 09:45 ; the line is getting replaced as below :45 read I'm trying to use the perl one liner in bash script perl -pi... (4 Replies)
Discussion started by: charlie87
4 Replies

2. Shell Programming and Scripting

awk script does not work when written as "one-liner"

In my quest to solve a bigger problem (See my previous post called "Create SQL DML insert statements from file using AWK or similar" - sorry, not allowed to post urls until I have > 5 posts) I'm trying to get my head round awk, but have some problem figuring out why the following script does work... (2 Replies)
Discussion started by: Yagi Uda
2 Replies

3. Shell Programming and Scripting

How can I do one liner import multiple custom .pm files in my perl script?

I am new for Perl I want to ask one question. I have around 50 custom packages which i am using in my Perl script. I want to import all .pm packages in my Perl script in an easy way. Right now i have to import each package individually. So Is there any way to do so?? Right Now i am doing like: ... (1 Reply)
Discussion started by: Navrattan Bansa
1 Replies

4. Shell Programming and Scripting

Perl one-liner convert to script format problem asking

Input_file_1: ABC1 DEF11 ABC3 DEF7 ABC7 DEF36 Input_file_2: DEF7 light 23 DEF11 over 2 DEF11 over 1 DEF17 blue 0 Perl one-liner that join two input file based on columns sharing a value (In this example, column 2 in Input_file_1 and column 1 in... (3 Replies)
Discussion started by: perl_beginner
3 Replies

5. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

6. Shell Programming and Scripting

help one liner...

Hi I have a log data that shows chunks of data like this: thisis example test, this is example test, this is example test thisis example test, this is example test, this is example test thisis example test, this is example test, this is example test thisis example test, this is example test,... (2 Replies)
Discussion started by: erick_tuk
2 Replies

7. Shell Programming and Scripting

Need a one liner

abc/abc1/abc2/abc3/abc4 i need a script to pick this above path when ever any patterns like the below will be found. abc/abc1 abc/abc1/abc2 abc1/abc2/abc3 abc2/abc3/abc4 abc2/abc3/ etc .... etc..... not only the above 5 but like these one.. any one liner will be of great... (1 Reply)
Discussion started by: debu182
1 Replies

8. Shell Programming and Scripting

Turn an awk one liner into a script?

Hi, I was just helped with an excellent one liner. Now I need to know how to turn it into a script that I can call at the command line. So, this works with file script: ------------ for file in DATA.txt; do awk 'NR==1;NR>1{for (i=1;i<=NF;i++){a+=$i}}END{for (i=1;i<=NF;i++){printf... (3 Replies)
Discussion started by: mikey11415
3 Replies

9. Shell Programming and Scripting

Need a script or one-liner to purge lines from a file.

i all. This one sounds so simple, but I can't get it to work. I need to delete lines with certain keywords from a file. I have a file called defaultRules, with keywords: IPSEC_AH IKE_UDP IPMP_TEST_IFACE2 Then, I have another file called rules.txt with some rules: ... (10 Replies)
Discussion started by: BRH
10 Replies

10. Shell Programming and Scripting

Need help passing variables in shell script to perl one-liner

I'm writing a script to automate some post-install tasks on RHEL4 servers. I need the following code to insert an 'A' in the middle of a string, then replace the string in a file. I know I can use sed to do this, but I'd like to use perl's in place edit so I don't have to write to a temp file,... (1 Reply)
Discussion started by: Xek
1 Replies
Login or Register to Ask a Question