-
March 3rd, 2009, 11:46 AM
#1
[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: 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 :-)
-
March 3rd, 2009, 12:28 PM
#2
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 :-)
-
March 3rd, 2009, 12:38 PM
#3
Re: BASH: read from end of file upwards
There is a native coreutil:
produces reversed output line by line, pretty like cat.
-
March 3rd, 2009, 12:42 PM
#4
Re: BASH: read from end of file upwards
А на этом форуме пишут по-русски?)
-
March 3rd, 2009, 12:45 PM
#5
Re: BASH: read from end of file upwards
Originally Posted by AmadeusAD
There is a native coreutil:
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 :-)
-
March 3rd, 2009, 12:48 PM
#6
Re: BASH: read from end of file upwards
Originally Posted by AmadeusAD
А на этом форуме пишут по-русски?)
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 :-)
-
March 3rd, 2009, 01:01 PM
#7
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).
-
March 3rd, 2009, 01:05 PM
#8
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
-
March 4th, 2009, 04:14 AM
#9
Re: BASH: read from end of file upwards
Originally Posted by AmadeusAD
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|