CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    [RESOLVED] BASH: read from end of file upwards

    Hi all,

    I'm not sure if this is the right place to ask for a doubt in Bash programming. If it is not, please sorry.

    I need to read a text file from last line to first line, ie in reverse order. To read I do:
    Code:
    cat $VAR | while read line; do  
    	INPUT=$(echo ${line})
    	echo "./dplt-catalog.sh undeploy-'$INPUT'"
    done
    where $VAR contains the name of the file. My first try was to read whole file counting the lines in file. Once I'd know the number of lines I'm able to read the until the last line and treat, then I read again to the second last and treat it, and so forth.
    This looks terrible, right? Because in need to read file #lines + 1 times. I'm quite sure Linux/UNIX has a command or a way to do it much more simple. For example to list the contains of a directory in reverse alphabetical order:
    Code:
    ls -l | sort -r
    So, I'm searching something like this. I hope you understand what I try.
    Any hint, please?

    Thanks a lot

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: BASH: read from end of file upwards

    I found a possible solution:
    Code:
    cat $VAR -n | sort -r -n | cut -f 2-
    Anyway, any other hint or tip will be very welcome.

    Albert.
    Last edited by AlbertGM; March 3rd, 2009 at 12:35 PM.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  3. #3
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: BASH: read from end of file upwards

    There is a native coreutil:
    Code:
    tac <filename>
    produces reversed output line by line, pretty like cat.

  4. #4
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: BASH: read from end of file upwards

    А на этом форуме пишут по-русски?)

  5. #5
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: BASH: read from end of file upwards

    Quote Originally Posted by AmadeusAD View Post
    There is a native coreutil:
    Code:
    tac <filename>
    produces reversed output line by line, pretty like cat.
    That's exactly what I was searching.... I've been wasting my time for 2 or 3 hours... but I learned many things
    Thanks a lot!!!!

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  6. #6
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: BASH: read from end of file upwards

    Quote Originally Posted by AmadeusAD View Post
    А на этом форуме пишут по-русски?)
    I already have many problems in English. Russian is impossible.
    Well, if it's Russian... or is it Ukrainian?

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  7. #7
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: BASH: read from end of file upwards

    Sorry, I posted that occasionally, just for fun

    Actually, I like the process of spending hours solving a problem too, that brings a lot to one's qualification. And was trying to construct something like the command line you showed above (of three commmands using pipes). Composed that quickly on perl, but in bash you were quicker Anyway, the string manipulation tasks, if they cannot be performed by a set of standard utils or it looks clumsy, have better to be performed with perl, awk, sed or something of that series (if there is not particular requirements).

  8. #8
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: BASH: read from end of file upwards

    That was Russian, you are right
    The question asks whether one can post in Russian on this forum. Nevermind

  9. #9
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: BASH: read from end of file upwards

    Quote Originally Posted by AmadeusAD View Post
    That was Russian, you are right
    The question asks whether one can post in Russian on this forum. Nevermind
    You can post... but I guess few people will be able to understand, and even less to answer

    Quote Originally Posted by AmadeusAD
    if they cannot be performed by a set of standard utils or it looks clumsy, have better to be performed with perl, awk, sed or something of that series (if there is not particular requirements).
    No, there's no requirement, but I don't know nothing about perl, awk or sed. Well, in fact, I don't know almost nothing about BASH, but I need to make some scripts in Linux.... so I must learn

    Thanks again

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured