awk equivalent script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk equivalent script
# 1  
Old 02-11-2008
awk equivalent script

Hi All,

I need a script that prints a blank line after finding that the next lines first field doesn't match the current lines first field.

I really want to do this in awk to improve my own knowledge but the best I can come up with is a bash script. I'd finally like to understand how awk processes a file Smilie

Here's the bash script so you can see what I'm trying to achieve:

Code:
COUNT=$( cat $1 | wc -l )

for ((a=1; a <= $COUNT ; a++))
do

    let "n=a+1"
    NEXT=$( awk -F':' -v L=$n 'NR == L { print $1 }' $1 )
    LINE=$( awk -F':' -v L=$a 'NR == L { print $0 }' $1 )
    CURR=$( awk -F':' -v L=$a 'NR == L { print $1 }' $1 )
    echo $LINE

    if [[ "$NEXT" != "$CURR" ]]
    then
       echo " "
    fi

done

Many thanks,

p.
# 2  
Old 02-11-2008
With sample input and desired output it would be easier ...
# 3  
Old 02-11-2008
Ah yes, it might make things clearer Smilie

Here's the input:

Quote:
system-d-1-2008-02-10.xls : CPU Utilisation : Amber Alert : 8
system-d-1-2008-02-10.xls : I-O Statistics : Amber Alert : 11
system-d-1-2008-02-10.xls : I-O Statistics : Red Alert : 34
system-d-1-2008-02-10.xls : Network Statistics - bond0 : Amber Alert : 1
system-d-1-2008-02-10.xls : Network Statistics - bond1 : Amber Alert : 13
system-d-1-2008-02-10.xls : Network Statistics - eth1 : Amber Alert : 13
system-d-1-2008-02-10.xls : Network Statistics - lo : Amber Alert : 4
system-d-1-2008-02-10.xls : Process Run Queue : Amber Alert : 13
system-d-2-2008-02-10.xls : I-O Statistics : Amber Alert : 1
system-d-2-2008-02-10.xls : I-O Statistics : Red Alert : 1
system-d-2-2008-02-10.xls : Network Statistics - bond0 : Amber Alert : 1
system-d-2-2008-02-10.xls : Network Statistics - bond1 : Amber Alert : 6
system-d-2-2008-02-10.xls : Network Statistics - eth0 : Amber Alert : 1
system-d-2-2008-02-10.xls : Network Statistics - eth1 : Amber Alert : 6
system-d-2-2008-02-10.xls : Process Run Queue : Amber Alert : 3
system-d-3-2008-02-10.xls : CPU Utilisation : Amber Alert : 15
system-d-3-2008-02-10.xls : I-O Statistics : Amber Alert : 5
system-d-3-2008-02-10.xls : I-O Statistics : Red Alert : 30
system-w-5-2008-02-10.xls : Network Statistics - bond0 : Amber Alert : 2
system-w-5-2008-02-10.xls : Network Statistics - eth0 : Amber Alert : 2
system-w-8-2008-02-10.xls : Network Statistics - bond0 : Amber Alert : 1
system-w-8-2008-02-10.xls : Network Statistics - eth0 : Amber Alert : 1
And here's what I'm trying to achieve:

Quote:
system-d-1-2008-02-10.xls : CPU Utilisation : Amber Alert : 8
system-d-1-2008-02-10.xls : I-O Statistics : Amber Alert : 11
system-d-1-2008-02-10.xls : I-O Statistics : Red Alert : 34
system-d-1-2008-02-10.xls : Network Statistics - bond0 : Amber Alert : 1
system-d-1-2008-02-10.xls : Network Statistics - bond1 : Amber Alert : 13
system-d-1-2008-02-10.xls : Network Statistics - eth1 : Amber Alert : 13
system-d-1-2008-02-10.xls : Network Statistics - lo : Amber Alert : 4
system-d-1-2008-02-10.xls : Process Run Queue : Amber Alert : 13

system-d-2-2008-02-10.xls : I-O Statistics : Amber Alert : 1
system-d-2-2008-02-10.xls : I-O Statistics : Red Alert : 1
system-d-2-2008-02-10.xls : Network Statistics - bond0 : Amber Alert : 1
system-d-2-2008-02-10.xls : Network Statistics - bond1 : Amber Alert : 6
system-d-2-2008-02-10.xls : Network Statistics - eth0 : Amber Alert : 1
system-d-2-2008-02-10.xls : Network Statistics - eth1 : Amber Alert : 6
system-d-2-2008-02-10.xls : Process Run Queue : Amber Alert : 3

system-d-3-2008-02-10.xls : CPU Utilisation : Amber Alert : 15
system-d-3-2008-02-10.xls : I-O Statistics : Amber Alert : 5
system-d-3-2008-02-10.xls : I-O Statistics : Red Alert : 30

system-w-5-2008-02-10.xls : Network Statistics - bond0 : Amber Alert : 2
system-w-5-2008-02-10.xls : Network Statistics - eth0 : Amber Alert : 2

system-w-8-2008-02-10.xls : Network Statistics - bond0 : Amber Alert : 1
system-w-8-2008-02-10.xls : Network Statistics - eth0 : Amber Alert : 1
Many thanks,

p.
# 4  
Old 02-11-2008
Code:
awk 'x[$1]++||$0=NR==1?$0:RS $0' input

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 02-11-2008
Wow, that's genius! Smilie

...I'm going to have to go and figure out how it works now Smilie

Many thanks, p.
# 6  
Old 02-12-2008
Hi Radoulov,

I've been trying to decipher your awk command but I'm a little stuck - I understand that the "? :" provides a conditional operator that is found in the C programming language - that has the form:

Code:
expr ? action1 : action2

For the expression do one or the other depending whether the expression is matched or not e.g.

Code:
grade = (avg >= 65) ? "Pass" : "Fail"

But the bit I don't understand is the expression:

Code:
x[$1]++||$0=NR==1

I can see that you're loading $1 into an array and incrementing it... but I really don't know what you're using it for and the second part on the other side of the OR clause is even more confusing Smilie

I hope you can explain it to me - I'd really appreciate it Smilie

Cheers, p.
# 7  
Old 02-12-2008
system library

How can I add new modules in the syetem library ?
I mean is it possible or not? Like in C library we can add some new modules so likewise is it possible for the system library or not?
please tell me soon...!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script Equivalent .

Hello. I wrote some code for an awk command but I want to learn to turn it into an awk script but am stuck. I have a file (data.csv) that has the following data: ADD,1,3,5,8,10,11,54 SUB,1,2,3,4 ADD,15,18,21,42,37 ADD,1,1,1,0,0,3,16 ADD,4,1,8,0,4,6,13,16,17,20,8,6,4 SUB,13,8If the line... (5 Replies)
Discussion started by: Eric7giants
5 Replies

2. Shell Programming and Scripting

sed Equivalent for awk/grep

Any equivalent command using awk or grep? sed -n "/^$(date --date='10 minutes ago' '+%b %_d %H:%M')/,\$p" /abc.log (7 Replies)
Discussion started by: timmywong
7 Replies

3. Shell Programming and Scripting

Java - Arrays.binarySearch function equivalent in awk

Hi all Does anyone know Java-Arrays.binarySearch function equivalent in awk I tried like this but it's not correct one,it just returns array index if and only when searched value available in array, for some reason if searched value not found then I want to return upper nearest neighbour index.... (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

4. Shell Programming and Scripting

awk equivalent code in C for printing NF

Hi all ! whether anyone in forum knows what awk will use while printing number of fields in file(NF) ? for example awk END'{print NF}' file prints number of columns in file if anyone knows equivalent code in C kindly share or explain logic behind it (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

5. Shell Programming and Scripting

What is the equivalent of NR (awk) in perl?

Hello, I searched online; it seems that perl use $NR as NR in awk; however it does not work for me. For example, how to re-write the following awk using perl: awk '{ print NR}' inputfile---------- Post updated at 01:55 PM ---------- Previous update was at 12:49 PM ---------- I found... (2 Replies)
Discussion started by: littlewenwen
2 Replies

6. 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

7. Windows & DOS: Issues & Discussions

awk to findstr equivalent

Hi, I 'd like to translate this command from awk to findstr on Windows DOS FILE: str1 server1 a str1 server2 a str2 server1 b str2 server2 b Awk command: awk ' $1 ~/str1/ { print $2, $3 } ' file.txt OUTPUT: server1 a server2 a Thanks, (1 Reply)
Discussion started by: phamp008
1 Replies

8. Shell Programming and Scripting

Bash Script equivalent KSH script?

I always find BASH easier than ksh. At my home, i have written this bash script. I am finding it hard to write its equivalent in ksh, any suggestions? ###################################### #return seconds since `00:00:00 1970-01-01 UTC' (a GNU extension)... (1 Reply)
Discussion started by: boy18nj
1 Replies

9. Linux

Need SCP script equivalent to FTP script .

Hi, Currently i'm using the folllowing FTP acript and its working. echo "open $server" > ftp_file echo "user $user $password" >> ftp_file echo "cd $remote_dir" >> ftp_file echo "lcd $local_dir" >> ftp_file echo "put $file">> ftp_file echo "bye" >> ftp_file ftp -nv < ftp_file I've... (1 Reply)
Discussion started by: vickramshetty
1 Replies

10. Shell Programming and Scripting

shell script equivalent for tcl function

Hello, I need experts help in converting the below tcl function to korn shell function equivalent. proc lsNetMaskToBits {mask} { set dw ; # Top N bits set set dw 0x ; # Make sure it's hexadecimal, else XOR fails. puts "lsNetMaskToBits dw $dw" set dw ; # Complement => low 32-N bits... (1 Reply)
Discussion started by: JackMelson
1 Replies
Login or Register to Ask a Question