Jump to content

Removing carriage return between two strings


Recommended Posts

Disclaimer: I've never coded and I just started AutoIt yesterday. Total newb alert.  :

I need to do a very, very simple task in a text file, but apparently some necessary syntax is eluding me.

I need to take this bit of text:

      SQUAD,

      NAME_RIFLE_formation),

 

And turn it into this:

      SQUAD,NAME_RIFLE_formation),

 

Essentially I need to remove the carriage return and tab between the "SQUAD," and "NAME_RIFLE_formation)," strings.

EDIT to add: This text is part of a larger file (a simple text document). I can't just remove all carriage returns, I can only very specifically remove the carriage returns between these two strings of text.

I've tried StringStripCR:

 

Local $result = StringStripCR("SQUAD," & Chr(13) & Chr(10) & Chr(9) & "NAME_RIFLE_formation),")

 

I also tried StringStripWS.

When I run either, nothing happens.

I think I'm messing up some syntax or something. What am I doing wrong?

Edited by JamesBiggles
Link to comment
Share on other sites

  • Moderators

JamesBiggles,

Welcome to the AutoIt forum. :)

Is this text part of a larger file? I ask because the task of removing the @CRLF in that case is significantly different to simple case of it being a stand-alone string as in your OP. :huh:

M23

P.S. Any relation to this well-known James Bigglesworth? ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

JamesBiggles,

Welcome to the AutoIt forum. :)

 

 

Thank you!

Is this text part of a larger file? I ask because the task of removing the @CRLF in that case is significantly different to simple case of it being a stand-alone string as in your OP.  :huh:

 

Ah yes, should have mentioned that. This text is part of a larger file (a simple text document). I can't just remove all carriage returns, I can only very specifically remove the carriage returns between these two strings of text.

P.S. Any relation to this well-known James Bigglesworth ;) 

 

 

I wish!

Edited by JamesBiggles
Link to comment
Share on other sites

Then you need to define the rules. Strip CR when line doesn't end with a ), ?

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • Moderators

JamesBiggles,

I would use a RegEx to do this:

$sText = "Blah blah" & @CRLF & _
         "SQUAD,"  & @CRLF & _
         "NAME_RIFLE_formation)," & @CRLF & _
         "blah blah"


$sNewText = StringRegExpReplace($sText, "(?is)^(.*squad,).*(name_rifle.*)$", "$1$2")

ConsoleWrite($sNewText & @CRLF)
To explain:

(?is)            - Make everything case-insensitive and treat line breaks as normal characters
^                - Start at the beginning and
(.*squad,)       - capture everything up to the word "squad"
.*               - Ignore anything until
(name_rifle.*)   - we capture everything from "name_rifle" onwards to 
$                - the end

$i$2             - Replace everything above by the 2 groups we captured
Makes the head spin - but it is the best way to do this! :D

Please ask if you have any questions. :)

M23

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

JamesBiggles,

I would use a RegEx to do this:

$sText = "Blah blah" & @CRLF & _
     "SQUAD,"  & @CRLF & _
     "NAME_RIFLE_formation)," & @CRLF & _
     "blah blah"


$sNewText = StringRegExpReplace($sText, "(?is)^(.*squad,).*(name_rifle.*)$", "$1$2")

ConsoleWrite($sNewText & @CRLF)
Please ask if you have any questions. :)

 

 

What are the "Blah blah"s I see in the $sText?

Link to comment
Share on other sites

  • Moderators

JamesBiggles,

They represent the additional text you said you had in the file. ;)

Just replace the beginning of the code with this and run the script on your actual file:

$sText = FileRead("Your_Filename")
Now you have the real file content in the $sText variable and you can see if it works "in the wild". If it does not then I suggets you post a copy of the file (perhaps suitably redacted) here so we can see what is going on. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

JamesBiggles,

That is a significantly different problem to the one you initially suggested. In future, please give us the full requirement from the beginning as having the goal posts moved mid-thread is extremely annoying. It usually means that the assistance given previously is not adapted to the particular case and so a complete waste of time for those who bothered to answer - and who might not bother to continue. Point taken I hope. ;)

It is not difficult to read in that basic file and modify it as you require. It is worth noticing that the file contains 2 forms of EOL (@CR and @CRLF) so that might well have led to your difficulties. Here is my take on how you might do it:

#include <Constants.au3>

; Read generic file
$sGeneric = FileRead("SquadData_GENERIC.h") ; 0d and 0d0a EOL!!!!

; Replace all EOL with ,
$sStandard = StringRegExpReplace($sGeneric, "((?<!\x0d)\x0a|\x0d(?!\x0a)|\x0d\x0a)", ",")
; Strip "
$sStripped = StringReplace($sStandard, '"', '')
; Split into elements
$aElements = StringSplit($sStripped, ",")

; Declare variable to hold final result
$sFinal = "(" & @CRLF
; Now loop through elements and create the new string
For $i = 1 To $aElements[0]
    ; Read element
    $sElement = $aElements[$i]
    ; Act depending on content
    Switch $sElement
        Case "SQUAD"
            ; Add brackets and remove internal blanks
            $sFinal &= @TAB & $sElement & "(" & $aElements[$i + 1] & "_formation)," & @CRLF
            $i += 1
        Case Else
            ; If it begins "TEAM_"
            If StringLeft($sElement, 5) = "TEAM_" Then
                ; Add internal and closing and opening braces
                $sFinal &= @TAB & "{" & $sElement & "," & @CRLF & @TAB & $aElements[$i + 6] & "}," & @CRLF & "}" & @CRLF & "{" & @CRLF
                ; Skip to next section element
                $i += 6
            Else
                ; Add element
                $sFinal &= @TAB & $sElement & "," & @CRLF
            EndIf
    EndSwitch
Next
; Remove the excess opening brace
$sFinal = StringTrimRight($sFinal, 8)

; And here is what we get
MsgBox($MB_SYSTEMMODAL, "Final", $sFinal)
Does that fit the bill? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 1 year later...

Late :) 

ok, much later, but also a quick option to remove the control characters. Simply do it twice with Stringreplace() -> one time for each control character...

#include <String.au3>

$sText = "Blah blah" & @CRLF & _
        "SQUAD," & @CRLF & _
        "NAME_RIFLE_formation)," & @CRLF & _
        "blah blah"

$sText = StringReplace($sText, @CR, "") ;CR out
$sText = StringReplace($sText, @LF, "") ;LF out

ConsoleWrite($sText & @CRLF)

Regards, Tombac




			
		
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...