Register
It is currently Sun Apr 20, 2014 10:05 am

while read, from a file with special characters [SOLVED]


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Jan 04, 2011 11:21 am   

Joined: Tue Jan 04, 2011 11:04 am
Posts: 2
Hello!
I am trying to make a script that would extract information from a text file. The problem is that the text file has double linebreaks (Not sure if that's the right name... in a text editor, typing "Return" twice).
I did this:
Code:
#!/bin/bash
if [ $1 ] && [ $2 ]; then
   if [ -e $2 ]; then
      rm $2
   fi
   while read sample1
   do
      if [ ${sample1:0:2} = 'mc' ]; then
         echo $sample1 >>$2
      fi
   done< $1
else
   echo usage $0 [ infile ] [outfile]
fi

Every time it encounters an empty line, it sends the following annoying message:
Quote:
./wiki2txt.sh: line 9: [: =: unary operator expected

How could I get rid of it?
Thank you in advance

EDIT: The infile has the following structure:
Quote:
== Group B ==
=== info ===
faefaefasdfaefsef
adfasefasefasfs

asefaefsefsdfefsfe

asfaefsefsfsfefsefseu

If the file doesn't have the equal signs, nothing happens. If there are equal signs and no double space, nothing happens. But together, they send me the said error.


Last edited by Geng on Wed Jan 05, 2011 10:02 am, edited 2 times in total.

Top
 Profile  
 PostPosted: Tue Jan 04, 2011 2:21 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
use more quotes!

always quote variables, and what may contain spaces, between single square brackets.


Top
 Profile  
 PostPosted: Wed Jan 05, 2011 4:33 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
99% of these types of errors are resolved by correct quoting. I'd say ~7% of all problems on this site are solved by correct quoting.

Code:
if [ "${sample1:0:2}" = 'mc' ]


Top
 Profile  
 PostPosted: Wed Jan 05, 2011 10:00 am   

Joined: Tue Jan 04, 2011 11:04 am
Posts: 2
Thank you! It worked as a charm :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP