Page 1 of 1

spliting a single line into multiple lines

Posted: 28 Apr 2018 01:17
by bars143
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

Re: spliting a single line into multiple lines

Posted: 28 Apr 2018 11:01
by Squashman
Why are you not using JREPL to match on a space and the word Android?

Re: spliting a single line into multiple lines

Posted: 28 Apr 2018 13:24
by bars143
i should start and relearn of what i done before and i go back where i start.thanks for suggestion

Re: spliting a single line into multiple lines

Posted: 28 Apr 2018 20:09
by bars143
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

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

Posted: 29 Apr 2018 14:39
by bars143
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

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

Posted: 29 Apr 2018 14:48
by bars143
SOLVED.

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

Posted: 30 Jun 2018 17:05
by thefeduke
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.

Re: spliting a single line into multiple lines

Posted: 30 Jun 2018 21:24
by Aacini
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

Re: spliting a single line into multiple lines

Posted: 03 Jul 2018 09:39
by dbenham
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

Re: spliting a single line into multiple lines

Posted: 04 Aug 2018 08:59
by bars143
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