Find the answer to your Linux question:
Results 1 to 3 of 3
i'm new in linux so pls,have mercy ....: I have to display from the list of files given as parameters the name of the file with the largest number of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    1

    Unhappy awk help


    i'm new in linux so pls,have mercy ....:
    I have to display from the list of files given as parameters the name of the
    file with the largest number of words and their number.(with AWK)
    pls help me :-s

  2. #2
    Linux User stokes's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    274
    You don't need awk if you just want to count the number of words in some files. you can use wc -l. for example:

    Code:
    wc -l *.txt
        500 hist.txt
      49612 itinerary.txt
          0 testfile.txt
      50112 total
    I would probably avoid awk if you are a brand new linux user unless you specifically want to use it for some reason.
    Registered Linux user #389109
    My Semi-Linux Blog

  3. #3
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    145

    Post

    It seems he needs to use awk to capture wc's output, like

    Code:
    SZ=`wc -l file.txt | awk '{ print $1; }'`
    to get a size of 'file.txt' in variable $SZ. So you would need some kind of a loop in a script, that iterates over file names in current directory and checks its sizes. So here is an example:

    Code:
    FILES=`ls .`
    
    BIGGEST_SZ=0
    
    
    for NAME in $FILES
    do
    SZ=`wc -l $NAME | awk '{ print $1; }'`
    if [ $SZ -gt $BIGGEST_SZ ]; then
    BIGGEST_SZ=$SZ
    BIGGEST_NAME=$NAME
    fi
    done
    
    echo "$BIGGEST_NAME has the biggest size: $BIGGEST_SZ"
    Anyway, it looks like a homework

Posting Permissions

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