1



I have a txt file contains certain lines with constant structure, for example:

My Line is : Hi. This is row no.1.
My Line is : Hi. This is row no.2
My Line is : Hi. This is row no.3.

I would like to create a new file contains same rows, but to delete all characters before specific substring (for example: "Hi"),
as well as the last specific character of this row (for example: "."), only if exists.

The final txt file I expect for is:

Hi. This is row no.1
Hi. This is row no.2
Hi. This is row no.3

How can I do this?

Tali B.
  • 125
  • 1
  • 11
  • It is likely that a standard batch file script will be unable to perform this task as a generic request. _You are unlikely to get a solution you can just swap characters and substrings in and out and maintain a working script_. In order to perform this task with reasonable robustness we would need to see a real time example of the text file content, have a proper idea of the real specific substrings and have a real example of the last specific row characters. – Compo Dec 11 '16 at 12:17
  • Please share your efforts! – aschipfl Dec 11 '16 at 15:02
  • Using [JREPL.BAT - a regular expression text processor](http://www.dostips.com/forum/viewtopic.php?t=6044): `jrepl "^.*(?=Hi.) \.$" " " /t " " /f old.txt /o new.txt` – dbenham Dec 11 '16 at 16:20

2 Answers2

1

In batch, string replacement and substring extraction are done using the SET command. Look it up using set /? or help set. However, when using SET, there are some characters in the string that could be problematic, including !, since this solution uses delayed expansion. There are methods that don't use delayed expansion, but without a real example of the strings you're working with, I'm not sure whether you need them or not.

String replacement looks like this.

%VARIABLE:original=replacement%

With delayed expansion, which allows the variable values to be determined for each iteration in the for, instead of once at the beginning for the entire for block, replace the % with !.

Look at HELP SET for examples of substring expansion; there are a number of them depending on whether you index from the start or end of the string.

Here is one solution according to the sample data you provided and my comments above. For each line in test1.txt, it processes it and outputs it to output.txt.

@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in (test1.txt) do (
    set line=%%a
    set line=!line:My Line is : =!
    if "!line:~-1!"=="." set line=!line:~0,-1!
    >> output.txt echo !line!
)
soja
  • 1,587
  • 8
  • 8
-1

with cut
cut -f2 -d: infile > outfile

but that leaves the trailing dot,
could pipe that to something to trim it

or try sed

sed 's/.*:\(.*\)\.$/\1/g' infile > outfile

but that really expects the final dot to be there.

sed 's/.*: \(.*\)/\1/g;s/\.$//g' infile > outfile

seems to do it

tomc
  • 1,146
  • 6
  • 10
  • 2
    Any recommendation for doing it in Windows? The [batch-file] tag is Windows-exclusive. – SomethingDark Dec 11 '16 at 16:58
  • windows does not get to claim batch-file as the usage predates it. Also although you may be trapped in an 'exclusive' mindset bash is not. Run a web search or two and find windows natively supporting bash and lift your self out of that particular rut. cheers! – tomc Dec 11 '16 at 22:48
  • I'm not a ms user so I can't really make recommendations but cygwin is old and may be too demanding, maybe try https://blogs.windows.com/buildingapps/2016/03/30/run-bash-on-ubuntu-on-windows/ – tomc Dec 11 '16 at 22:58
  • 1
    It has nothing to do with my mindset. I completely agree that "batch-file" isn't a very good name for a tag for Windows batch; I'd rather see something like "ms-batch" or "windows-batch" but as far as I know tag names can't be changed. Also, installing third-party software or even significantly changing Windows configurations may not be an option in some environments, like at a workplace. Please consider that some of us may be limited to significantly fewer alternatives than you are. – SomethingDark Dec 12 '16 at 02:14