unix help (sed command)

Discussion in 'Mac Basics and Help' started by scan, Feb 9, 2006.

  1. scan macrumors 6502

    Joined:
    Oct 24, 2005
    #1
    I am using the sed command to search a file for a particular pattern and replacing it with somehting depending on the pattern. what I'm having trouble with is replacing the whole line with the word and getting rid of the rest of the line. I don't want to manipulate the file though. just the output

    for example:

    12345blahblahblah

    abcde

    would be:

    numeric
    empty
    word

    but my expression does:

    numericblahblahblah


    Edit: my empty just doesn'st work. I don't knwo how to search for empty line
     
  2. mklos macrumors 68000

    mklos

    Joined:
    Dec 4, 2002
    Location:
    My house!
    #2
    Did you try looking at the man pages in UNIX?

    Just do:

    man sed

    and that will display the man (manual) pages for that command in UNIX.
     
  3. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #3
    I suspect that you need to make the pattern match the whole line, so that the whole line is what gets replaced when the pattern is matched. So instead of matching numerics and replacing them with the word "numeric", you want to match numerics-followed-by-anything and replace THAT with the word "numeric".

    Example:
    Code:
    sed -e 's/^[0-9][0-9]*.*$/numeric/g' <infile >outfile
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    A regular expression for an empty line is /^$/ so s/^$/empty/g should work.

    Personally I would do this in perl rather than sed.

    B
     
  5. Cooknn macrumors 68020

    Cooknn

    Joined:
    Aug 23, 2003
    Location:
    Fort Myers, FL
    #5
    Code:
    sed -e 's/^[0-9][0-9]*.*$/numeric/g' <infile >outfile
    That brings back memories :p
     
  6. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #6
    You could also use awk and some if/then/else logic.
     
  7. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #7
    ahhh, sed and awk. i love me some sed and awk.

    scan -- i don't think you need sed at all. to me, this is an awk program.

    Code:
    #!/usr/bin/awk -f
    
    #----------------------------------------------------------
    #
    # %Z%%M% %I% %G%
    #
    # foo
    #
    # sxz 2/10/06
    #
    #----------------------------------------------------------
    
    /[0-9*]/ {
            print "numeric"
    }
    
    /[a-zA-Z*]/ {
            print "word"
    }
    
    /^$/ {
            print "empty"
    }
    
     
  8. superbovine macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #8
    Doctor Q looks cooler! :p
     
  9. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #9
    what? elegance! simplicity! built-in recursion!

    ::shakes fist::

    :)
     
  10. fisty macrumors member

    Joined:
    Jan 30, 2006
    #10
    i dont understand why if ur on a unix course u come to this site to cheat tbh...
    wotn help ya....just give ya easy anwsers...

    whatever
     
  11. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #11
    actually I've been trying to figure this out for days now and I finally figureded it out BY MYSELF. I don't come on here to "cheat" so you say. I completely forgot I asked for help on here and eventually figured it out. I'm not one of those people who try to get things done by cheating. It wouldn't benefit me because I actually have to learn this stuff. its not just for marks


    oh yeah about thread topic, I know I dont' have to use sed but thats what the question asks for. infact I was discussing with my teacher at the question and she didn't knwo either. lol she kept suggesting solutions that wouldn't have worked. finally i figured it out and she copied down my answer
     
  12. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #12
    this reminds me that I have another question. I'm trying to list the bytes of the files/dir in a directory. I'm piping ls and cut but I'm having trouble cutting the right field. any suggestions?
     
  13. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #13
    Cut can cut either by fields (with -f) or by columns (with -c). You can use either with the output of ls. When you use fields, you need to consider what delimiter will be used and fields with variables amount of whitespace become a problem, but the first column is easy if you use " " (a space) as delimiter:
    Code:
    ls -l | cut -d" " -f1
    Example with columns:
    Code:
    ls -l | cut -c26-36
    To avoid the whitespace problem, you can switch from cut to awk, which counts fields between whitespace as humans do, e.g.,
    Code:
    ls -l | awk '{print $6,$7}'
     
  14. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #14

    I have to use ls, tr, and cut. can't use awk. but I already tried those suggestions. they dont' really work
     
  15. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #15
    I don't understand why the cut -c example wouldn't get you the filesizes (the output you want, if I understand you correctly), assuming your "ls" command output has the filesize in columns 26 thru 36. If not, you can adjust the column numbers.

    If you are supposed to use "tr", then the person posing the problem probably has something specific in mind. I suspect it is this:
    Code:
    ls -l | tr -s " " | cut -d" " -f5
    The tr command with the -s switch means to translate multiple spaces into one space, which takes care of the whitespace problem I referred to above. Then you can use cut -f with space as the field delimiter.

    For the record: The only case I know where this will fail is when the groupname is very long and/or the filesize is very big, such that those two fields run together with no intervening space, or similarly if the username and groupname run together. But I'm sure that's not a concern for your purposes.
     
  16. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #16
    thanks. i just figured it out. i was so happy i was dancing
     
  17. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #17
    i'm a fan of cut, but sometimes it just can't extract the information i want. in that case, i use xargs.
     
  18. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #18
    Care to give an example?
     
  19. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #19
    i'm struggling to think of a good xargs example. the project i'm thinking of was in the early-mid 90's, and i wasn't allowed to take any code with me that i could now reference.

    but, i will illustrate an example of where cut is deficient (you already demonstrated how to use awk to get around it). the instances where i used xargs were similar, though more complex (where i was processing the extracted data).

    the example is grabbing block sizes from a directory:

    % ls -ls
    total 9048
    1520 -rwxr-xr-x 1 zim staff 713656 May 9 2001 102-0300_IMG.JPG*
    1416 -rwxr-xr-x 1 zim staff 661821 May 9 2001 103-0301_IMG.JPG*
    992 -rwxr-xr-x 1 zim staff 457411 May 9 2001 103-0302_IMG.JPG*
    1024 -rwxr-xr-x 1 zim staff 470563 May 9 2001 103-0303_IMG.JPG*
    1400 -rwxr-xr-x 1 zim staff 651717 May 9 2001 103-0304_IMG.JPG*
    1128 -rwxr-xr-x 1 zim staff 514108 May 9 2001 103-0305_IMG.JPG*
    1568 -rwxr-xr-x 1 zim staff 737366 May 9 2001 103-0306_IMG.JPG*
    %


    because the blocksizes are of different lengths, some lines are left with padded white space (notice i subbed in -1 for -l to avoid the "total" line):

    % ls -1s | cut -f 1 -d " "
    1520
    1416

    1024
    1400
    1128
    1568
    %


    as you showed above, awk solves this:

    % ls -1s | awk '{print $1}'
    1520
    1416
    992
    1024
    1400
    1128
    1568
    %


    Doc Q -- if you do know how to solve the example above using cut, please post it.
     
  20. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #20
    Did you mean to show it with a leading space, like this?
    Code:
    1520 -rwxr-xr-x 1 zim staff 713656 May 9 2001 102-0300_IMG.JPG*
    1416 -rwxr-xr-x 1 zim staff 661821 May 9 2001 103-0301_IMG.JPG*
     992 -rwxr-xr-x 1 zim staff 457411 May 9 2001 103-0302_IMG.JPG*
    1024 -rwxr-xr-x 1 zim staff 470563 May 9 2001 103-0303_IMG.JPG*
    1400 -rwxr-xr-x 1 zim staff 651717 May 9 2001 103-0304_IMG.JPG*
    1128 -rwxr-xr-x 1 zim staff 514108 May 9 2001 103-0305_IMG.JPG*
    1568 -rwxr-xr-x 1 zim staff 737366 May 9 2001 103-0306_IMG.JPG*
    In other words, are we talking about a case where the sizes in the first column are right-justified?
     
  21. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #21
    yes and yes.
     
  22. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #22
    Cut -c isn't ideal for this output. Although a particular display will have the sizes in a fixed range of columns, you don't know the width since it depends on the longest filesize. And the spaces would confuse cut if you used cut -f.

    What I didn't understand is how xargs would help.
     
  23. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #23
    clearly, awk is the more straightforward step after cut. what i was remembering (from a project about 15 years ago), was encountering some issue w/ cut and using xargs as a workaround, but i can't recall what it was i was trying to do (and it must have been more involved than just grabbing a column of info).

    so -- probably best to ignore my xargs comment. sorry for any confusion.
     

Share This Page