Page 1 of 2 12 LastLast
Results 1 to 16 of 32

Thread: Any 'awk' experts out there?

  1. #1
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,117
    Thanks
    55
    Thanked
    26 times in 25 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media Vivid 200

    Any 'awk' experts out there?

    I need some help parsing a file that contains some lines of the following form:

    99 maximum global priority in sys class (MAXCLSYSPRI)

    I need to be able to parse this line so that it recognises the word in brackets (which is always the last parameter) and pulls out the value (which is the first parameter). It then needs to store the name and value in an array (or two separate arrays), doing this for the entire input file (which is actually a UNIX command piped to awk). The awk script should ignore lines that do not contain words enclosed in parantheses as their last parameter.

    The post-processing should then print the word and value separated by a tab character. Any help would be appreciated. I was going to write my utility in Java or Visual Basic but i've been asked to use awk.

  2. #2
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: Any 'awk' experts out there?

    Code:
    awk '$8~/\(.*\)/{gsub(/[()]/, "", $8); print $8 "\t" $1}'
    Last edited by directhex; 17-04-2008 at 09:51 AM.

  3. Received thanks from:

    Taz (17-04-2008)

  4. #3
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,117
    Thanks
    55
    Thanked
    26 times in 25 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media Vivid 200

    Re: Any 'awk' experts out there?

    ^ Wow, thanks directhex. As I don't know how many params may be on a line, could I replace the $8 with $NF?

  5. #4
    mush-mushroom b0redom's Avatar
    Join Date
    Oct 2005
    Location
    Middlesex
    Posts
    3,438
    Thanks
    174
    Thanked
    362 times in 279 posts
    • b0redom's system
      • Motherboard:
      • Some iMac thingy
      • CPU:
      • 3.4Ghz Quad Core i7
      • Memory:
      • 24GB
      • Storage:
      • 3TB Fusion Drive
      • Graphics card(s):
      • nViidia GTX 680MX
      • PSU:
      • Some iMac thingy
      • Case:
      • Late 2012 pointlessly thin iMac enclosure
      • Operating System:
      • OSX 10.8 / Win 7 Pro
      • Monitor(s):
      • Dell 2713H
      • Internet:
      • Be+

    Re: Any 'awk' experts out there?

    Yes that will work - example below slightly easier to follow:

    cat <FILE> | grep \( | awk '{print $1 " " $NF}' | sed 's/[()]//g'

    Read input file
    Search for open bracket - need to escape as it's used by shell
    Print the 1st parameter, then a space and then the last parameter
    Then strip out the brackets
    Last edited by b0redom; 17-04-2008 at 10:05 AM.

  6. Received thanks from:

    Taz (17-04-2008)

  7. #5
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: Any 'awk' experts out there?

    Quote Originally Posted by b0redom View Post
    Yes that will work - example below slightly easier to follow:

    cat <FILE> | grep \( | awk '{print $1 " " $NF}' | sed 's/[()]//g'

    Read input file
    Search for open bracket - need to escape as it's used by shell
    Print the 1st parameter, then a space and then the last parameter
    Then strip out the brackets
    oh i agree it's neater, and that's pretty much how i would have done it if the requirement wasn't "use awk and only awk" - the awk version is probably a little faster though

  8. #6
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,117
    Thanks
    55
    Thanked
    26 times in 25 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media Vivid 200

    Re: Any 'awk' experts out there?

    I've discussed the requirements in a bit more detail and the project manager is concerned about multiple shells being launched.

    directhex, i'm getting a syntax error when I try to use the awk-only example:

    Code:
    awk: syntax error near line 1
    awk: illegal statement near line 1
    #
    My test script contains:

    Code:
    # !/bin/sh
    cat kernel.txt | awk '$NF~/\(.*\)/{gsub(/[()]/, "", $NF); print $NF "\t" $1}'
    A sample from kernel.txt contains:

    Code:
    *
    * Utsname Tunables
    *
        5.10  release (REL)
    columbia  node name (NODE)
       SunOS  system name (SYS)
    Generic_127111-01  version (VER)
    *
    * Process Resource Limit Tunables (Current:Maximum)
    *
    0x0000000000000100:0x0000000000010000   file descriptors
    *
    * Streams Tunables
    *
         9  maximum number of pushes allowed (NSTRPUSH)
     65536  maximum stream message size (STRMSGSZ)
      1024  max size of ctl part of message (STRCTLSZ)
    Any ideas?

  9. #7
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: Any 'awk' experts out there?

    Code:
    jms@osc-franzibald:/tmp$ cat kerny | awk '$NF~/\(.*\)/{gsub(/[()]/, "", $NF); print $NF "\t" $1}'
    REL	5.10
    NODE	columbia
    SYS	SunOS
    VER	Generic_127111-01
    Current:Maximum	*
    NSTRPUSH	9
    STRMSGSZ	65536
    STRCTLSZ	1024
    jms@osc-franzibald:/tmp$
    check your copypasta didn't pick up any funny interweb formatting. retype if need be.

  10. #8
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,117
    Thanks
    55
    Thanked
    26 times in 25 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media Vivid 200

    Re: Any 'awk' experts out there?

    ^ thanks, mate.

    I had to use 'nawk' instead of 'awk' on Solaris 10. Works like a champ. Many thanks for your help.

    My script now looks like this (Solaris 10):

    Code:
    # !/bin/sh
    sysdef -i | nawk '$NF~/\(.*\)/{gsub(/[()]/, "", $NF); print $NF "\t" $1}'

  11. #9
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: Any 'awk' experts out there?

    assume that standard solaris utilities are broken and worthless - even basic things like "tar" are beyond use. 'gawk' is GNU awk, and should behave the same on solaris as on linux. it's the awk i use by default.

  12. #10
    mush-mushroom b0redom's Avatar
    Join Date
    Oct 2005
    Location
    Middlesex
    Posts
    3,438
    Thanks
    174
    Thanked
    362 times in 279 posts
    • b0redom's system
      • Motherboard:
      • Some iMac thingy
      • CPU:
      • 3.4Ghz Quad Core i7
      • Memory:
      • 24GB
      • Storage:
      • 3TB Fusion Drive
      • Graphics card(s):
      • nViidia GTX 680MX
      • PSU:
      • Some iMac thingy
      • Case:
      • Late 2012 pointlessly thin iMac enclosure
      • Operating System:
      • OSX 10.8 / Win 7 Pro
      • Monitor(s):
      • Dell 2713H
      • Internet:
      • Be+

    Re: Any 'awk' experts out there?

    Quote Originally Posted by Taz View Post
    I've discussed the requirements in a bit more detail and the project manager is concerned about multiple shells being launched.
    Any ideas?
    That's your problem, I've never met a decent project manager who understands techinical stuff at a low level....

  13. #11
    Agent of the System ikonia's Avatar
    Join Date
    May 2004
    Location
    South West UK (Bath)
    Posts
    3,736
    Thanks
    39
    Thanked
    75 times in 56 posts

    Re: Any 'awk' experts out there?

    you can't use nawk in the same way on hpux, the porting page builds it differently.

    I'd try to stick with an awk variant that will be consisant across all your platforms choices, eg: gawk
    It is Inevitable.....


  14. #12
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: Any 'awk' experts out there?

    or do all the cleanup logic inside the application itself, using a cross-platform regular expression library you can trust, rather than on the shell. (note: i'm a massive hypocrite on this point)

  15. #13
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,117
    Thanks
    55
    Thanked
    26 times in 25 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media Vivid 200

    Re: Any 'awk' experts out there?

    What i'm doing is creating a single shell script that will identify the OS type and to execute the required OS-specific bits of code based on the OS that has been identified. I'll test the script on all the variants that i've been asked to cater for.

    I've done Solaris and Linux already and am just waiting for access to HP-UX and AIX.

  16. #14
    Agent of the System ikonia's Avatar
    Join Date
    May 2004
    Location
    South West UK (Bath)
    Posts
    3,736
    Thanks
    39
    Thanked
    75 times in 56 posts

    Re: Any 'awk' experts out there?

    and thats where you may have a problem, a script that uses nawk may not work on hpux, as directhex said, use something that is outside of the shells interoperability.

    But as directhex also said, I don't set a good example either, however I'd try to consider a awk version that is available and as generic across all platforms as possible, the best one I can see for solaris/hpux/linux/aix would be gawk.
    It is Inevitable.....


  17. #15
    Throbbing Member
    Join Date
    Aug 2004
    Location
    Scotlandshire
    Posts
    633
    Thanks
    15
    Thanked
    9 times in 8 posts
    • 8bit's system
      • Motherboard:
      • MSI Z170A-G43 PLUS
      • CPU:
      • Intel i7-6700K
      • Memory:
      • 2x Kingston HyperX Fury Black (8GB)
      • Storage:
      • 1x Crucial MX100 512GB, 1x Western Digital Caviar Black 1TB (WD1001FALS)
      • Graphics card(s):
      • Sapphire AMD R9 390 Nitro
      • PSU:
      • Corsair RM650x
      • Case:
      • Corsair Carbide Air 540
      • Operating System:
      • Windows 10 Home
      • Monitor(s):
      • LG 29UM67 29", 21:9, 2560x1080
      • Internet:
      • PlusNet Fibre

    Re: Any 'awk' experts out there?

    Watch the space between the # and the ! on the head line, that breaks on some *nixes. Probably being a bit pedantic in this day and age but
    Code:
    #!/bin/sh
    should work on anything.
    "shiro" - Windows 10 Home x64 :: Intel i7-6700K :: Corsair Hydro H90 :: MSI Z170A-G43 Plus :: 2x 8GB Kingston HyperX :: NVidia 3070 FE :: Corsair Force MP600 (1TB) :: Crucial MX100 (512GB) :: WD Caviar Black (1TB) :: Lite-On BD-ROM :: Corsair Carbide Air 540 (white) :: LG 32QK500 2560x1440 :: Logitech M500 :: Cherry KC6000 Slim ::

  18. #16
    Agent of the System ikonia's Avatar
    Join Date
    May 2004
    Location
    South West UK (Bath)
    Posts
    3,736
    Thanks
    39
    Thanked
    75 times in 56 posts

    Re: Any 'awk' experts out there?

    #!/bin/sh
    nope,

    there are multiple implimentations of bourne, a free one, each unix has its propriaty one, most linux's link to /bin/sh to bash and some (ubuntu) links it to ash, there there are multiple version of "sh" in fact some of them arn't even sh.
    It is Inevitable.....


Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Teen's blood type switch stuns experts
    By godsdog in forum General Discussion
    Replies: 14
    Last Post: 27-01-2008, 07:25 PM
  2. AMD to experts: Experts don't know jack
    By HEXUS in forum HEXUS News
    Replies: 2
    Last Post: 14-12-2007, 05:16 PM
  3. Replies: 7
    Last Post: 27-06-2006, 09:19 AM
  4. AWK help...
    By streetster in forum Software
    Replies: 12
    Last Post: 10-05-2005, 07:14 PM
  5. Experts
    By Zak33 in forum General Discussion
    Replies: 10
    Last Post: 27-01-2005, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •