I'm trying to determine where to cut off a log in order to shrink its size. The log was started in 2010 and has been appended to by scripts that run daily since then. I'm grepping each line of the log to pull out lines that have dates in them, and then I want to grab the last 4 characters of those lines as that represents the year. Then I can determine on what line the year 2018 first appears for example, and truncate the file above that.
I'm trying to use tail -c 4 to grab the last 4 characters of each line, but I keep getting "cannot open input" error from tail.
Code:
#!/bin/bash
date=$(grep ' EST ' input.log)
IFS=$'\n'
for line in $date
do
printf "%s\n" "$line" > output.tmp
chmod 777 output.tmp
echo $(tail -c 4 output.tmp)
done
When I run this code with just "tail output.tmp", with no options, it works as expected and outputs the full line that is currently being iterated.
But when I try to use tail -c 4, that's when I get the "tail: cannot open input" error.
I have checked the man page for tail and -c option is available, so what am I doing wrong? Or is there a better way to approach this besides using tail? (I do not have grep -o option available on my system).
You don't need a temp file:
#!/bin/bash
date=$(grep ' EST ' input.log)
IFS=$'\n'
for line in $date
do
echo ${line: -4}
done