Software applications and operating systems usually represent a newline with one or two control characters:
* Systems based on ASCII or a compatible character set use either LF (Line feed, 0x0A) or CR (Carriage Return, 0x0D) individually, or CR followed by LF (CR+LF, 0x0D0A); see below for the historical reason for the CR+LF convention. These characters are based on printer commands: The line feed indicated that one line should feed out of the printer, and a carriage return indicated that the printer carriage should return to the beginning of the current line.
o LF: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, etc.), BeOS, Amiga, RISC OS, and others
o CR+LF: DEC RT-11 and most other early non-Unix non-IBM OSes, CP/M, MP/M, MS-DOS, OS/2, Microsoft Windows
o CR: Commodore machines, Apple II family and Mac OS up to version 9
This thread has many useful comments. However, the OP has not returned to tell what s/he used.
One way to ask these kinds of questions is to include a sample of what the line really looks like, such as:
Or with a different choice of arguments:
Then responders may more easily provide advice ... cheers, drl
Hi,
I have this file which has some octal NULL characters (\000). I need to replace these characters with an ASCII NULL.
I've tried using Perl, the UNIX tr command..
History of this
I received a COBOL generated file, ran the od command to convert to a xxx byte per record file.
Now,... (3 Replies)
Hi All,
Is there any way to append a newline character at the end of a file(coma-separated file), through shell script?
I need to check whether newline character exists at the end of a file, if it does not then append it.
Regards,
Krishna (1 Reply)
Hi All,
I have named a file with current date,time and year as follows:
month=`date | awk '{print $2}'`
date=`date | awk '{print $3}'`
year=`date | awk '{print $6}'`
time=`date +%Hh_%Mm_%Ss'`
filename="test_"$month"_"$date"_"$year"_"$time".txt"
> $filename
The file is created with a... (2 Replies)
Hi,
I have a .txt file which has a tilde(~) in it.
All that I want is to break into a newline whenever there is an occurence of '~'.
I have tried SED to do that but I could not succeed.
I would appreciate if I can get a shell script(ksh) for this problem real quick.
Thanks in advance.
... (5 Replies)
Hi Guyz,
I have an XML message in following format:
I want my contents to be formatted in following order:
i.e. I want a newline after every XML tag end.
How to do this?
Thnx in advance. (5 Replies)
How to replace null with space?
I want to make each line with 80 characters. If any line contains only 5 characters and remaining is null, then i want to make it as 80 characrets where 5 is original characters and remaining 75 characters will be null..
NULL can come in between the line,... (3 Replies)
Hi All,
I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists.
foreach file (`ls $dd_PLAYCARD_EDI_IN`)
if ( -f $dd_PLAYCARD_EDI_IN/${file} ) then
cat -n... (4 Replies)
Hi Gurus,
Need help. I'm a beginner in Unix. I have a requirement, need to add or append newline (\n) character in file.
Sample Data:
1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#Output:
1|Main|Test|~#
2|Main|Hello|~#
3|Main|Unix|~#
4|Main|File|~#\n -- append only... (13 Replies)
Hello all,
I have maybe a simple Problem - but I do not know to handle it.
All what I want, is to write a line to file without a newline at the end. It works with "echo -n" for all lines, but not for the last one. At the end of the file is always a "0a" (hex)
My small script:
... (10 Replies)
I have a variable like below:
str1="10.9.11.128\n-rwxr-xr-x user1 2019-12-29 17:53 /var/branch/custom/tg.xml 286030210\n10.9.12.129\n-rwxr-xr-x user1 2019-12-29 17:53 /app/branch/custom/tg.xml 286030210\n10.9.20.130\n-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/tg.xml 286030210"
I... (8 Replies)
Discussion started by: mohtashims
8 Replies
LEARN ABOUT DEBIAN
file::countlines
File::CountLines(3pm) User Contributed Perl Documentation File::CountLines(3pm)NAME
File::CountLines - efficiently count the number of line breaks in a file.
SYNOPSIS
use File::CountLines qw(count_lines);
my $no_of_lines = count_lines('/etc/passwd');
# other uses
my $carriage_returns = count_lines(
'path/to/file.txt',
style => 'cr',
);
# possible styles are 'native' (the default), 'cr', 'lf'
DESCRIPTION
perlfaq5 answers the question on how to count the number of lines in a file. This module is a convenient wrapper around that method, with
additional options.
More specifically, it counts the number of line breaks rather than lines. On Unix systems nearlly all text files end with a newline (by
convention), so usually the number of lines and number of line breaks is equal.
Since different operating systems have different ideas of what a newline is, you can specifiy a "style" option, which can be one of the
following values:
"native"
This takes Perl's "
" as the line separator, which should be the right thing in most cases. See perlport for details. This is the
default.
"cr"
Take a carriage return as line separator (MacOS style)
"lf"
Take a line feed as line separator (Unix style)
"crlf"
Take a carriage return followed by a line feed as separator (Microsoft Windows style)
Alternatively you can specify an arbitrary separator like this:
my $lists = count_lines($file, separator => 'end{itemize}');
It is taken verbatim and searched for in the file.
The file is read in equally sized blocks. The size of the blocks can be supplied with the "blocksize" option. The default is 4096, and can
be changed by setting $File::CountLines::BlockSize.
Do not use a block size smaller than the length of the separator, that might produce wrong results. (In general there's no reason to chose
a smaller block size at all. Depending on your size a larger block size might speed up things a bit.)
Character Encodings
If you supply a separator yourself, it should not be a decoded string.
The file is read in binary mode, which implies that this module works fine for text files in ASCII-compatible encodings, including ASCII
itself, UTF-8 and all the ISO-8859-* encodings (aka Latin-1, Latin-2, ...).
Note that the multi byte encodings like UTF-32, UTF-16le, UTF-16be and UCS-2 encode a line feed character in a way that the 0x0A byte is a
substring of the encoded character, but if you search blindly for that byte you will get false positives. For example the LATIN CAPITAL
LETTER C WITH DOT ABOVE, U+010A has the byte sequence "0x0A 0x01" when encoded as UTF-16le, so it would be counted as a newline. Even
search for "0x0A 0x00" might give false positives.
So the summary is that for now you can't use this module in a meaningful way to count lines of text files in encodings that are not ASCII-
compatible. If there's demand for, I can implement that though.
Extending
You can add your own EOL styles by adding them to the %File::CountLines::StyleMap hash, with the name of the style as hash key and the
separator as the value.
AUTHOR
Moritz Lenz <http://perlgeek.de>, <mailto:moritz@faui2k3.org>
COPYRIGHT AND LICENSE
Copyright (C) 2008 by Moritz A. Lenz. This module is free software. You may use, redistribute and modify it under the same terms as perl
itself.
Example code included in this package may be used as if it were in the Public Domain.
DEVELOPMENT
You can obtain the latest development version from <http://github.com/moritz/File-CountLines>:
git clone git://github.com/moritz/File-CountLines.git
perl v5.10.1 2010-08-19 File::CountLines(3pm)