UNIX file with Newlines


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
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

Code:
tr -d '\n' filename

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

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

this record should be like as follows.

Code:
1 abc hyd

it's not working.

Could you please help me out to overcome the issue.

Thanks in Advance!




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 2 Weeks Ago at 09:32 AM.. Reason: AddedCODE tags.
# 2  
Old 2 Weeks Ago
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.
# 3  
Old 2 Weeks Ago
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)
# 4  
Old 2 Weeks Ago
Quote:
Originally Posted by victory
I tried using the command

Code:
tr -d '\n' filename

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

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.:

Code:
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
# 5  
Old 2 Weeks Ago
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:

Code:
1abcHYD
2cdeCHN
3ghi
BLR
4jklPUN


End up our scenario should be the below as mentioned

Code:
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

Last edited by RudiC; 2 Weeks Ago at 10:07 AM..
# 6  
Old 2 Weeks Ago
Quote:
Originally Posted by victory
It is happening only for few records not for all records.
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
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
File formatting with newlines J1nx007 Shell Programming and Scripting 5 04-22-2017 12:14 PM
Remove newlines and carriage return from a csv file using UNIX A_Gaddale UNIX for Beginners Questions & Answers 3 04-22-2016 06:10 AM
Remove newlines Suneelbabu.etl UNIX for Dummies Questions & Answers 33 03-02-2014 01:51 PM
Replace commas with newlines brianjb Shell Programming and Scripting 2 05-18-2012 04:10 PM
Need help with eliminating newlines with Perl brianjb Shell Programming and Scripting 2 02-07-2012 09:38 AM
removing newlines after a certain word. DaytonCPS Shell Programming and Scripting 3 11-17-2011 08:43 PM
Delete newlines after every one space shoaibjameel123 Shell Programming and Scripting 4 10-12-2011 07:03 AM
'for LINE in $(cat file)' breaking at spaces, not just newlines natedawg1013 Shell Programming and Scripting 10 08-19-2011 09:01 AM
finding string in very long file without newlines aaronpoley UNIX for Dummies Questions & Answers 5 05-25-2010 06:14 PM
Ignoring newlines in my search ndedhia1 Shell Programming and Scripting 7 04-08-2010 12:37 PM
Padding lines in a file with newlines npatwardhan Shell Programming and Scripting 5 02-05-2010 09:43 AM
Transpose with two newlines as delimiter npatwardhan Shell Programming and Scripting 10 01-29-2010 11:42 AM
Removing inserted newlines from a fileld of fixed width file. enigma_1 Shell Programming and Scripting 8 08-19-2009 06:18 PM
Add CRs (newlines) Gale Gorman OS X (Apple) 10 03-27-2009 03:30 PM
Newlines in shell variables narcvs Shell Programming and Scripting 5 03-26-2009 02:54 AM