UNIX file with Newlines

Hi Friends,

I have a data file with new lines.

How to remove the newlines and should be showed in one line.

I tried using the command

tr -d '\n' filename

sed 's/\n//g' file name

Ex: 1 abc hyd is actual record
but in our scenario showing it as

1 abc
hyd

this record should be like as follows.

1 abc hyd

it's not working.

Could you please help me out to overcome the issue.

Thanks in Advance!

It's a bit difficult to believe that a one line record should be wrapped in two lines, automatically, out of the blue sky, UNLESS you have some formatting issues. We have too little info to judge. So - please enlighten us and tell us how and why your "scenario showing it as" a split line. You might describe the scenario, and the tool(s) used for it. A hex dump of the data records might help as well.

You have at least two "code" problems to overcome:

  1. if this happens to all records
  2. if this happens to only some records.

We need to know which is true before we go any further. If neither one is correct we need you to post
Sample input (8-10 lines of original file)
Sample expected output (same 8-10 lines that you fixed manually)

I suppose "file name" should read "filename", otherwise these would be two filenames without quoting, but otherwise both these should work as expected. If they don't (btw: telling us "does not work" is not enough, SHOW us HOW it doesn't work, i.e. by copying a terminal session) you may investigate one of these:

  • Windows files
    Maybe your line endings are not what they seem to be. If your files originate in Windows/DOS this is likely to be the case. Use od -ax file to verify.

  • output neglected?
    sed puts it output not into the file but to <stdout>. If you do not redirect it into a new file and move that over the old one your changes will be lost. i.e.:

sed 's/\n//g' file > file.new
mv file.new file

To think of other issues you will have to tell us a bit more about your environment: shell? OS-version?

I hope this helps.

bakunin

Hi,

Not all records are new lined in my file.

few of the records are formatted and few are missing the format and moving to another line.

ex:

1abcHYD
2cdeCHN
3ghi
BLR
4jklPUN

End up our scenario should be the below as mentioned

1abcHYD
2cdeCHN
3ghiBLR
4jklPUN

The above highlighted one which new lined, Not all the records are incorrect only the record which I have highlighted the one creating new line and align it as mentioned above.

Seems 3rd record is not in format So when we execute the command it should be showed in proper format of the file.

Thanks,
Venkat

--- Post updated at 09:47 AM ---

It is happening only for few records not for all records.

environment is Linux

OK, now, that you have provided an example it is a lot clearer what you want. To correct selectively some records you will need to specify how they could be selected: is there a certain line length that tells use which record is "complete" and which one is not? A number of fields? Or is there any other property to base this decision on?

I hope this helps.

bakunin