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!

Trailing (only) cr/lf Removal

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
Scripting on an AIX box with Korn shell, I need to be able to truncate just the trailing cr/lf pair on the very last record of a multi-hundred record file. I've looked and don't even see the characters but file recipient (it's ftp-d from me to him) insists that the blank line is causing problems with their pickup/processing routine.

Assuming there is a cr/lf there (all of the other records have one) how do I remove just the one on the last record?

Tnx.

Tom

"My mind is like a steel whatchamacallit ...
 
Something like this ?
sed '$d' /path/to/input > output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Not sure exactly what this is doing - can you elaborate a bit? I don't use sed all that often and, every time I do, I have to dig back into the books.

Tnx.

"My mind is like a steel whatchamacallit ...
 
Hi

Note that PHV's code will remove the last line, whatever it contains. To remove the last line only if is empty :
Code:
sed '${/^$/d}' /path/to/input > output
Tested with GNU [tt]sed[/tt]. I am sure it does not work with AIX's [tt]sed[/tt], but use it as an idea.

Feherke.
 
Feherke:

Thanks, I'll give that a shot. PHV's example seemed to work but the recipient - who is writing the parsing program there from scratch, apparently - said it's still causing problems on his end. Since the file is Unix standard format - lf at end of each record, cr/lf at end of the file - I'm not sure I can get rid of just that last cr. He did ask that I change the file so that all records end in a cr/lf pair - that he can apparently program for - but I'm trying to get this done under a deadline and don't want to go back through all the testing steps again.

Appreciate the assist, both of you.

Tom

"My mind is like a steel whatchamacallit ...
 
it's still causing problems on his end
Which problem ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Tom,

Show us the bytes to see what's actually there. Why not give a hexdump/tail of your file, e.g:

$ od -x /path/to/file | tail -n 4

Cheers,
ND [smile]
 
IMHO this is something for the reception side to take care of (or to live with). On FTP'ing a textfile from unix to windows, end of line markers from unix (LF) are translated to windows (CR+LF).

Even if you would remove the last LF of a textfile and then FTP it to windows, the last line still gets a CR+LF added again. E.g. windows editor shows this as an empty line after the last text line. Normal behaviour if you ask me.


HTH,

p5wizard
 
Although not an exact answer for your specific problem, I'm having to handle similar issues with the files input to me.

So thought I'd just mention the things I am dealing with at the moment in case it gives you any further ideas:

I'm writing a Unix korn shell script to analyse text files. So I make use of sed/grep/nawk. They all expect the file to be in Unix format - ie the end of the lines need to end with \n (or "lf" or "\012" however you want to put it).

I receive files with \r\n or sometimes \r\f (carriage return, form feed) which messes things up for me.

So now as I read in the files, I just do this:
cat $FILE_NAME | tr '\015' '\012' | <etc etc>
which would double space my file if read by a human, but fixes my problems - "tr '\015' '\012'" translates all carriage-returns to line-feeds.
Sometimes I'll do "tr -d '\015'" which means "delete all carriage-returns"

I found od -ta to be a useful od command when analysing my file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top