Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

yanklines

- - - - -

  • Please log in to reply
52 replies to this topic

#26
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Supervisor
  • 5,866 posts
  • Joined 07-April 07
  • OS:98SE
  • Country: Country Flag

Donator

Sure! :)


How to remove advertisement from MSFN

#27
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
Is there any simple way to modify yanklines to treat all lines with spaces/tabs at the beginning/end as equal (regardless of the number of spaces)?

I mean sth like this:

"abc.txt"
"abc.txt"      <-spaces / tabs
"abc.txt"                                   <-spaces / tabs
spaces->            "abc.txt"

Edited by tomasz86, 26 July 2012 - 05:30 AM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#28
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Supervisor
  • 5,866 posts
  • Joined 07-April 07
  • OS:98SE
  • Country: Country Flag

Donator

Not easily, no. This kind of thing is better done with sed. sed is unix, of course, but there are countless ports of sed for Win32. I like the cygwin version, but that needs also cygwin1.dll, so probably something based on mingw may suit you better. And sed is very powerful, so I do think you should invest the time needed to get familiar with it.

#29
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
I guess I'll need to study sed later on but at the moment I've managed to get through it using the following method.


1) This is a real-life example:

AppPatch.Help.Files=10,"Help"<-no spaces
AppPatch.Help.Files=10,"Help"              <-spaces
Com.Files=11,com<-no spaces
Com.Files=11,com          <-spaces
ADMT.FIles=65622<-no spaces
ADMT.FIles=65622          <-spaces

2) The cmd script:

FOR /F %%I IN ('FINDSTR/V "," 1.inf') DO ECHO>>temp.txt %%I
FOR /F tokens^=1-2^ delims^=^", %%I IN ('FINDSTR ^"^"^" 1.inf') DO ECHO>>temp.txt %%I"%%J"
FOR /F "tokens=1,2 delims=, " %%I IN ('FINDSTR "," 1.inf ^| FINDSTR/V ^"^"^"') DO ECHO>>temp.txt %%I,%%J

3) Result:

ADMT.FIles=65622<-no spaces
ADMT.FIles=65622<-no spaces
AppPatch.Help.Files=10,"Help"<-no spaces
AppPatch.Help.Files=10,"Help"<-no spaces
Com.Files=11,com<-no spaces
Com.Files=11,com<-no spaces

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#30
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,534 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

I've managed to get through it using the following method.

FOR /F %%I IN ('FINDSTR/V "," 1.inf') DO ECHO>>temp.txt %%I
FOR /F tokens^=1-2^ delims^=^", %%I IN ('FINDSTR ^"^"^" 1.inf') DO ECHO>>temp.txt %%I"%%J"
FOR /F "tokens=1,2 delims=, " %%I IN ('FINDSTR "," 1.inf ^| FINDSTR/V ^"^"^"') DO ECHO>>temp.txt %%I,%%J

Not only does your 'fix' not show that your request of white space from the beginning of lines has been fulfilled you apear to have been a little carried away with a specific character, ^.

#31
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
@Yzöwl

Well, the request was more general while here I need to deal with specific lines and at the moment I don't know of any better way then the one I used above.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#32
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Supervisor
  • 5,866 posts
  • Joined 07-April 07
  • OS:98SE
  • Country: Country Flag

Donator

Yes, you do. By using sed (and perhaps unix2dos to correct the line terminators depending on the implementation of sed you choose) almost any modification one can conceieve, however complex, can be performed fast. I put yanklines together to remove dupes fast, in a file, which is very far from the things sed does well. But while you want to reformat individual lines, one by one, no other tool is needed since sed is very powerful and time-proven. sed can be used as a filter, just like FIND.

# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
sed 's/^[ \t]*//'                    # see note on '\t' at end of file

<...>
USE OF '\t' IN SED SCRIPTS: For clarity in documentation, we have used
the expression '\t' to indicate a tab character (0x09) in the scripts.
However, most versions of sed do not recognize the '\t' abbreviation,
so when typing these scripts from the command line, you should press
the TAB key instead. '\t' is supported as a regular expression
metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.
Reference: sed1liners

#33
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,812 posts
  • Joined 13-January 06
For the record autoit has a function to remove white space of any kind (tab included).

#34
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,534 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

I need to deal with specific lines and at the moment I don't know of any better way then the one I used above.

Depending upon poison characters, and using a cmd file, simply using a CALL may be all you need.
FOR /F "DELIMS=" %%# IN (1.INF) DO CALL :SUB %%#

GOTO :EOF

:SUB

>>TEMP.TXT ECHO=%*


#35
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag

Depending upon poison characters, and using a cmd file, simply using a CALL may be all you need.

FOR /F "DELIMS=" %%# IN (1.INF) DO CALL :SUB %%#
GOTO :EOF
:SUB
>>TEMP.TXT ECHO=%*

You're right :w00t:


There are sometimes comments present:

SystemRoot.Files=10             ; %windir% (replace if exist)
so this seems to work perfectly:

FOR /F "DELIMS=;" %%# IN (1.INF) DO CALL :SUB %%#
GOTO :EOF
:SUB
>>TEMP.TXT ECHO=%*
Thank you.

Edit: Actually it's still not perfect. This gets broken:

Source:
TSClientDefault.Files=65618,%TSCLIENTDIR%
Result:
TSClientDefault.Files=65618,

Edited by tomasz86, 27 July 2012 - 10:07 PM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#36
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
This is a "minified" version of the silent yanklines script which is created on the fly from commandline:

ECHO>yanklines.vbs ^If WScript.Arguments.Count ^<^> 2 then
ECHO>>yanklines.vbs WScript.Quit
ECHO>>yanklines.vbs end ^If
ECHO>>yanklines.vbs Const ForReading=1, ForWriting=2
ECHO>>yanklines.vbs Dim i,j
ECHO>>yanklines.vbs Set objFSO=CreateObject("Scripting.FileSystemObject")
ECHO>>yanklines.vbs Set objInputFile=objFSO.OpenTextFile(WScript.Arguments.Item(0),ForReading)
ECHO>>yanklines.vbs Set objOutputFile=objFSO.OpenTextFile (WorkingDir ^& WScript.Arguments.Item(1),ForWriting,True)
ECHO>>yanklines.vbs Set objDict=CreateObject("Scripting.Dictionary")
ECHO>>yanklines.vbs objDict.CompareMode=1
ECHO>>yanklines.vbs j=0
ECHO>>yanklines.vbs On Error Resume Next
ECHO>>yanklines.vbs While Not objInputFile.AtEndOfStream
ECHO>>yanklines.vbs arrinputRecord=split(objInputFile.Readline,"vbNewLine")
ECHO>>yanklines.vbs strFirstField=arrinputRecord(0)
ECHO>>yanklines.vbs ^If objDict.Exists(strFirstField) then
ECHO>>yanklines.vbs j=j+1
ECHO>>yanklines.vbs Else
ECHO>>yanklines.vbs objDict.add strFirstField,strFirstField
ECHO>>yanklines.vbs End ^If
ECHO>>yanklines.vbs Wend
ECHO>>yanklines.vbs colKeys=objDict.Keys
ECHO>>yanklines.vbs For Each strKey in colKeys
ECHO>>yanklines.vbs objOutputFile.writeline objDict.Item(strKey)
ECHO>>yanklines.vbs Next
ECHO>>yanklines.vbs objInputFile.Close
ECHO>>yanklines.vbs objOutputFile.Close
Maybe someone will fine it useful.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#37
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,397 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

This is a "minified" version of the silent yanklines script which is created on the fly from commandline:
....
Maybe someone will fine it useful.


IMHO this is - besides smaller - "plainer":
1.000 bytes instead of 1.356 BUT including 10 bytes for the "@ECHO OFF", so actually 366 bytes smaller or almost 27% smaller :w00t: ;).

@ECHO OFF&(
ECHO ^If WScript.Arguments.Count ^<^> 2 then
ECHO WScript.Quit
ECHO end ^If
ECHO Const ForReading=1, ForWriting=2
ECHO Dim i,j
ECHO Set objFSO=CreateObject^("Scripting.FileSystemObject"^)
ECHO Set objInputFile=objFSO.OpenTextFile^(WScript.Arguments.Item^(0^),ForReading^)
ECHO Set objOutputFile=objFSO.OpenTextFile ^(WorkingDir ^& WScript.Arguments.Item^(1^),ForWriting,True^)
ECHO Set objDict=CreateObject^("Scripting.Dictionary"^)
ECHO objDict.CompareMode=1
ECHO j=0
ECHO On Error Resume Next
ECHO While Not objInputFile.AtEndOfStream
ECHO arrinputRecord=split^(objInputFile.Readline,"vbNewLine"^)
ECHO strFirstField=arrinputRecord^(0^)
ECHO ^If objDict.Exists^(strFirstField^) then
ECHO j=j+1
ECHO Else
ECHO objDict.add strFirstField,strFirstField
ECHO End ^If
ECHO Wend
ECHO colKeys=objDict.Keys
ECHO For Each strKey in colKeys
ECHO objOutputFile.writeline objDict.Item^(strKey^)
ECHO Next
ECHO objInputFile.Close
ECHO objOutputFile.Close
)>yanklines.vbs

jaclaz

#38
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
Nice ;)

I'm wondering if these lines are absolutely necessary:

1.
If WScript.Arguments.Count <> 2 then 
WScript.Quit 
end If

2.
objInputFile.Close
objOutputFile.Close
The script seems to work without them too...
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#39
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,798 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag
I believe (1) is very necessary, or or least the "< 2" part is, since I'm not sure what will happen, besides error, if you do not specify both the input and output file. If you have extra arguments, ie "> 2", they will just be ignored but it is an indication that something is screwed up in the program call.

AFAIK, (2) is not "necessary" since both files "should" be closed when the script exits, but it is best practice and I would always do it. You should always try to clean up after yourself, and not depend on what "should" happen. But that's just my opinion.

Cheers and Regards

Edited by bphlpt, 19 September 2012 - 09:11 PM.

Posted Image


#40
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Supervisor
  • 5,866 posts
  • Joined 07-April 07
  • OS:98SE
  • Country: Country Flag

Donator

I'm wondering if these lines are absolutely necessary:

2.

objInputFile.Close
objOutputFile.Close
The script seems to work without them too...

Removing these lines you'd be introducing a potential bug by relying on behavior which is not guaranteed.
And running a large number of times such a buggy script may lead to all kinds of hard to debug conditions.
Don't ever forget to close all open files before terminating.

#41
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,397 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

I believe (1) is very necessary, or or least the "< 2" part is, since I'm not sure what will happen, besides error, if you do not specify both the input and output file. If you have extra arguments, ie "> 2", they will just be ignored but it is an indication that something is screwed up in the program call.

I don't get it. :unsure:
That would be a check to make sure that exactly 2 arguments are passed to the script, if the script is invoked only from a batch and only with the correct 2 arguments there is no need for that check (which should be made BEFORE in the calling batch).
Or am I missing something?

jaclaz

#42
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag

Removing these lines you'd be introducing a potential bug by relying on behavior which is not guaranteed.
And running a large number of times such a buggy script may lead to all kinds of hard to debug conditions.
Don't ever forget to close all open files before terminating.

What do you mean by "closing" files?

This is how I use the script:

(
ECHO ^If WScript.Arguments.Count ^<^> 2 then
ECHO WScript.Quit
ECHO end ^If
ECHO Const ForReading=1, ForWriting=2
ECHO Dim i,j
ECHO Set objFSO=CreateObject^("Scripting.FileSystemObject"^)
ECHO Set objInputFile=objFSO.OpenTextFile^(WScript.Arguments.Item^(0^),ForReading^)
ECHO Set objOutputFile=objFSO.OpenTextFile ^(WorkingDir ^& WScript.Arguments.Item^(1^),ForWriting,True^)
ECHO Set objDict=CreateObject^("Scripting.Dictionary"^)
ECHO objDict.CompareMode=1
ECHO j=0
ECHO On Error Resume Next
ECHO While Not objInputFile.AtEndOfStream
ECHO arrinputRecord=split^(objInputFile.Readline,"vbNewLine"^)
ECHO strFirstField=arrinputRecord^(0^)
ECHO ^If objDict.Exists^(strFirstField^) then
ECHO j=j+1
ECHO Else
ECHO objDict.add strFirstField,strFirstField
ECHO End ^If
ECHO Wend
ECHO colKeys=objDict.Keys
ECHO For Each strKey in colKeys
ECHO objOutputFile.writeline objDict.Item^(strKey^)
ECHO Next
ECHO objInputFile.Close
ECHO objOutputFile.Close
)>TEMP\yanklines.vbs
FOR /F "tokens=* delims=" %%I IN ('DIR/B/S "%SPLITDIR%"') DO (
	START "yanklines" /WAIT TEMP\yanklines.vbs "%%I" "TEMP\temp.txt"
	MOVE "TEMP\temp.txt" "%%I"
)

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#43
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,798 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag


I believe (1) is very necessary, or or least the "< 2" part is, since I'm not sure what will happen, besides error, if you do not specify both the input and output file. If you have extra arguments, ie "> 2", they will just be ignored but it is an indication that something is screwed up in the program call.

I don't get it. :unsure:
That would be a check to make sure that exactly 2 arguments are passed to the script, if the script is invoked only from a batch and only with the correct 2 arguments there is no need for that check (which should be made BEFORE in the calling batch).
Or am I missing something?

jaclaz


You are absolutely correct, of course, as long as the assumption is assured. I think both Den and I were looking at the code snippet from a "stand-alone" perspective, or as a function you might paste into another piece of code or share with someone else. No comment was made in the last few posts as to any assumptions or requirements, either before or after yanklines.vbs is invoked, so not knowing if the proper error checking had been done before the function was called ...


Removing these lines you'd be introducing a potential bug by relying on behavior which is not guaranteed.
And running a large number of times such a buggy script may lead to all kinds of hard to debug conditions.
Don't ever forget to close all open files before terminating.

What do you mean by "closing" files?


He's talking about

objInputFile.Close
objOutputFile.Close

Cheers and Regards

Edited by bphlpt, 20 September 2012 - 10:28 AM.

Posted Image


#44
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag

objInputFile.Close
objOutputFile.Close

I know that ;) What I don't know is whether these lines may have any impact when the script is used like I do it.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#45
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,397 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag
Slightly bigger but allows to copy and paste the script "as is" (without any need for carets) at the END of the batch....

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

FOR /F "tokens=1 delims=[]" %%A in ('FIND /N "::THIS IS START OF VBS SCRIPT::" %~dpnx0') DO SET More_offset=%%A
ECHO 'Hi I am yanklines.vbs script>yanklines.vbs
FOR /F "tokens=*" %%A IN ('MORE +%More_offset% %~dpnx0') DO ECHO %%A>>yanklines.vbs
ECHO do whatever the batch is supposed to do
GOTO :EOF

::THIS IS START OF VBS SCRIPT::
If WScript.Arguments.Count <> 2 then
WScript.Quit
end If
Const ForReading=1, ForWriting=2
Dim i,j
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objInputFile=objFSO.OpenTextFile(WScript.Arguments.Item(0),ForReading)
Set objOutputFile=objFSO.OpenTextFile (WorkingDir & WScript.Arguments.Item(1),ForWriting,True)
Set objDict=CreateObject("Scripting.Dictionary")
objDict.CompareMode=1
j=0
On Error Resume Next
While Not objInputFile.AtEndOfStream
arrinputRecord=split(objInputFile.Readline,"vbNewLine")
strFirstField=arrinputRecord(0)
If objDict.Exists(strFirstField) then
j=j+1
Else
objDict.add strFirstField,strFirstField
End If
Wend
colKeys=objDict.Keys
For Each strKey in colKeys
objOutputFile.writeline objDict.Item(strKey)
Next
objInputFile.Close
objOutputFile.Close

jaclaz

#46
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,798 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag


objInputFile.Close
objOutputFile.Close

I know that ;) What I don't know is whether these lines may have any impact when the script is used like I do it.


In a perfect world, where things always work the way they are supposed to, no I don't think so. But as Den explained, just because those files are "supposed" to be closed automatically when the script exits, there is no guarantee that they will, due to system error or whatever. I know that there is never a guarantee with computers anyway, but to decrease the possibility of error with very minimal effort on your part, it is always best practice to clean up after yourself and do what you can to ensure that files are closed and system resources are released when you no longer need them.

Cheers and Regards

Posted Image


#47
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Supervisor
  • 5,866 posts
  • Joined 07-April 07
  • OS:98SE
  • Country: Country Flag

Donator



objInputFile.Close
objOutputFile.Close

I know that ;) What I don't know is whether these lines may have any impact when the script is used like I do it.


In a perfect world, where things always work the way they are supposed to, no I don't think so. But as Den explained, just because those files are "supposed" to be closed automatically when the script exits, there is no guarantee that they will, due to system error or whatever. I know that there is never a guarantee with computers anyway, but to decrease the possibility of error with very minimal effort on your part, it is always best practice to clean up after yourself and do what you can to ensure that files are closed and system resources are released when you no longer need them.

I couldn't put it any better if I tried. And this is something that holds true for any generalized OS, which usually are articulated around the files & resources metaphor(s) (except, maybe, for those few that are explicitly built around strongly-enforced "garbage-collection").

#48
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
Understood :)

But the first three lines are not required when both source and destination is always specified as in my script, right?

If WScript.Arguments.Count <> 2 then
WScript.Quit
end If

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#49
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Supervisor
  • 5,866 posts
  • Joined 07-April 07
  • OS:98SE
  • Country: Country Flag

Donator

Yes. Any conditional that is either always true or never true is not needed.

#50
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,798 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag
It goes back to the distinction between "general purpose" and "specialized". A general purpose function that might end up being used anywhere needs all kinds of error checking for safety for the app and the system it is run on. The more specialized the function is, the more conditions are assumed or required and the smaller the code can be, as long as it can be guaranteed that those conditions are always met. The error checking always needs to be done, explicitly by the function or implicitly by the programmer. Either in the function itself, or external to it as jaclaz stated:

(which should be made BEFORE in the calling batch)


And then it gets down to programmer's preference. Some like to have redundant checks everywhere, trying to make their code absolutely bulletproof. It will never happen, human error, machine error, and fate will prevent it, but you can get close. Others like to program lean and mean with the absolute minimal checks. That produces smaller, faster code, not as big a requirement these days with processor speed and memory and disc sizes and prices being what they are, but in a perfect world it will run very well. Or you could do a middle ground of the minimal checks plus some extra ones "just in case". If the extra checks are just a line or two and don't take a lot of time to execute, then put them in. You can always pull them back out later as you are optimizing your code and you see which conditions are most likely to occur. That is what you are doing now, and you are doing it the right way, carefully. :)

-::End of philosophical programming methodology rant::-

Cheers and Regards

Posted Image





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN