M
M
Michael Vasyukov2017-11-24 22:53:29
Text Processing Automation
Michael Vasyukov, 2017-11-24 22:53:29

How to quickly format a file?

I have a txt file like this:

spoiler

----Slovo1-------slovo2------12Slovo----------34452gSlovo
----Slovo345-----Slovo234--slovo234----- --Slovo6643
and more than 1000 such lines
' - ' is a space

You need to format it so that all the first spaces (before the characters) are removed, and the rest become |. It should turn out something like:
file.txt

Slovo1|slovo2|12Slovo|34452gSlovo
Slovo345|Slovo234|slovo234|Slovo6643
and more than 1000 such lines

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2017-11-24
@programmer_developer

In any text editor that supports regular expression search/replace
Replace ^\s+ with nothing
Replace \s+ with |

E
ElijahTr, 2017-11-24
@ElijahTr

For example like this:

let text = `
    Slovo1      slovo2        12Slovo        34452gSlovo
    Slovo345     Slovo234  slovo234      Slovo6643
`;

console.log(text);
text = text.replace(/\s\s+/g, '|');
console.log(text);

https://codepen.io/elijah_tr/pen/LOObMg
Or take a regular expression from there and apply it in other ways :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question