Computing.Net > Forums > Unix > Reading from a file

Reading from a file

Reply to Message Icon

Original Message
Name: coop
Date: July 6, 2005 at 06:23:40 Pacific
Subject: Reading from a file
OS: XP
CPU/Ram: 2Gighrtz Pentium 4/512mb
Comment:

Hi
I have a script that reads from a text file and in the script I have conditional statements. The script works if I create a text file from scratch, but for some reason, my conditional statements do not work if my text file comes from a zip file.
ie: I use the command: unzip myfolder/myfile.zip -d myfolder
and then I read the text file that comes from the zip.
I have no idea what's going on. Any help would be greatly appreciated.

Thanks
Chris


Report Offensive Message For Removal


Response Number 1
Name: coop
Date: July 6, 2005 at 07:29:38 Pacific
Reply: (edit)

Hi

My colleague determined that I had ^M at the end of each line in the text file that I extracted. Why is it that the ^M character does not show up when I cat the file?

I now need to find a way to remove the ^M's from my file. I believe I saw something in the forums about this.

Thanks


Report Offensive Follow Up For Removal

Response Number 2
Name: Jim Boothe
Date: July 6, 2005 at 11:24:38 Pacific
Reply: (edit)

Control characters do not generally have displayable graphics associated with them. They are not for display, but for control purposes. The cat command simply writes the data to stdout, and what happens happens. For example, if it writes a line to the screen that contains a tab (this is also a control character), the tab "character" is not displayed, but the output device reacts to it by advancing to the next predefined tab position.

But the vi editor needs to be more helpful because we are making changes to the data and we need to know each character that is present, including the control characters. So vi displays a ^M on the screen for a Ctrl-M character. And you can tell that it is in fact a single control character because you will advance across it in a single keystroke (whereas it would take you two keystrokes to advance across two actual characters ^M.

Strip these with:

sed 's/^M//' textfile > textfile.new

And you have to enter the ^M by typing Ctrl-V followed by Ctrl-M.


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: July 8, 2005 at 23:32:36 Pacific
Reply: (edit)

Jim has done a good job of describing the control-m issue. However, embedding ^M in a script isn't very portable. This script also removes ^M:

tr -d '\015\032' < file > newfile

or

sed 's/'"$(printf '\015')"'$//
s/'"$(printf '\032')"'$//'file > newfile

The other character - 032 is the DOS EOF.

Regards,

Nails




Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Reading from a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge