Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.

UNIX file with Newlines

Login or Register to Reply

Thread Tools Search this Thread
# 1  
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

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!

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

Last edited by RudiC; 02-28-2019 at 10:32 AM.. Reason: AddedCODE tags.
# 2  
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  
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  
Originally Posted by victory
I tried using the command

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

sed 's/\n//g' file >
mv 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.

# 5  

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.



End up our scenario should be the below as mentioned


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.


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

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

environment is Linux

Last edited by RudiC; 03-01-2019 at 11:07 AM..
# 6  
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.

Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File formatting with newlines

Hi All - I am in need of some help in formating the below file Requirement - 1) replace newlines with space 2) replace '#~# ' with newline ----------------------- sample inputfile a I|abc|abc|aaa#~# I|sddddd|tya|dfg sfd ssss#~# I|tya1|tya2|dfg|sfd|aaa#~#... (5 Replies)
Discussion started by: J1nx007
5 Replies

2. UNIX for Beginners Questions & Answers

Remove newlines and carriage return from a csv file using UNIX

I need to remove new lines and carriage returns from csv file. Is there anything other than sed and gwak by which we could achieve this ? Any suggestions ? (3 Replies)
Discussion started by: A_Gaddale
3 Replies

3. UNIX for Dummies Questions & Answers

Remove newlines

Hi buddy's my file are like this:,name,band,sal 1,"suneel",,10 2,"bargav sand",,20 30," ebdug gil",,4 but i want,name,band,sal 1,"suneel",,10 2,"bargav sand",,20 30,"ebdug gil",,4 any command or Shell script for this. please help me it's urgent to implement (33 Replies)
Discussion started by: Suneelbabu.etl
33 Replies

4. Shell Programming and Scripting

Replace commas with newlines

Good afternoon, I am trying to read user input. Here is what I have so far: echo "Type the Container ID for every container that you want subnets exported" echo "for (with comma between each one, for example... 1,45,98)" echo -n "if you want every one listed, then just type ALL in caps... (2 Replies)
Discussion started by: brianjb
2 Replies

5. Shell Programming and Scripting

'for LINE in $(cat file)' breaking at spaces, not just newlines

Hello. I'm making a (hopefully) simple shell script xml parser that outputs a file I can grep for information. I am writing it because I have yet to find a command line utility that can do this. If you know of one, please just stop now and tell me about it. Even better would be one I can input... (10 Replies)
Discussion started by: natedawg1013
10 Replies

6. UNIX for Dummies Questions & Answers

finding string in very long file without newlines

What's the best way to find a string in a very long file without newlines in Unix? The standard utility I'm aware of for finding a string in a single file is grep, but for a long file without newlines, I think the output is just going to be the input. I suppose I could use sed to replace the... (5 Replies)
Discussion started by: aaronpoley
5 Replies

7. Shell Programming and Scripting

Ignoring newlines in my search

I have a file that has lines that are deliminated with '^A', but some of the lines go for a few lines and I need those lines to be appended into one line. All of the lines start with 'low debug' and end with ' " 0 '. How can I read each line from start to finish without some of the data... (7 Replies)
Discussion started by: ndedhia1
7 Replies

8. Shell Programming and Scripting

Remove improperly placed newlines

Hello, there. I have a file that's a horrible, horrible mess. (Basically, it's an export from a firewall config.) The people who generated the file didn't think that putting a newline in the middle of a hostname would ever be a problem. It is. Here's an example of the stuff in the file: ... (2 Replies)
Discussion started by: mikesimone
2 Replies

9. Shell Programming and Scripting

Padding lines in a file with newlines

Hi Guys, I have a file as shown below: 55 6 77 8 88 98 7 85 45 544 1 33 32 13 13 (5 Replies)
Discussion started by: npatwardhan
5 Replies

10. Shell Programming and Scripting

Removing inserted newlines from a fileld of fixed width file.

Hi champs! I have a fixed width file in which the records appear like this 11111 <fixed spaces such as 6> description for 11111 <fixed spaces such as 6> some more field to the record of 11111 22222 <fixed spaces such as 6> description for 22222 <fixed spaces such as 6> some more field to the... (8 Replies)
Discussion started by: enigma_1
8 Replies

Featured Tech Videos