moderated >> Concatenation working weirdly on Unix

by Dan Harris » Tue, 27 Feb 2007 02:29:20 GMT

Hi there all,

Have a small but very confusing problem regarding differing behaviour
on Windows and Linux formats. The following code to concatenate pairs
of lines from a file works completely as expected on Windows:

# Get all the lines into a list.
my @lines_list = <INFILE>;

# Set our loop variable.
my $loop = 0;

while ($loop < @lines_list) {
my $first_line = $lines_list[$loop];
my $second_line = $lines_list[$loop + 1];
my $two_lines = '';

# If our first line is a blank (i.e. a newline character) skip.
if ($first_line eq '\n') {
$loop++;
print STDOUT "Skipping blank line.\n";
next;
}

if (defined($second_line)) {
# Not EOF.
if ($second_line eq '\n') {
# Second line is a blank line, so we only use the first.
$two_lines = $first_line;
} else {
# Set up our two line variable with a space inbetween.
chomp($first_line);
$two_lines = "$first_line $second_line";
}
} else {
# Second line is undefined, so use first alone.
$two_lines = $first_line;
}

# Make corrections in the line.
$two_lines = &make_corrections($two_lines);
(more code here...........)

It's the chunk:

# Set up our two line variable with a space inbetween.
chomp($first_line);
$two_lines = "$first_line $second_line";

which seems to work differently on my Linux machine. To demonstrate,
on Windows the following occurs:

First line is "Here is the first line..."
Second line is "and the second"
So $two_lines would become "Here is the first line... and the second".
On my Linux box however, $two_lines is set to " and the
secondline...". So essentially the space and the second line are being
pasted over the top of the first line! I'm baffled to be honest. Any
help hugely appreciated.

Thanks,

Dan Harris


moderated >> Concatenation working weirdly on Unix

by Christian Winter » Tue, 27 Feb 2007 14:27:14 GMT



[...]

Linux and Windows have different line endings for text files, Linux uses
only a newline, while Windows has a carriage return + newline.
From the looks of it you are accessing a Windows format file from Linux,
where the chomp() only removes the newline from the string.

If you want to use Windows text files on an *nix-ish OS, you have three
possibilites how to deal with it:
- convert the file before usage by uploading it via FTP in ASCII mode or
running a conversion tool like dos2unix or recode over it);
- set the variable for the line terminator accordingly to have chomp()
remove both characters;
- use the PerlIO abstraction layer for windows files with open:
open( I, "<:crlf", "windowsfile.txt" ) or die $!;
which implicitely converts all CR+LF to a simple newline, thus
making everything work without other changes to the code or the
data files.

See "perldoc -f chomp" and "perldoc PerlIO" for more detailed
explanations.

-Chris

moderated >> Concatenation working weirdly on Unix

by John W. Krahn » Wed, 28 Feb 2007 05:42:21 GMT


^^^^

^^^^

You are comparing your variables against a two character string consisting of
the backslash character and the 'n' character. Escape sequences have to be
interpolated in double quoted strings like "\n".



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall

moderated >> Concatenation working weirdly on Unix

by Dr.Ruud » Mon, 05 Mar 2007 03:30:37 GMT

Dan Harris schreef:


You are confusing value and presentation. Also print the length() of the
lines, and you'll see what I mean.

--
Affijn, Ruud

"Gewoon is een tijger."

Similar Threads

1. Weirdly damaged file system

I'm a relative Linux newbie with a filesystem that is displaying odd
behavior. I can't figure it out and I hoped one of you experts could
help me.

This is Red Hat 9.  I (foolishly) downloaded and installed a bunch of
new software, and when this was through, some of my directories had
disappeared. Specifically, /misc appeared empty although it used to
have three big subdirs in it.

This panicked me, not that the contents of the directories were so
valuable but once file start vanishing randomly who knows what is
going on.  This is an ext3 filesystem.

Here's the weird part:

fsck -fn on the filesystem (while mounted) reveals some problems
(orphaned inodes, corrupted lists, wrong block counts). But they can't
be fixed while the filesystem is mounted.

If I mount the filesystem with Knoppix (which mounts it as ext2) the
missing files have been miraculously restored.  Furthermore, fsck on
the filesystem reveals no problems.

Booting up the original Red Hat system in single-user mode has the
same behavior as Knoppix: files are there, fsck thinks everything is OK.

But in the normally running system, files are still gone and fsck 
reports damage.

In other words, the only state that reveals the problem is one that does
not allow it to be fixed! Argh!

I thought this might have something to do with journalling, so I
converted the filesystem to ext2 and then re-created the journal. This
had no effect.

Anyway, I'm stumped and so is my local neighborhood Linux wiz.  Anybody
want to lend me a clue?

2. Hyperlinks in Cells Wrapping Weirdly

3. concatenation of strings __FUNCTION__ now deprecated

4. virtual file that's a concatenation of other files - Linux

5. echo value of a variable formed by concatenation of string

On Sep 6, 8:37 pm, Robert Harris < XXXX@XXXXX.COM >
wrote:
> anshul makkar wrote:
> > Hi,
>
> > I am writing a simple script where I need to concatenate two strings
> > to form a new variable and print its value
>
> > summerwinter=192.168.2.1
> > season1='summer'
> > season2='winter'
> > echo "[${season1}] + [${season2}] = $[${season1}${season2}]"
>
> > This script gives the error :
> > 192.168.2.1: syntax error in expression (error token is ".168.2.1")
>
> > If instead of summerwinter=192.18.2.1 , I give summerwinter=100, then
> > script runs correctly.
>
> Try:
>
> eval result='$'$season1$season2
> echo "$season1 + $season2 = $result"
>
> (no arithmetic!)
>
> Robert
>
>
>
> > I know that error is due to misinterpretation of "." but I am not able
> > to figure out the alternative.
>
> > Please if you have any suggestion or hint, then please do share it.
>
> > Thanking You
> > Anshul Makkar

Yes, this one worked. Thanks a lot.

Anshul Makkar

6. echo value of a variable formed by concatenation of string

7. string concatenation

In article < XXXX@XXXXX.COM >,
  XXXX@XXXXX.COM  (Shri) wrote:

> hi all,
> 
>       i am writing a code in which i have a char buffer "cwdir[]"
> which hold the current working directory by calling the function
> getcwd(). later i change the directory to "/" as i have to make my
> code Deamon. and later again i want to run some other executable
> available at the path holded by the "cwdir[]" using the system()
> system call. presently i concatenate program name (to be executed) to
> the "cwdir[]" and use system(chdir)to run the program.
> 
> do we have any facility to automate this process as i need to  run
> many other programs also using the system() system call for ex as we
> can use the ## symbol to cancatenate in macro ...

I've taken comp.lang.c and comp.std.c out of the newsgroups, as this is 
entirely Unix-specific; the C language says nothing about working 
directories or how programs are found by system().

The simplest solution I think would be for you to prepend the directory 
in cwdir[] to your PATH environment variable.

oldpath = getenv("PATH");
newpath = malloc(strlen(oldpath) + strlen(cwdir) + 2);
sprintf(newpath, "%s:%s", cwdir, oldpath);
setenv("PATH", newpath, 1);

-- 
Barry Margolin,  XXXX@XXXXX.COM 
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

8. SCRIP SHELL : CONCATENATION FICHIERS SELON CRITERES DE LEUR NOM ..AU SECOURS