#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Help with simple script


    Hello,

    I am new to the Unix world, and would like some assistance making something simple: I am trying to design a simple script that retrieves information from a default file using awk and sed if possible.

    Here is the content of the file, named sample:

    1.-PL000000002002615641 18 DAY 25/11/2013
    2.-PL000000002002412789 93 DAY 6/11/2012
    3.-PL000000002002341128 44 DAY 16/11/2013
    4.-PL000000002002646981 27 DAY 28/11/2013


    I would like to remove all the information except PL000000002002615641 for example. Keeping all the PL numbers.

    I have tried awk '{print $1}' sample but I still have a lot of characters that I have been unable to remove with sed.

    Any help would be greatly appreciated,

    Thank you!
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    859
    Rep Power
    388
    Originally Posted by sayayinx
    Hello,

    I am new to the Unix world, and ...
    ...
    Any help would be greatly appreciated,

    Thank you!
    Try:
    Code:
    awk '/^PL/{print $1}' my_file.txt

    PS if the input file actually contains the numbering (1.-...), then use this:
    Code:
    awk 'substr($1,4,2) == "PL" {print substr($1,4)}' my_file.txt
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0
    Thank you very much, the second line worked!

    Originally Posted by LKBrwn_DBA
    Try:
    Code:
    awk '/^PL/{print $1}' my_file.txt

    PS if the input file actually contains the numbering (1.-...), then use this:
    Code:
    awk 'substr($1,4,2) == "PL" {print substr($1,4)}' my_file.txt
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,477
    Rep Power
    1752
    Be warned that if the files has lines that start with numbers of 10 or higher then you'll need to revisit the awk command.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,477
    Rep Power
    1752
    Just in case the file could start with numbers greater than 9, then this should work:

    Code:
    awk -F\- '{split($2,X," ");print X[1]}' sample
    or
    Code:
    awk '{match($0,/(PL[0-9]+)/); print substr($0,RSTART,RLENGTH)}' sample
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    859
    Rep Power
    388

    Cool


    Originally Posted by SimonJM
    Just in case the file could start with numbers greater than 9, then . . . .
    Correct!
    Thanks for the regex.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,477
    Rep Power
    1752
    Originally Posted by LKBrwn_DBA
    Correct!
    Thanks for the regex.
    Never used it before in awk (beyond the ~ comparison operator), and I thought you should be able to do something with it ...
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo