hankknight
asked on
Linux: Loop through file BACKWARDS
This loops through a file starting at the beginning:
#!/bin/bash
while read LINE
do
if [ $(( i )) -lt 5 ];
then
i=$((i+1))
echo $LINE
else break
fi
done <"$1"
How can I loop through the file starting at the end and going backward one line at a time? Can -tail be used for this? I do not want to read only the last 5 lines because I want to use a more complicated condition than the one above. Instead, I want to read the file backwards, starting at the last line then moving up one line at a time.
ASKER
How can I do this with the code I posted? This does NOT work:
#!/bin/bash
while read LINE
do
if [ $(( i )) -lt 5 ];
then
i=$((i+1))
echo $LINE
else break
fi
done < tac "$1"
what command you input and what error is ?
Please send it out
Please send it out
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That does what I want. Is there a way to do that without creating a temporary junk file?
I think it already asnwered your question
you can add rm -rf junk.txt in your script to delete the temp file .
creating a temp file is not harmful
you can add rm -rf junk.txt in your script to delete the temp file .
creating a temp file is not harmful
Thanks
Have a nice day
Duncan
Have a nice day
Duncan
>> Is there a way to do that without creating a temporary junk file <<
Yes, and please see my last comments to your thread https://www.experts-exchange.com/questions/28295367/Linux-Count-lines-in-file-that-begin-with.html where I already provided this solution.
#!/bin/bash
while read LINE
do
if [ $(( i )) -lt 5 ];
then
i=$((i+1))
echo $LINE
else break
fi
done <<< $(tac $1)
Yes, and please see my last comments to your thread https://www.experts-exchange.com/questions/28295367/Linux-Count-lines-in-file-that-begin-with.html where I already provided this solution.
#!/bin/bash
while read LINE
do
if [ $(( i )) -lt 5 ];
then
i=$((i+1))
echo $LINE
else break
fi
done <<< $(tac $1)
woolmilkporc,
Yes, it is correct no need for creating temp file from your code.
I tried your code at my side, but the output of result is different from my result.
hankknight,
could you also try it if you have time ?
Duncan
Yes, it is correct no need for creating temp file from your code.
I tried your code at my side, but the output of result is different from my result.
hankknight,
could you also try it if you have time ?
Duncan
ASKER
<<< $(tac $1)
That does not seem to be working on my OS. I have posted a question about that here:
https://www.experts-exchange.com/questions/28297339/Linux-Process-file-in-reverse.html
That does not seem to be working on my OS. I have posted a question about that here:
https://www.experts-exchange.com/questions/28297339/Linux-Process-file-in-reverse.html
To make this thread complete:
"$(tac $1)" must be enclosed in double quotes in order to keep the newline characters from being stripped off by the shell.
wmp
"$(tac $1)" must be enclosed in double quotes in order to keep the newline characters from being stripped off by the shell.
wmp
if so , using tac command
tac youfile.txt>output,txt
some Linux distrubtion, tail -r yourfile.txt that also works