spliting a single line into multiple lines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bars143
Posts: 87
Joined: 01 Sep 2013 20:47

spliting a single line into multiple lines

#1 Post by bars143 » 28 Apr 2018 01:17

hello to everyone ! its been years and forgot everything i learned there before.

but now i learn a basic android for theming status bar

and i want and stable scripts to make xml editing easy.

here is my sample intial content of status_bar.xml content

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.statusbar.phone.PhoneStatusBarView android:orientation="vertical" android:id="@id/status_bar" android:background="@drawable/system_bar_background" android:focusable="true" android:descendantFocusability="afterDescendants"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">
    <ImageView android:id="@id/notification_lights_out" android:paddingBottom="2.0dip" android:visibility="gone" android:layout_width="@dimen/status_bar_icon_size" android:layout_height="fill_parent" android:src="@drawable/ic_sysbar_lights_out_dot_small" android:scaleType="center" android:paddingStart="6.0dip" />

above content had 5 lines with many special characters.
and i want every line that has a word "android:..." (which preceded with one space ) to split into new line

example result of above will be the ff:

Code: Select all

	
<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.statusbar.phone.PhoneStatusBarView
android:orientation="vertical" 
android:id="@id/status_bar"
android:background="@drawable/system_bar_background"
android:focusable="true"
android:descendantFocusability="afterDescendants"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">
    <ImageView android:id="@id/notification_lights_out"
	android:paddingBottom="2.0dip"
	android:visibility="gone"
	android:layout_width="@dimen/status_bar_icon_size"
	android:layout_height="fill_parent"
	android:src="@drawable/ic_sysbar_lights_out_dot_small"
	android:scaleType="center"
	android:paddingStart="6.0dip" />

its now from 5 lines into 17 lines.
so needs scripts to search for "android:" that preceded only with <one space char>
after spliting a preceding space will be remove and repace with a tab (single space only)

and here is the script i use from google:

Code: Select all

	 @echo off

>newfile.txt (
  for /f "delims=" %%A in (files.txt) do for %%B in (%%A) do echo %%B
)

PAUSE
EXIT
	 

result above is

Code: Select all

	 version
"1.0"
encoding
<com.android.systemui.statusbar.phone.PhoneStatusBarView
android:orientation
"vertical"
android:id
"@id/status_bar"
android:background
"@drawable/system_bar_background"
android:focusable
"true"
android:descendantFocusability
"afterDescendants"
xmlns:android
"http://schemas.android.com/apk/res/android"
xmlns:systemui
"http://schemas.android.com/apk/res/com.android.systemui">
<ImageView
android:id
"@id/notification_lights_out"
android:paddingBottom
"2.0dip"
android:visibility
"gone"
android:layout_width
"@dimen/status_bar_icon_size"
android:layout_height
"fill_parent"
android:src
"@drawable/ic_sysbar_lights_out_dot_small"
android:scaleType
"center"
android:paddingStart
"6.0dip"
/>

	 


other script i use is:

Code: Select all

	 jrepl "\q.*?\q" $0 /x /jmatch /f files.txt /o newfile2.txt
	 

the result from using jreply is:

Code: Select all

	 "1.0"
"utf-8"
"vertical"
"@id/status_bar"
"@drawable/system_bar_background"
"true"
"afterDescendants"
"http://schemas.android.com/apk/res/android"
"http://schemas.android.com/apk/res/com.android.systemui"
"@id/notification_lights_out"
"2.0dip"
"gone"
"@dimen/status_bar_icon_size"
"fill_parent"
"@drawable/ic_sysbar_lights_out_dot_small"
"center"
"6.0dip"

	 




i had hard time recalling of what i learned before to apply above problem but seem returned back as a newbie again :?

hoping for some to help my scripts. :(

bars

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

Re: spliting a single line into multiple lines

#2 Post by Squashman » 28 Apr 2018 11:01

Why are you not using JREPL to match on a space and the word Android?

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: spliting a single line into multiple lines

#3 Post by bars143 » 28 Apr 2018 13:24

i should start and relearn of what i done before and i go back where i start.thanks for suggestion

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: spliting a single line into multiple lines

#4 Post by bars143 » 28 Apr 2018 20:09

im come back and here is my text2.txt file content:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.statusbar.phone.PhoneStatusBarView android:orientation="vertical" android:id="@id/status_bar" android:background="@drawable/system_bar_background" android:focusable="true" android:descendantFocusability="afterDescendants"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">
    <ImageView android:id="@id/notification_lights_out" android:paddingBottom="2.0dip" android:visibility="gone" android:layout_width="@dimen/status_bar_icon_size" android:layout_height="fill_parent" android:src="@drawable/ic_sysbar_lights_out_dot_small" android:scaleType="center" android:paddingStart="6.0dip" />
	
tried this script with "=?\r?\n" as a newline equivalent( found in first page of jreply where i used post it before):

Code: Select all

type test2.txt | jrepl "=?\r?\n" "endofline" /m >output.txt
and the result is good , it replace endofline char? with string "endofline" but not creating newline:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>endofline<com.android.systemui.statusbar.phone.PhoneStatusBarView android:orientation="vertical" android:id="@id/status_bar" android:background="@drawable/system_bar_background" android:focusable="true" android:descendantFocusability="afterDescendants"endofline  xmlns:android="http://schemas.android.com/apk/res/android"endofline  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">endofline    <ImageView android:id="@id/notification_lights_out" android:paddingBottom="2.0dip" android:visibility="gone" android:layout_width="@dimen/status_bar_icon_size" android:layout_height="fill_parent" android:src="@drawable/ic_sysbar_lights_out_dot_small" android:scaleType="center" android:paddingStart="6.0dip" />endofline	
as foundout above that newline with equal char "=?\r?\n" as a code for newline, then create scripts like this one:

Code: Select all

type test2.txt | jrepl "android:" "=?\r?\n" /m >output.txt
and the result produced literal rather than newline char???:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.statusbar.phone.PhoneStatusBarView =?\r?\norientation="vertical" =?\r?\nid="@id/status_bar" =?\r?\nbackground="@drawable/system_bar_background" =?\r?\nfocusable="true" =?\r?\ndescendantFocusability="afterDescendants"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">
    <ImageView =?\r?\nid="@id/notification_lights_out" =?\r?\npaddingBottom="2.0dip" =?\r?\nvisibility="gone" =?\r?\nlayout_width="@dimen/status_bar_icon_size" =?\r?\nlayout_height="fill_parent" =?\r?\nsrc="@drawable/ic_sysbar_lights_out_dot_small" =?\r?\nscaleType="center" =?\r?\npaddingStart="6.0dip" />



therafter, I tried adding /jq and /jmatchq then both results as shown below with my scripts are also shown produce error:

Code: Select all

C:\Users\leochard\Downloads\NEWEST_FILES\JREPL7.11>type test2.txt   | jrepl "a
roid:" "=?\r?\n" /jq /m  1>output.txt
JScript runtime error while defining /J or /JMATCH code: Syntax error

C:\Users\leochard\Downloads\NEWEST_FILES\JREPL7.11>type test2.txt   | jrepl "and
roid:" "=?\r?\n" /jmatchq /m  1>output.txt
JScript runtime error while defining /J or /JMATCH code: Syntax error
thats all i had
i will try harder next time...

bars

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: spliting a single line into multiple lines [solved]

#5 Post by bars143 » 29 Apr 2018 14:39

found out and solved:

Code: Select all

del output.txt 
type test.txt | jrepl "=?\r?\n" "endofline" /m >output.txt
rename output.txt test2.txt
pause
type test2.txt | jrepl " android:" "endofline" /m >output.txt
rename output.txt test3.txt
pause
type test3.txt | jrepl "endofline" "\r\n" /x /m >output.txt
pause
:D

to my own effort
bars

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: spliting a single line into multiple lines (solved)

#6 Post by bars143 » 29 Apr 2018 14:48

SOLVED.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: spliting a single line into multiple lines (solved)

#7 Post by thefeduke » 30 Jun 2018 17:05

bars143 wrote:
29 Apr 2018 14:48
SOLVED.
bars, thank you very much for sharing your progress and thought process. I examined all of your posts with great interest, because I was wrestling with the handling of spaces and tabs. It was all you with only one suggestion. Even though your requirement was solved, I am posting an added filter to your very first experiment. It yields this output:

Code: Select all

orientation=vertical
id=@id/status_bar
background=@drawable/system_bar_background
focusable=true
descendantFocusability=afterDescendants
id=@id/notification_lights_out
paddingBottom=2.0dip
visibility=gone
layout_width=@dimen/status_bar_icon_size
layout_height=fill_parent
src=@drawable/ic_sysbar_lights_out_dot_small
scaleType=center
paddingStart=6.0dip
from this script:

Code: Select all

    @echo off
    SetLOCAL EnableDelayedExpansion
Rem spliting a single line into multiple lines
Rem Start https://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5042
Rem #1 Post by bars143 » Sat Apr 28, 2018 3:17 am

Echo=This is set up for an input file named: %~n0.txt
If not Exist %~n0.txt (Echo.Please create one and try again.& Exit /B)

Rem Breaks each whole record into one record per standard delimeters with filter
>%~n01.txt (
  for /f "delims=" %%A in (%~n0.txt) do for %%B in (%%A) do (
    Set roid=%%B
    If /I "!roid:~0,8!" EQU "android:" (
      Set And=!roid:~8!=
    ) Else (
      Set roid=!roid:"=!
      If Defined And (
        Echo=!And!!roid!
        Set "And="
      )
    )
  )
)

EXIT /B
Somewhat simpler than three JREPLs.

John A.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: spliting a single line into multiple lines

#8 Post by Aacini » 30 Jun 2018 21:24

Mmmmm... Here it is an even simpler solution:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (status_bar.xml) do (
   set "line=%%a"
   for /F "delims=" %%b in (^"!line: android:^=^
% Do NOT remove this line %
android:!^") do (
      echo %%b
   )
)
This program replaces every "<space>android:" by "<cr><lf>android:" in a single step...

Antonio

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: spliting a single line into multiple lines

#9 Post by dbenham » 03 Jul 2018 09:39

bars143 wrote:
28 Apr 2018 01:17
here is my sample intial content of status_bar.xml content

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.statusbar.phone.PhoneStatusBarView android:orientation="vertical" android:id="@id/status_bar" android:background="@drawable/system_bar_background" android:focusable="true" android:descendantFocusability="afterDescendants"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">
    <ImageView android:id="@id/notification_lights_out" android:paddingBottom="2.0dip" android:visibility="gone" android:layout_width="@dimen/status_bar_icon_size" android:layout_height="fill_parent" android:src="@drawable/ic_sysbar_lights_out_dot_small" android:scaleType="center" android:paddingStart="6.0dip" />
above content had 5 lines with many special characters.
and i want every line that has a word "android:..." (which preceded with one space ) to split into new line

example result of above will be the ff:

Code: Select all

	
<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.statusbar.phone.PhoneStatusBarView
android:orientation="vertical" 
android:id="@id/status_bar"
android:background="@drawable/system_bar_background"
android:focusable="true"
android:descendantFocusability="afterDescendants"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">
    <ImageView android:id="@id/notification_lights_out"
	android:paddingBottom="2.0dip"
	android:visibility="gone"
	android:layout_width="@dimen/status_bar_icon_size"
	android:layout_height="fill_parent"
	android:src="@drawable/ic_sysbar_lights_out_dot_small"
	android:scaleType="center"
	android:paddingStart="6.0dip" />
its now from 5 lines into 17 lines.
so needs scripts to search for "android:" that preceded only with <one space char>
after spliting a preceding space will be remove and repace with a tab (single space only)
That bit in red above cannot give the output you are seeking. The true result would be:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.statusbar.phone.PhoneStatusBarView
	android:orientation="vertical" 
	android:id="@id/status_bar"
	android:background="@drawable/system_bar_background"
	android:focusable="true"
	android:descendantFocusability="afterDescendants"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">
    <ImageView android:id="@id/notification_lights_out"
	android:paddingBottom="2.0dip"
	android:visibility="gone"
	android:layout_width="@dimen/status_bar_icon_size"
	android:layout_height="fill_parent"
	android:src="@drawable/ic_sysbar_lights_out_dot_small"
	android:scaleType="center"
	android:paddingStart="6.0dip" />
Your proposed output strips the space if the source line was not indented, and substitutes tab for the space if the source line is indented.

Here is my proposal, along with a "simple" JREPL command to implement it:

For each line, determine the whitespace prefix for that line. Then for each "android" with preceding whitespace (separate from the prefix), strip the preceding whitespace, add newline + line prefix + 2 spaces + "android"

Code: Select all

jrepl "^\s*|\s+android" "prefix=$txt=$0|$txt='\r\n'+prefix+'  android'" /t "|" /xseq /jq /f status_bar.xml
--OUTPUT--

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.statusbar.phone.PhoneStatusBarView
  android:orientation="vertical"
  android:id="@id/status_bar"
  android:background="@drawable/system_bar_background"
  android:focusable="true"
  android:descendantFocusability="afterDescendants"
  androidTest
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui">
    <ImageView
      android:id="@id/notification_lights_out"
      android:paddingBottom="2.0dip"
      android:visibility="gone"
      android:layout_width="@dimen/status_bar_icon_size"
      android:layout_height="fill_parent"
      android:src="@drawable/ic_sysbar_lights_out_dot_small"
      android:scaleType="center"
      android:paddingStart="6.0dip" />
 
Note that my code relies on the fact that undeclared variables (prefix in this case) default to global scope. It wouldn't be bad to include /JBEG "var prefix" as an option to explicitly declare a global variable.


Dave Benham

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: spliting a single line into multiple lines

#10 Post by bars143 » 04 Aug 2018 08:59

thanks to Aacini and dbenham for their own alternative scripts.

now its easy and lessen mistakes when editing (and copy-and-paste) on .xml files

Post Reply