You can read line by line of input using the read
bash command:
while read my_variable; do
echo "The text is: $my_variable"
done
To get input from a specific file, use the input redirect <
:
while read my_variable; do
echo "The text is: $my_variable"
done < my_logfile
Now, to get the last column, you can use the ${var##* }
construction. For example, if the variable my_var
is the string some_file_name
, then ${my_var##*_}
is the same string, but whith everything before (and including) the last _
deleted.
We come up with:
while read line; do
echo "The last column is: ${line##* }"
done < my_logfile
If you want to echo it to another file, use the >>
redirect:
while read line; do
echo "The last column is: ${line##* }" >> another_file
done < my_logfile
Now, to take away the querystring, you can use the same technique:
while read line; do
last_column="${line##* }"
url="${last_column%%\?*}"
echo "The last column without querystring is: $url" >> another_file
done < my_logfile
This time, we have %%?*
instead of ##*?
because we want to delete what's after the first ?
, instead of before the last. (Note that I have escaped the character ?
, which is special to bash.) You can read all about it here.
I didn't understand where to get the page hits, but I think the main idea is there.
EDIT: Now the code works. I had forgotten the do
bash keywork. Also, we need to use >>
instead of >
in order not to overwrite the another_file
every time we do echo "..." > another_file
. By using >>
, we append to the file. I have also corrected the %%
instead of ##
.