Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Print the last line or change the last line

11 views
Skip to first unread message

foxidrive

unread,
Jun 6, 2012, 1:02:22 AM6/6/12
to

Hi,

I have text files (in Windows 7) in which the last line is just a CRLF (an empty line).


My script using GnuSED V4.2.1 removes CRLF lines but I wish to keep the last line - for appending a set of files into a single file.
Is there some way I can modify the last line so that it isn't deleted, perhaps by adding > to it, or can I add a blank line at the end of each file using the same sed command?


This is my script:

sed -e "/^ *$/d" -e "/^$/d" "file.txt" > "newfile.txt"



Thanks
Mic

foxidrive

unread,
Jun 7, 2012, 2:02:50 PM6/7/12
to

foxidrive

unread,
Jun 7, 2012, 2:12:47 PM6/7/12
to
Sorry for the duplicates - I wanted to clarify that the lines I refer to as CRLF are blank lines with no characters, just a CRLF.

An example is a file format like this:

===start file===
Book: A brief history of time

Author: You know who

Synopsis: 42

===end file===

and similar files with the same format and and I'd like to end up with this

===start file===
Book: A brief History of time
Author: You know who
Synopsis: 42

Book: Flight
Author: Another author
Synopsis: A book about flight

===end file===


Hi,

I have text files (in Windows 7) in which the last line is just a CRLF (an empty line) but there are other blank lines in it.
My script using GnuSED V4.2.1 removes blank lines that only have a CRLF but I wish to keep the last blank line.

Stephane Chazelas

unread,
Jun 7, 2012, 3:18:19 PM6/7/12
to
2012-06-08 04:12:47 +1000, foxidrive:
[...]
> sed -e "/^ *$/d" -e "/^$/d" "file.txt" > "newfile.txt"

sed '$q;/[^ ]/!d'

--
Stephane

foxidrive

unread,
Jun 9, 2012, 12:51:05 AM6/9/12
to
On Friday 08/06/2012 05:18, Stephane Chazelas wrote:
> 2012-06-08 04:12:47 +1000, foxidrive:
> [...]
>> sed -e "/^ *$/d" -e "/^$/d" "file.txt" > "newfile.txt"
>
> sed '$q;/[^ ]/!d'

I'm not sure that it has the same function, Stephane. I tried to adapt it to GnuSED but didn't have success.



--
Mic

Stephane Chazelas

unread,
Jun 9, 2012, 3:32:17 PM6/9/12
to
2012-06-09 04:51:05 +0000, foxidrive:
That should work with any shell. The quoting is Bourne-like
shell syntax as found on all Unix-like systems.

What you want is the string "$q;/[^ ]/!d" to be passed as the
one and only argument to sed.

It says that, on the last line ($), sed should quit (q) (after
having displayed the line).

For any other line, unless it matches /[^ ]/ (contains any
non-space), delete it (d). That is print all the lines that
contain at least one non-space characters.

--
Stephane

foxidrive

unread,
Jun 9, 2012, 9:55:26 PM6/9/12
to
On Sunday 10/06/2012 05:32, Stephane Chazelas wrote:
> 2012-06-09 04:51:05 +0000, foxidrive:
>> On Friday 08/06/2012 05:18, Stephane Chazelas wrote:
>>> 2012-06-08 04:12:47 +1000, foxidrive:
>>> [...]
>>>> sed -e "/^ *$/d" -e "/^$/d" "file.txt" > "newfile.txt"
>>>
>>> sed '$q;/[^ ]/!d'
>>
>> I'm not sure that it has the same function, Stephane. I tried
>> to adapt it to GnuSED but didn't have success.
>
> That should work with any shell. The quoting is Bourne-like
> shell syntax as found on all Unix-like systems.
>
> What you want is the string "$q;/[^ ]/!d" to be passed as the
> one and only argument to sed.

Thank you for your explanation.

This functions without an error in GnuSED but it removes the last line which is a blank line.
In the last command line below (type b.txt) I would like the blank line to remain in between each output of the sed command line.


c:\Files\Lists>type a.txt
Book: "The Taming of the Shrew"

Author: William Shakespeare

The main plot depicts the courtship of Petruchio, a gentleman of Verona,
and Katherina, the headstrong, obdurate shrew. Initially, Katherina is
an unwilling participant in the relationship, but Petruchio tempers her
with various psychological tormentsùthe "taming"ùuntil she becomes a
compliant and obedient bride.

c:\Files\Lists>sed "$q;/[^ ]/!d" a.txt
Book: "The Taming of the Shrew"
Author: William Shakespeare
The main plot depicts the courtship of Petruchio, a gentleman of Verona,
and Katherina, the headstrong, obdurate shrew. Initially, Katherina is
an unwilling participant in the relationship, but Petruchio tempers her
with various psychological torments-the "taming"-until she becomes a
compliant and obedient bride.

c:\Files\Lists>sed "$q;/[^ ]/!d" a.txt >>b.txt

c:\Files\Lists>sed "$q;/[^ ]/!d" a.txt >>b.txt

c:\Files\Lists>type b.txt
Book: "The Taming of the Shrew"
Author: William Shakespeare
The main plot depicts the courtship of Petruchio, a gentleman of Verona,
and Katherina, the headstrong, obdurate shrew. Initially, Katherina is
an unwilling participant in the relationship, but Petruchio tempers her
with various psychological tormentsùthe "taming"ùuntil she becomes a
compliant and obedient bride.
Book: "The Taming of the Shrew"
Author: William Shakespeare
The main plot depicts the courtship of Petruchio, a gentleman of Verona,
and Katherina, the headstrong, obdurate shrew. Initially, Katherina is
an unwilling participant in the relationship, but Petruchio tempers her
with various psychological tormentsùthe "taming"ùuntil she becomes a
compliant and obedient bride.

c:\Files\Lists>

It seems to be difficult.


> It says that, on the last line ($), sed should quit (q) (after
> having displayed the line).
>
> For any other line, unless it matches /[^ ]/ (contains any
> non-space), delete it (d). That is print all the lines that
> contain at least one non-space characters.



--
Mic

foxidrive

unread,
Jun 9, 2012, 10:03:18 PM6/9/12
to
On Sunday 10/06/2012 11:55, foxidrive wrote:
> On Sunday 10/06/2012 05:32, Stephane Chazelas wrote:
>> 2012-06-09 04:51:05 +0000, foxidrive:
>>> On Friday 08/06/2012 05:18, Stephane Chazelas wrote:
>>>> 2012-06-08 04:12:47 +1000, foxidrive:
>>>> [...]
>>>>> sed -e "/^ *$/d" -e "/^$/d" "file.txt" > "newfile.txt"
>>>>
>>>> sed '$q;/[^ ]/!d'
>>>
>>> I'm not sure that it has the same function, Stephane. I tried
>>> to adapt it to GnuSED but didn't have success.
>>
>> That should work with any shell. The quoting is Bourne-like
>> shell syntax as found on all Unix-like systems.
>>
>> What you want is the string "$q;/[^ ]/!d" to be passed as the
>> one and only argument to sed.
>
> Thank you for your explanation.
>
> This functions without an error in GnuSED but it removes the last line which is a blank line.
> In the last command line below (type b.txt) I would like the blank line to remain in between each output of the sed command line.

My mistake, it works well. My a.txt file had a trailing crlf but not a blank line. I added a blank line and it works fine.

Thank you Stephane.

--
Mic
0 new messages