Find the answer to your Linux question:
Results 1 to 6 of 6
hello users I use this syntax in a bash-script on kubuntu: ls -lah --sort=size `cat $LOGFILE.tmp` | awk '{print $5 " " $8}' > $LOGFILE now, the problem is, if ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    28

    awk capture column 2 to the end

    hello users

    I use this syntax in a bash-script on kubuntu:

    ls -lah --sort=size `cat $LOGFILE.tmp` | awk '{print $5 " " $8}' > $LOGFILE

    now, the problem is, if a filename (usualy in $ has a space inside, then the rest of the name is in $9 etc...

    how can I say, and now from $8 to the Lineend?

    thank you.
    raphael

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Use $NF to refer to the last column,
    ls -lah --sort=size `cat $LOGFILE.tmp` | awk '{print $5 " " $NF}' > $LOGFILE

  3. #3
    Just Joined!
    Join Date
    Aug 2007
    Posts
    28
    hmm, not exactly, what I mean.

    if I have this situation:

    logfile:
    75k /home/user/Text-File1.txt
    72k /home/user/Text-File2.txt
    78k /home/user/Text File3.txt

    notice the "space" in the third line!


    then process through:
    cat logfile | awk '{print $2}'

    then the third line is only: "/home/user/Text"

    and not the exact Filename... how can I change that?

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    if you have Python
    Code:
    #!/usr/bin/env python
    for lines in open("logfile"):
        lines = lines.strip().split(" ",1)
        filename = lines[1]
        size = lines[0]
        print filename,size

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Use cut (and not cat!)
    Code:
    cut -d" "  -f2-  logfile

  6. #6
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    If all filenames starts with "/", you can use awk like this:
    awk 'BEGIN{FS=" /"} {print "/"$2}' logfile

Posting Permissions

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