PDA

View Full Version : Make all text file content in one line if a space found



santa1234
09-12-2022, 05:24 AM
Make all text file content in one line if space found after a word, number, or special character


1. I have a text file with thousands of short lines. If space is found say a (tab)
is a space (tab)
here.

#Now I want to make it all in a single line together if a space (tab)
is found after the last word in every line.

The output will look like the below:

1. I have a text file with thousands of short lines. If space is found say a is a space here.

#Now I want to make it all in a single line together if a space is found after the last word in every line.

Ps. I also want to delete all extra spaces after this.

https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg (https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg)
https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgxLtKj969oiIu7zNb94AaABAg (https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgxLtKj969oiIu7zNb94AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg)
https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugx12mI-a39T41NaZ8F4AaABAg.9iDQqIP56NV9iFD0AkeeJG (https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugx12mI-a39T41NaZ8F4AaABAg.9iDQqIP56NV9iFD0AkeeJG)
https://www.youtube.com/watch?v=vXyMScSbhk4&lc=Ugxa2VYHMWJWXA6QI294AaABAg.9irLgSdeU3r9itU7zdnW Hw (https://www.youtube.com/watch?v=vXyMScSbhk4&lc=Ugxa2VYHMWJWXA6QI294AaABAg.9irLgSdeU3r9itU7zdnW Hw)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg.9htChVuaX9W9htG01cKBzX (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg.9htChVuaX9W9htG01cKBzX)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg.9ht16tzryC49htJ6TpIO XR (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg.9ht16tzryC49htJ6TpIO XR)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgwMKwGZpDjv7vi7pCx4AaABAg (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgwMKwGZpDjv7vi7pCx4AaABAg)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg.9ht16tzryC49htOKs4jh 3M (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg.9ht16tzryC49htOKs4jh 3M)
https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgxVW-am20rQ5GFuJ9F4AaABAg (https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgxVW-am20rQ5GFuJ9F4AaABAg)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)

DocAElstein
09-14-2022, 01:20 AM
Hello susan santa 12345

This is potentially quite easy if you know a couple of things about your text file
_ 1 the format on the character(s) used as the line separator
_ 2 how consistent that character(s) used as the line separator is.

To explain what I am talking about:
In computing generally, and certainly in text files, a computer knows when to make a new line in text when it sees 1 or 2 specific characters in the text string. These characters are known as
_ the carriage return , ( in VBA coding this is the constant vbCr )
and
_ the line feed, ( in VBA coding this is the constant vbLf )

Most commonly, both those 2 characters are used. But sometimes, just one of those characters is used.
We need to know which character(s) are used in your text file, and we need to know if that is constantly the case.

When we know this, we can replace all combination of the tab character and the new line character(s) with nothing. That effectively removes the space and the new line, which is what you want.

One way to do this in VBA coding would be as follows:
We would use VBA to import the text file into a single string variable,
then do that replacing , ( so something like replacing vbTab & vbCr & vbLf with nothing,
then remake the text file from the modified string.


To demonstrate this VBA way, we would need a sample text file. For the demonstration we only need a text file of a few lines. ( The first thing I would do with that text file is to determine what character(s) are used for the new line, for example using my function here: https://excelfox.com/forum/showthread.php/2302-quot-What%e2%80%99s-in-a-String-quot-VBA-break-down-Loop-through-character-contents-of-a-string )

( I expect that another way to do this would be using Power Query. I don’t know how to do that, but someone else may be able to demonstrate that after I have determined what character(s) are used for the new line )


So, in order to help you further we need a reduced size sample text file from you



Alan

santa1234
09-14-2022, 04:24 AM
Please find the attached sample file.



https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://eileenslounge.com/viewtopic.php?p=316441#p316441 (https://eileenslounge.com/viewtopic.php?p=316441#p316441)
https://eileenslounge.com/viewtopic.php?p=324736#p324736 (https://eileenslounge.com/viewtopic.php?p=324736#p324736)
https://eileenslounge.com/viewtopic.php?p=324990#p324990 (https://eileenslounge.com/viewtopic.php?p=324990#p324990)
https://eileenslounge.com/viewtopic.php?f=27&t=41937&p=325485#p325485 (https://eileenslounge.com/viewtopic.php?f=27&t=41937&p=325485#p325485)
https://eileenslounge.com/viewtopic.php?p=325609#p325609 (https://eileenslounge.com/viewtopic.php?p=325609#p325609)
https://eileenslounge.com/viewtopic.php?p=325610#p325610 (https://eileenslounge.com/viewtopic.php?p=325610#p325610)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGADdPM65i9 PG (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGADdPM65i9 PG)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGADdPQHFk_ zm (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGADdPQHFk_ zm)
http://www.eileenslounge.com/viewtopic.php?p=324457#p324457 (http://www.eileenslounge.com/viewtopic.php?p=324457#p324457)
http://www.eileenslounge.com/viewtopic.php?p=324064#p324064 (http://www.eileenslounge.com/viewtopic.php?p=324064#p324064)
http://www.eileenslounge.com/viewtopic.php?p=323960#p323960 (http://www.eileenslounge.com/viewtopic.php?p=323960#p323960)
https://www.youtube.com/watch?v=7VwD9KuyMk4&lc=UgyZCnNfnZRfgwzDlQF4AaABAg (https://www.youtube.com/watch?v=7VwD9KuyMk4&lc=UgyZCnNfnZRfgwzDlQF4AaABAg)
https://www.youtube.com/watch?v=7VwD9KuyMk4&lc=UgyZCnNfnZRfgwzDlQF4AaABAg.ADd4m2zp_xDADd6Nnotj 1C (https://www.youtube.com/watch?v=7VwD9KuyMk4&lc=UgyZCnNfnZRfgwzDlQF4AaABAg.ADd4m2zp_xDADd6Nnotj 1C)
s://www.youtube.com/watch?v=7VwD9KuyMk4&lc=UgySdtXqcaA27wQLd1t4AaABAg (s://www.youtube.com/watch?v=7VwD9KuyMk4&lc=UgySdtXqcaA27wQLd1t4AaABAg)
http://www.eileenslounge.com/viewtopic.php?p=323959#p323959 (http://www.eileenslounge.com/viewtopic.php?p=323959#p323959)
http://www.eileenslounge.com/viewtopic.php?f=30&t=41784 (http://www.eileenslounge.com/viewtopic.php?f=30&t=41784)
http://www.eileenslounge.com/viewtopic.php?p=323966#p323966 (http://www.eileenslounge.com/viewtopic.php?p=323966#p323966)
http://www.eileenslounge.com/viewtopic.php?p=323959#p323959 (http://www.eileenslounge.com/viewtopic.php?p=323959#p323959)
http://www.eileenslounge.com/viewtopic.php?p=323960#p323960 (http://www.eileenslounge.com/viewtopic.php?p=323960#p323960)
http://www.eileenslounge.com/viewtopic.php?p=323894#p323894 (http://www.eileenslounge.com/viewtopic.php?p=323894#p323894)
http://www.eileenslounge.com/viewtopic.php?p=323843#p323843 (http://www.eileenslounge.com/viewtopic.php?p=323843#p323843)
https://www.youtube.com/watch?v=fEHKPhJxgBA&lc=Ugxx8_MjhC9FDaQgcHN4AaABAg (https://www.youtube.com/watch?v=fEHKPhJxgBA&lc=Ugxx8_MjhC9FDaQgcHN4AaABAg)
https://www.youtube.com/watch?v=jpjYm4UvyWk&lc=Ugx_Qd4rfAN_ZYcJbo94AaABAg.ACGbG9c76OWACGbjKa7H 8k (https://www.youtube.com/watch?v=jpjYm4UvyWk&lc=Ugx_Qd4rfAN_ZYcJbo94AaABAg.ACGbG9c76OWACGbjKa7H 8k)
https://www.youtube.com/watch?v=jpjYm4UvyWk&lc=Ugx_Qd4rfAN_ZYcJbo94AaABAg (https://www.youtube.com/watch?v=jpjYm4UvyWk&lc=Ugx_Qd4rfAN_ZYcJbo94AaABAg)
https://www.youtube.com/watch?v=GyPHaydeng0&lc=UgzE4a4f_e_y9Rk5OR94AaABAg (https://www.youtube.com/watch?v=GyPHaydeng0&lc=UgzE4a4f_e_y9Rk5OR94AaABAg)
https://www.youtube.com/watch?v=I5FkNG94BcQ&lc=UgxXnkEHqulXSR5tXwh4AaABAg (https://www.youtube.com/watch?v=I5FkNG94BcQ&lc=UgxXnkEHqulXSR5tXwh4AaABAg)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa6BSa17 3Z (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa6BSa17 3Z)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa6-64Xpgl (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa6-64Xpgl)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa5ms39y jd (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa5ms39y jd)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa5ZXJwR CM (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa5ZXJwR CM)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa4Pr15N Ut (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa4Pr15N Ut)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa4I83Je lY (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGABa4I83Je lY)
https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGADdMo2n-hyF (https://www.youtube.com/watch?v=3t8Mk4URi6g&lc=UgzoakhRXOsCaoRm_Nd4AaABAg.8xzeMdC8IOGADdMo2n-hyF)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)

DocAElstein
09-14-2022, 02:41 PM
Hi

These are your sample files: https://i.postimg.cc/4xHH4FN2/Before-and-After-Reduce-text-Lines.jpg
3956

I won’t use those for this demonstration. I prefer to use these reduced size files
content in one line input reduced sample.txt https://app.box.com/s/grrxh1rl372pzp2exn6em00ovhj4qhih
content in one line output reduced sample.txt https://app.box.com/s/mpyvgf4kj9q04szjtj0255cns24lbxos

https://i.postimg.cc/yYdStNqH/Before-and-After-Reduce-text-Lines-reduced-sample.jpg
3957


It may be advisable for you to repeat the things that I am doing for you for more parts of the full size text file



So I have taken a look at your files, and done a macro for you ( Sub ReplaceInTextFileThreeCharacters__Space_vbCr_vbLf_ _WithA__Space__() )
See here for full details: https://excelfox.com/forum/showthread.php/2559-Notes-tests-text-files-manipulation-of-text-files-in-Excel-and-with-Excel-VBA?p=16696&viewfull=1#post16696

Here the summary:
_ It does not appear that you have any tab characters in your text file
_ It seems that your text file uses the typical line separator of the two character pair, a carriage return and a line feed, ( In VBA coding, vbCr & vbLf )
_ It seems that a single space precedes the new line characters that you wish to remove

So..
_ The macro that I have done for you, ( Sub ReplaceInTextFileThreeCharacters__Space_vbCr_vbLf_ _WithA__Space__() ) , replaces the three character combination of a space and a carriage return and a line feed, with a single space.
( In the given VBA coding I replace the three character combination of " " & vbCr & vbLf with a single space " " )
It appears to give you the results you want


Alan




P.S.
Please note , susan santa 12345 et al
_ You have a number of accounts here and at other forums. Some of your posts have been deleted and/ or your accounts banned.
_ I expect your posts have been deleted and/ or your account banned, because of two main reasons:
_1) You often post poorly explained questions
_2) Often when you get help you do not give any feedback, or you do not reply. This is very uncourteous and disrespectful to the people who voluntarily give their time to help you.
And we don't know whether or not we are wasting our time helping you because we have no reply or feedback from you.

_ This is not the first time that I have said all this, or similar, to you.

I appreciate that you may not understand too well the English language, and I am much more reluctant to ban people than most forum Moderators are. But if you do not improve your posting style , I will delete you and your accounts in the future.

It is your right on a free forum to post as you choose. But if you want to continue posting in the style that you have so far, then it is probably best for you to post at the larger forums such as mrexcel.com and excelforum.com. At excelfox.com we only have a small select number of answering members, and we do not like wasting our time on people who just want a quick service to get their work done for them.
At excelfox we are interested in helping with problems, promoting, discussing, and learning office applications, not just giving a free coding writing service.

Seriously no hard feelings intended, :) , I am just telling you it as it is.

Alan