LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2012, 07:13 AM   #1
abc00001
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
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,

gconfd-root kde-root ksocket-root orbit-root ssh-gKQZBa3854 temp.out test.sh test.txt VMwareDnD vmware-root Backup completed

gconfd-root kde-root ksocket-root orbit-root ssh-gKQZBa3854 temp.out test.sh test.txt VMwareDnD vmware-root Backup failed

Is there any way to read the file line by line properly? As the log is generated by a vendor software, the format cannot be changed.

Thanks a lot for help.
 
Old 10-16-2012, 07:19 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Try this:
Code:
while read line
do
echo "$line"
done < logfile.out
Mind the double quotes....

EDIT: Using set -f turns off globbing and in this case your original solution (no double quotes) works.

Last edited by druuna; 10-16-2012 at 07:23 AM.
 
Old 10-16-2012, 09:04 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
try this:

Code:
cat logfile.out
 
Old 10-16-2012, 10:19 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.

druuna has provided multiple solutions to help.
 
Old 10-16-2012, 09:28 PM   #5
abc00001
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
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"

rm $TEMPFILE
exit 0
 
Old 10-16-2012, 11:35 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. Please use [code] and [/code] tags
2. What do you mean by 'double quote does not help'? What did you try, what happened, what should have happened?
 
Old 10-17-2012, 01:55 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by abc00001 View Post
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
 
Old 10-17-2012, 10:01 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
The other thing to consider with your second solution is, what if a line does not start with "***"? You may remove valuable information.

Also, +1 to being curious how the double quotes did not work as I have been unable to reproduce what you have shown when using quotes?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sql: put "beginning of line" and "end of line" within a charset... masavini Programming 7 09-19-2012 08:03 AM
[SOLVED] Script to insert line into file that contains multiple """ characters tara Programming 2 02-15-2012 06:43 PM
renaming file names beginning with "-" prashln Linux - General 5 05-01-2010 01:47 AM
C++ input file reading "line by line" assamite Programming 2 05-31-2008 04:54 PM
Escaping "-" at the beginning of a file! thebored Linux - General 2 04-08-2005 06:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration