Shell Programming Help Please

Discussion in 'Scripting & Programming' started by zimbo, Feb 21, 2008.

  1. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    Hi

    Im working on a shell programming project at uni and wanted to ask you guys something..

    Code:
    wc -l SampleFile
    Is the command im running and assume I have 42 lines in SampleFile, output would be:

    Code:
    42 SampleFile
    How do remove the "SampleFile" and just end up with 42?

    Thanks for the help!
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  2. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    Pipe the output of wc into sed and use a regular expression to select and delete the a-z and A-Z characters.
     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1
  3. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    Code:
    sed | wc -l SampleFile /[A..Z,a..z]
    I cant try it out at the moment.. but something like that? :rolleyes

    btw how you doing buddy? long time.....
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  4. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    If you're doing it in a script use a variable that will equal the file name, but manually you can do it like this: wc -l SampleFile | sed -e 's/SampleFile//'

    Here's how you would do it from a script using a variable so it can be automated to use with any file name:
    a='SampleFile'
    wc -l $a | sed -e 's/'"$a"'//'

    Just remember your script has to have a way to read the file name into the variable.
     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1
  5. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    Cheers freddy!! 8) gonna use the manual one
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  6. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    I figured awk would do the same so I played around with it. Here's another solution.

    wc -l filename | awk '{print $1}'
     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1
  7. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Another way of doing this is:
    set `wc -l filename` ; echo $1

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  8. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    And the simplest one of all (I think):

    cat filename | wc -l

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  9. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    Thanks for all the help guys! 8)
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.