Best way to remove last line-feed in text file

What is the best way to remove last line-feed in text file? (so that the last line of text is the last line, not a line-feed). The best I can come up with is: echo -n "$(cat file.txt)" > newfile.txt
(as echo -n will remove all trailing newline characters)

What is the best way to remove last line-feed in text file? (so that the last line of text is the last line, not a line-feed). The best I can come up with is: echo -n "$(cat file.txt)" > newfile.txt
(as echo -n will remove all trailing newline characters)
According to my experiments, you have removed all line terminators from the file, and replaced those between lines with a space.
That is to say, you have turned a multi-line file into one long line with no line terminator.
If that is what you want, and your files are not very big, then your echo statement might be all you need.
If you need to deal with larger files, you could try using the 'tr' command, and something like
tr '
' ' ' <file.txt >newfile.txt
The only problem with this is, it will most likely give you a trailing space, as the last newline is going to be converted to a space. If that is not acceptable, then something else will have to be arranged.
However, if you really want to maintain a multi-line file, but remove just the very last line terminator, that gets a bit more complicated. This might work for you:
perl -ne '
chomp;
print "
" if $n++ != 0;
print;
' file.txt >newfile.txt
You can use cat -e to see which lines have newlines, and you should see that the last line does not have a newline, but all the others still do.
I guess if you really did mean to remove all newline characters and replace them with a space, except for the last line, then a modification of the above perl script would do that:
perl -ne '
chomp;
print " " if $n++ != 0;
print;
' file.txt >newfile.txt
Am I even close to understanding what you are asking for?

Similar Messages

  • Removal of Line feed from EDI file in IDOC to EDI file scenario

    Hi Experts,
    I have one problem related to line feed in EDI file. Currently i am using seeburger mapping to do XML to EDI mapping. After mapping i am using File Adapter(with NFS) to create file in my PI server.
    The line looks like
    UNB+UNOA......
    UNH+1.......
    BGM.........
    but i want my file in without line feed
    UNBUNOA......UNH1......BGM
    Can this be possible without use of Shell script?
    Is anything i can do in Seeburger mapping [i checked BIC mapper in seeburger and see some parameters there]?
    Regards,
    Shradha

    Hi,
    I am explaining my scenario in detail:
    my Current EDI file looks like
    UNAf1f2............'
    UNBf1f2............'
    BGMf1f2............'
    UNHf1f2............'
    now i want my file to be in following format
    UNAf1f2............'UNBf1f2............'BGMf1f2............'UNHf1f2............'
    NOTE: to create my current file i am using File adapter with NFS and seeburger mapping.
    Can you please tell me a way to remove the line feed from the current file ?
    I already tried newline checkbox from seeburger and it is not working. and other parameter i can use from see burger mapping tool which will remove my line feed.
    Regards,
    Shradha

  • How to only read the last line in the text file by using BufferedReader ?

    Dear all,
    Hello, I am new to Java. Do anybody know how to read the last line (this is the last record) in the text file.The method I am now using is reading from the first line until I reach the last line in the text file. Thank you!!
    BufferedReader br = new BufferedReader(new FileReader("c:\\sdk1.4.1\\bin\\dbExport.txt"));
    DataInputStream in = new DataInputStream(new FileInputStream("c:\\sdk1.4.1\\bin\\dbExport.txt"));
    String input;
    String firstinput;
    String secondinput;
    int count=90;
    int year=1955;
    while ((input = br.readLine()) != null) {
    firstinput = input.substring(0, 10);
    secondinput = input.substring(10);
    String insertStore1 = ("INSERT INTO AUTHORS " +
    "VALUES ('" + count + "', '" + firstinput + "', '" + secondinput + "', 1955)");
    System.out.println(insertStore1);
    int result = stmt.executeUpdate(insertStore1);

    I suppose you could use a java.io.RandomAccessFile.

  • How to read the last line in a text file using text_io?? please help

    Dear all
    I made a procedure that append text into a text file on the operating system.
    the text file grows rapidly. It contains now about 200,000 line . I need to read the last line only . In other wprds i need to go direct to the last line to read some values. Is it possible??
    Please help

    Hello,
    If you know the number of the line you want to read, I can sugget you to use the MORE dos command or the TAIL unix command that redirect to a temporary text file
    Example to create a file that contains the 200010th last lines :
    (Client)Host( 'MORE the_file_name.txt +200010 > small_file.txt') ;So you have only to read the small file with the TEXT_IO functions.
    Francois

  • What's the easiest way to remove/append a line of a text file???

    The line to remove can be any line in the file - not only the first or last one.
    Is there any good tutorial for file operations?
    Thanks a million,
    Serano.

    To remove a line from a text file, the easiest way is to read it in one line at a time and write out all lines except the one you want to remove to a new version of the file. Likewise, to insert a line, you read the file one line at a time and write the lines to a new version, inserting the new line at its correct spot.

  • How to read last line from a .txt file?

    Hello
    I have a string: e.g. "my name is John"
    and i wanna verify if this string is equal with the last line from a text file
    for example, if in the txt file i have:
    asdasd
    sdgsdfgasd
    asdfgadfgadf
    sdgasdgsdf
    my name is john
    then it's OK.
    but if i have
    asdgsdfg
    dsfhsdfhsd
    sdgasdfg
    sdgsdg
    my name is Jdfgsdg
    this is not correct
    How should i do this?
    Thanks:)

    Read from the beginning an discard all lines (if you have a small file), or use RandomAccessFile and scan from the end till you find the beginning of the last line.
    Kaj

  • Delete specified line in a text file

    can anyone help me??
    how to remove specific line in a text file using java?? thank thanks

    Read the file, write a new one, and when you write skip over the line you want to delete.

  • FCC in receiver channel - get rid of last line feed?

    Dear experts,
    I have the following XML structure
    <DT_SALES_REPORT_JDE_CA>
        <list>
            <RecordsetA>
             <a>
             <b>
         </RecordsetA>
         <RecordsetB>
             <a>
             <b>
             <c>
         </RecordsetB>
             <a>
             <b>
             <c>
         <RecordsetB>
         </RecordsetB>
        </list>
    </DT_SALES_REPORT_JDE_CA>
    and I am using CC in the receiver file adapter:
    RecordsetA.fieldSeparator     ","
    RecordsetB.fieldSeparator     ","
    RecordsetA.endSeparator          "\n
    RecordsetB.endSeparator          "\n
    RecordsetA.beginSeparator     "
    RecordsetB.beginSeparator     "
    which gives me this structure:
    "a","b"
    "a","b","c"
    "a","b","c"
    This is fine the only issue I have is that at the very end it also gives me a line feed of course.
    But I have to get rid of that last line feed!
    But how can I achieve that? Is that possible at all?
    I cannot use ".fixedLength" because the fields have variable lenghts.
    Thank you for any ideas and best regards,
    Peter

    Hello Zameer Hamza,
    You are right. I removed the .endSeperator and line feeds are entered automatically. But now I still have one line feed at the very end which I do not need.
    Would you have any other ideas how to resolve this?
    thank you again,
    Peter

  • What's the best way to remove inactive iChat users from jabberd2.db?

    I'm about to run Autobuddy for users on my iChat server. However, there are several users that are no longer around and I don't want their records showing up in everyone's buddy list.
    What's the safest/best way to remove them?
    My plan is to use sqlite3 on the command line and use SQL to remove the entries from the "active" table, but I don't know what impact that may have on the rest of the database.
    Any thoughts or suggestions?

    Never mind...
    Thought I had looked through enough threads.  Found the following just after posting my question:
    /usr/bin/jabber_autobuddy -d [email protected]
    Works like a charm.

  • How is the best way to remove something from a photo?

    How is the best way to remove something from a photo?

    This is difficult to answer without fully knowing what you are trying to do.
    That said, a few excellent and user friendly retouching tools include:  The Spot Heealing Brush Tool, Healing Brush Tool, Patch Tool, and the Cloning Stamp Tool.

  • Best way to remove duplicates based on multiple tables

    Hi,
    I have a mechanism which loads flat files into multiple tables (can be up to 6 different tables) using external tables.
    Whenever a new file arrives, I need to insert duplicate rows to a side table, but the duplicate rows are to be searched in all 6 tables according to a given set of columns which exist in all of them.
    In the SQL Server Version of the same mechanism (which i'm migrating to Oracle) it uses an additional "UNIQUE" table with only 2 columns(Checksum1, Checksum2) which hold the checksum values of 2 different sets of columns per inserted record. when a new file arrives it computes these 2 checksums for every record and look it up in the unique table to avoid searching all the different tables.
    We know that working with checksums is not bulletproof but with those sets of fields it seems to work.
    My questions are:
    should I use the same checksums mechanism? if so, should I use the owa_opt_lock.checksum function to calculate the checksums?
    Or should I look for duplicates in all tables one after the other (indexing some of the columns we check for duplicates with)?
    Note:
    These tables are partitioned with day partitions and can be very large.
    Any advice would be welcome.
    Thanks.

    >
    I need to keep duplicate rows in a side table and not load them into table1...table6
    >
    Does that mean that you don't want ANY row if it has a duplicate on your 6 columns?
    Let's say I have six records that have identical values for your 6 columns. One record meets the condition for table1, one for table2 and so on.
    Do you want to keep one of these records and put the other 5 in the side table? If so, which one should be kept?
    Or do you want all 6 records put in the side table?
    You could delete the duplicates from the temp table as the first step. Or better
    1. add a new column WHICH_TABLE NUMBER to the temp table
    2. update the new column to -1 for records that are dups.
    3. update the new column (might be done with one query) to set the table number based on the conditions for each table
    4. INSERT INTO TABLE1 SELECT * FROM TEMP_TABLE WHERE WHICH_TABLE = 1
    INSERT INTO TABLE6 SELECT * FROM TEMP_TABLE WHERE WHICH_TABLE = 6
    When you are done the WHICH_TABLE will be flagged with
    1. NULL if a record was not a DUP but was not inserted into any of your tables - possible error record to examine
    2. -1 if a record was a DUP
    3. 1 - if the record went to table 1 (2 for table 2 and so on)
    This 'flag and then select' approach is more performant than deleting records after each select. Especially if the flagging can be done in one pass (full table scan).
    See this other thread (or many, many others on the net) from today for how to find and remove duplicates
    Best way of removing duplicates

  • I got some hair spray on my new retina display screen. What is the best way to remove.

    I got some hair spray on my new Mac Book Pro Retina Display. Any thoughts on the best way to remove?

    I would use this.  I use it on my MBPs and it does an excellent job.  I cannot say with authority that it will remove your hair spay residue.
    Ciao.
    http://www.soap.com/p/windex-for-electronics-aerosol-97299?site=CA&utm_source=Go ogle&utm_medium=cpc_S&utm_term=ASJ-294&utm_campaign=GoogleAW&CAWELAID=1323111033 &utm_content=pla&adtype=pla&cagpspn=pla&noappbanner=true
    I clicked the reply button too early.
    Message was edited by: OGELTHORPE

  • Best way to remove Stateful session beans

    Hi folks.
    I'm running Weblogic 6.1. I'm trying to find the best way of removing
    stateful session beans. I know to call EJBObject.remove() on the
    client side, but this will not always happen if the client crashes or
    times out. This is a java client application connection to weblogic,
    no servlets are involved.
    Is there a way to signal the appserver to remove all stateful session
    beans associated with a user when the User logs out? I would rather
    not remove them using a time out mechanism.
    thanks.
    rob.

    But in the documentation and also based on my experience I noticed that the
    timeout does not take effect till the max-beans-in-cache limit is reached.
    How do you handle that?
    "Thomas Christen" <[email protected]> wrote in message
    news:3e35795d$[email protected]..
    Hi,
    Is there a way to signal the appserver to remove all stateful session
    beans associated with a user when the User logs out? I would rather
    not remove them using a time out mechanism.Had the same problem and solved it the following way :
    - The client has thread polling its sessionbean at the server (every 30
    Sec.)
    - The session bean has a short timeout (2 Minutes)
    If the client fails, the timeout will catch it otherwise the client will
    gracefully call remove bevor exit.
    Regards
    Tomy

  • Best way to remove workstation?

    Hi! when we move a computer to another room, we rename the computer, then do a WSREG -UNREG and WSREG, the new workstation object is created, but the old workstation object stay there.
    If I delete the old object, the information stays on the sybase db!
    So what is the best way to remove a workstation object?
    I have a remove policy that work, but if I dont want to wait?
    thank you,
    Eric.

    eric,
    It appears that in the past few days you have not received a response to your
    posting. That concerns us, and has triggered this automated reply.
    Has your problem been resolved? If not, you might try one of the following options:
    - Visit http://support.novell.com and search the knowledgebase and/or check all
    the other self support options and support programs available.
    - You could also try posting your message again. Make sure it is posted in the
    correct newsgroup. (http://forums.novell.com)
    Be sure to read the forum FAQ about what to expect in the way of responses:
    http://support.novell.com/forums/faq_general.html
    If this is a reply to a duplicate posting, please ignore and accept our apologies
    and rest assured we will issue a stern reprimand to our posting bot.
    Good luck!
    Your Novell Product Support Forums Team
    http://support.novell.com/forums/

  • Best way to remove osx.1,5

    I never use osx on my G3 iMac and would love to remove it so as just to run on 9.2.2 .. Not sure of the best way to remove osx and all the associated applications.
    Any help would be appreciated.
    Davit

    If you can still boot to OS X, use the freeware AppDelete @ http://reggie.ashworth.googlepages.com/appdelete to delete your applications & associated files.
    Once you have deleted the OS X applications & files, reboot into OS 9.2. Then drag the OS X system folder to the Trash & empty trash. Some of the OS X invisible files may still remain on the HD, but they won't hinder the OS 9 operation & shouldn't take up too much space on your HD.
    What version of OS X do you have installed? Most users want to delete OS 9, not OS X. Most OS 9 browsers & mail programs are (almost?) obsolete for today's web. In the near future your OS 9 browser may not work on many websites & you'll want an operational browser which requires OS X. Unless you need more space on your hard drive, I would leave OS X installed.
     Cheers, Tom

Maybe you are looking for