Problem on reading file with "***" in the beginning of each line
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Problem on reading file with "***" in the beginning of each line
I am using the following code to read line by line from a file.
while read line
do
echo $line
done < logfile.out
The code is very common of course it works well in most situation. However, since the log file that I am going to read contains "***" at the beginning of each line. The while loop above will not work as expected. For example the log file contains,
*** Backup completed
*** Backup failed
When trying to read it using the code listed above, the output will become,
Any asterisk not quoted will be expanded by the shell, hence all files and folders present in the directory you are running it from will be displayed along with your string.
Thanks for everyone's reply. I am deeply appreciated.
By the way, the double quote does not help on solving the issue. Thus, I do it in a indirect way. Since I just only concern about the content of the line but not that three "***". I just remove them before read the line content. Below is my stupid solution.
Example of the log file content:
*** (warn) backup failed
*** (info) backup successful
*** (warn) backup did not start
The code to read this file.
#!/bin/bash
LOGFILE=/tmp/log.out
TEMPFILE=/tmp/temp.out
cat $LOGFILE | sed -r 's/^.{3}//' > $TEMPFILE
while read line
do
echo $line
Perform something on the content...
fi
done < "$TEMPFILE"
By the way, the double quote does not help on solving the issue. Thus, I do it in a indirect way. Since I just only concern about the content of the line but not that three "***". I just remove them before read the line content.
Although your solution seems to do what you want, it can be simplified.
I'm also curious why the double quotes do not work, but maybe this will help:
Code:
#!/bin/bash
LOGFILE="infile"
while read FIRST SECOND REST
do
# echo "$FIRST"
echo "$SECOND"
echo "$REST"
done < "$LOGFILE"
exit 0
When using the example data given in post #5, this will be the output:
Code:
$ ./tets.sh
(warn)
backup failed
(info)
backup successful
(warn)
backup did not start
The while read construct can use more then one variable. In the above code snippet it uses three (FIRST SECOND REST) variables to read a line. The first part [the ***] will be stored in FIRST, the backup status [(xyz)] will be stored in SECOND and the rest of the line in REST.
The # echo "$FIRST" part is there to show you that something can be done with it (even though you mention not needing it).
Last edited by druuna; 10-17-2012 at 02:24 AM.
Reason: grammar fix
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.