Help with removing line breaks and leading whitespace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shafferzee
Posts: 2
Joined: 09 Jun 2018 17:58

Help with removing line breaks and leading whitespace

#1 Post by shafferzee » 09 Jun 2018 18:16

Hi guys -

I'm searching for a way to remove leading whitespaces and line breaks from my text.

I was successfully able to remove line breaks by using the following...

Code: Select all

call jrepl "[\r\n]" "" /m /f "input.txt" /o "output.txt"
However, I'm a bit stuck on removing whitespace and am need of some help.
Any assistance is greatly appreciated, thanks! :D

Here is an example of my text.

Code: Select all

                        type: client;
                        name: luf4;
                      server: luf4;
                   client id: \
00000004;
            scheduled backup: Enabled;
                     comment: ;
             Save operations: ;
            archive services: Disabled;
                    schedule: Always;
                  statistics: elapsed = 2279805, index size (KB) = 35437,
                              amount used (KB) = 35437, entries = 224622;
Current output after removing line breaks.

Code: Select all

                        type: client;                        name: luf4;                      server: luf4;                   client id: \00000004;            scheduled backup: Enabled;                     comment: ;             Save operations: ;            archive services: Disabled;                    schedule: Always;                  statistics: elapsed = 2279805, index size (KB) = 35437,                              amount used (KB) = 35437, entries = 224622;
Desired output.

Code: Select all

type: client;name: luf4;server: luf4;client id:00000004;scheduled backup: Enabled;comment: ;Save operations: ;archive services: Disabled;schedule: Always;statistics: elapsed = 2279805, index size (KB) = 35437,amount used (KB) = 35437, entries = 224622;

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help with removing line breaks and leading whitespace

#2 Post by Squashman » 09 Jun 2018 19:18

Using JREPL for this task is a little overkill. You could use a single `FOR /F` command to output all the lines onto one line.

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: Help with removing line breaks and leading whitespace

#3 Post by sst » 09 Jun 2018 21:24

As Squashman has pointed out, your specific task can be handled by a single line of FOR /F
Your question is more about of regex usage rather than batch scripting, but to cover your curiosity here is the JREPL way

Code: Select all

call jrepl "[\r\n]|^ *| *$" "" /m /f "input.txt" /o "output.txt"
Although you didn't ask for, trailing spaces are covered too. Be aware that the above only removes leading and trailing <SPACE> characters (ASCII code 32) not every other white spaces like TAB,...

shafferzee
Posts: 2
Joined: 09 Jun 2018 17:58

Re: Help with removing line breaks and leading whitespace

#4 Post by shafferzee » 10 Jun 2018 03:05

Squashman wrote:
09 Jun 2018 19:18
Using JREPL for this task is a little overkill. You could use a single `FOR /F` command to output all the lines onto one line.
I plan on sticking with JREPL due to the need of making more complex text transformations to this particular text block in the future. I feel like this is a good tool to learn to use.
Thanks for the help from both Squashman & sst. :P

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help with removing line breaks and leading whitespace

#5 Post by Squashman » 10 Jun 2018 13:44

That is fine but learning the basics usually helps set a foundation for learning more complex tasks.

Post Reply