Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script question, why is running different in Linux?

Status
Not open for further replies.

flaw

Programmer
Sep 24, 2002
2
US
I have a comma delimited file that has a set of data every four lines. I run the following script to get the four lines all on one line. I then import the file into a database. The script works fine when I run it under Windows with cgywin. When I run it in Bash on Mandrake, the script appears to be fine but it has an extra space where each line is joined.

For example, line 1 is A,B,C and line 2 is D,E,F. The script on Windows makes it Line 1 A,B,C,D,E,F,etc. On Mandrake, the script makes it A,B,C ,D,E,F ,. The problem is that the database program reads these spaces as line breaks (so I guess that means they may not be spaces after all but something else).

I want to be able to run this script on Mandrake because my Mandrake is on a much faster computer. Any hints as to why its doing this? Otherwise the script appears to run fine.

Also, I've been trying to fool around with awk to fix the file after running the first script on Mandrake, but since I am so new with it, I've been having some difficulty. Any help there will be appreciated.

The script is below:

#!/bin/sh

COUNT=0
CURRENT_STR=""

NEXT=""
while [ 1 ]; do
COUNT=0
while [ $COUNT -lt 4 ]; do
read NEXT
if [ "x$NEXT" = "x" ]; then
exit;
fi

if [ "x$CURRENT_STR" = "x" ]; then
CURRENT_STR=$NEXT
else
CURRENT_STR="$CURRENT_STR,$NEXT"
fi
COUNT=`expr $COUNT + 1`
done
echo $CURRENT_STR
CURRENT_STR=""

done

 
Not much help, but it runs as expected on Solaris!

Greg.
 
Does the file contain any carriage returns? If you have dos2unix, you could try running it on the file first, and run your script again after. It might fix your problem. //Daniel
 
Tend to agree with Daniel here ... open the file in vi and enter the command set list. This will display any control characters in your file.

Greg.
 
Okay,did that. It shows a ^M in every one of those spaces.

Can someone give me a hint on how to get rid of them?

Thanks for the help.
 
As I said previously, try running dos2unix on the file. If you don't have it, you could easily write a Perl script to do it. It would look something like this:
Code:
#!/usr/bin/perl
# Change the above to your path to perl

open (INFILE, $ARGV[1]);
open (OUTFILE, ">$ARGV[2]");
while (<INFILE>)
{
    s/\r//g;
    print OUTFILE $_;
}
close (INFILE);
close (OUTFILE);
To run this script, execute [tt]/path/to/perl scriptname inputfile outputfile[/tt] or just [tt]/path/to/scriptname inputfile outputfile[/tt] if Perl is in your /etc/shells file. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top