Discussion:
[9fans] trailing newline in awk
(too old to reply)
dexen deVries
2012-01-26 12:15:09 UTC
Permalink
hi list,

can't wrap my head around this: in an awk script, how to take some action if a
file lacks last trailing Line Feed?

i want to modify files with contents like:
text LF
text LF
text

and leave undisturbed files with content like:
text LF
text LF
text LF

in other words, i want to either:
- match line that is empty and is last line of file (awk doesn't seem to take
any action on such line), or, alternatively,
- match a line that is last line of file and lacks terminating LF.
--
dexen deVries

[[[↓][→]]]

Already many of the mutants disguised as human beings are walking the streets
of Earth's cities.
-- Music Instructor, ``Electro City''
hugo rivera
2012-01-26 13:17:08 UTC
Permalink
Seems impossible to do in awk, but I could be wrong. In ssam is easy:

% ssam -ne 'y/.+\n/p' file

prints the last line if it's missing the line feed.
Post by dexen deVries
hi list,
can't wrap my head around this: in an awk script, how to take some action if a
file lacks last trailing Line Feed?
text LF
text LF
text
text LF
text LF
text LF
 - match line that is empty and is last line of file (awk doesn't seem to take
any action on such line), or, alternatively,
 - match a line that is last line of file and lacks terminating LF.
--
dexen deVries
[[[↓][→]]]
Already many of the mutants disguised as human beings are walking the streets
of Earth's cities.
 -- Music Instructor, ``Electro City''
--
Hugo
Steve Simon
2012-01-26 13:20:40 UTC
Permalink
Interesting one.

Both sed and awk has treat EOF as a line terminator
so either of these will work:

awk '{print}' junk
sed '' junk

but sam doesn't seem to regard a line without a newline as
having an end, so its not simple here:

{ echo ',x/$?\n/ c/\n/' ; echo '1,$p' } | sam -d junk >[2] /dev/null

-Steve
Jonathan Cast
2012-01-26 15:32:25 UTC
Permalink
Post by dexen deVries
hi list,
can't wrap my head around this: in an awk script, how to take some action if a
file lacks last trailing Line Feed?
text LF
text LF
text
text LF
text LF
text LF
If this is all you're trying to do,

awk 1 file

works, no?

jcc

Continue reading on narkive:
Loading...