fpc : 连续2个以上的空格替换成1个空格

最简单的shell 命令: cat tmp.txt |tr -s ' ' > tmp2.txt

我们先看看 python 的写法: 

space1.py

# -*- coding: cp936 -*-
import os, sys
import re

if len(sys.argv) ==2:
    f1 = sys.argv[1]
else:
    print 'usage: space1.py file1.txt '
    sys.exit(1)

if not os.path.exists(f1):
    print 'ERROR: %s not found\n' % f1
    sys.exit(1)

fp = open(f1,'rt')
f2 = f1 +'.bak'
fp2 = open(f2,'w')
aline =''
for line in fp:
    aline = re.sub(r'[ ]{2,}',' ',line)
    fp2.write(aline)
#
fp.close()
fp2.close()


我们看看free pascal 写法:

space1.pas

Program space1;
{$mode objfpc}{$H+}
uses sysutils;
Var
  f1,f2:text;
  str,str2:string;
  ln:integer;
Begin
  if ParamCount =1 then
    begin
	AssignFile(f1,ParamStr(1));
	AssignFile(f2,ParamStr(1)+'.bak');
    end
  else begin
	if ParamCount =2 then
	  begin
	  Assign(f1,ParamStr(1));
	  Assign(f2,ParamStr(2));
	  end
    else begin
	  writeln(' usage: readfile file1.txt ');
	  writeln(' usage: readfile file1.txt file2.bak ');
	  exit;
	end;
  end;
  ln:=0;
  Reset(f1);
  Rewrite(f2);
  while not eof(f1) do
  begin
	ln:=ln+1;
	readln(f1,str);
	if length(str)>0 then
	begin
	  while Pos('  ',str)>0 do
	  begin
	    str := StringReplace(str,'  ',' ',[rfReplaceAll]);
	  end;
	  str2 := str;
	  writeln(f2,str2);
    end;
  end;
  CloseFile(f2);
  CloseFile(f1);
  writeln('line number: ',ln);
End.


编译 fpc space1.pas

运行 space1.exe  tmp.txt

这是用户区的settings.json: { "code-runner.fileDirectoryAsCwd": true, "code-runner.runInTerminal": true, "code-runner.clearPreviousOutput": true, "security.workspace.trust.untrustedFiles": "open", "markdown-preview-enhanced.mathRenderingOption": "MathJax", "markdown.extension.completion.enabled": true, "python.defaultInterpreterPath": "C:\\Users\\imsfp\\AppData\\Local\\Programs\\Python\\Python313\\python.exe", "editor.fontFamily": "Maple Mono, Consolas, monospace", "terminal.integrated.fontFamily": "monospace", "workbench.iconTheme": "vscode-icons", "workbench.colorCustomizations": { "editorCursor.foreground": "#FF0000", "terminalCursor.foreground": "#FF5555" }, "window.titleBarStyle": "custom", "python.terminal.executeInFileDir": true, "python.analysis.completeFunctionParens": true, "code-runner.executorMap": { "python": "set PYTHONIOENCODING=utf8 && $pythonPath -u $fullFileName" }, "code-runner.showExecutionMessage": false, "git.ignoreMissingGitWarning": true, "git.openRepositoryInParentFolders": "never", "github.copilot.chat.agent.thinkingTool": true, "github.copilot.chat.languageContext.typescript.enabled": true, "github.copilot.chat.newWorkspaceCreation.enabled": true, "github.copilot.chat.editor.temporalContext.enabled": true, "terminal.integrated.defaultProfile.windows": "Windows PowerShell", "editor.formatOnSave": true, "python.analysis.typeCheckingMode": "strict", "chat.editing.confirmEditRequestRetry": false, "github.copilot.chat.agent.toolValidation": "strict", "github.copilot.codeQualityScan.enabled": true, "chat.mcp.discovery.enabled": true, "[markdown]": { "editor.defaultFormatter": "DavidAnson.vscode-markdownlint" }, "explorer.confirmDelete": false, "files.autoSave": "afterDelay", "geminicodeassist.project": "noble-subset-466101-u9", "IntelligentCodeCompletion.disableLanguages": { "vue": "false", "typescript": "false", "javascript": "false", "python": "false", "go": "false", "c": "false", "c++": "false", "shell": "false", "bash": "false", "batch": "false", "lua": "false", "java": "false", "php": "false", "ruby": "false" }, "FunctionQuickCommands.disableLanguages": { "typescript": "false", "javascript": "false", "python": "false", "go": "false", "c": "false", "c++": "false", "lua": "false", "java": "false", "php": "false", "ruby": "false" }, "IntelligentCodeCompletion.enabled": true, "zgsm.allowedCommands": [ "npm test", "npm install", "tsc", "git log", "git diff", "git show" ], "github.copilot.nextEditSuggestions.enabled": true, "[python]": { "diffEditor.ignoreTrimWhitespace": false, "editor.defaultColorDecorators": "never", "gitlens.codeLens.symbolScopes": [ "!Module" ], "editor.formatOnType": true, "editor.wordBasedSuggestions": "off" }, "terminal.integrated.splitCwd": "workspaceRoot", "code-runner.executorMapByFileExtension": { ".vb": "cd $dir && vbc /nologo $fileName && $dir$fileNameWithoutExt", ".vbs": "cscript //Nologo", ".scala": "scala", ".jl": "julia", ".cr": "crystal", ".ml": "ocaml", ".zig": "zig run", ".exs": "elixir", ".hx": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt", ".rkt": "racket", ".scm": "csi -script", ".ahk": "autohotkey", ".au3": "autoit3", ".kt": "cd $dir && kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar", ".kts": "kotlinc -script", ".dart": "dart", ".pas": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt", ".pp": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt", ".d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt", ".hs": "runhaskell", ".nim": "nim compile --verbosity:0 --hints:off --run", ".csproj": "dotnet run --project", ".fsproj": "dotnet run --project", ".lisp": "sbcl --script", ".kit": "kitc --run", ".v": "v run", ".vsh": "v run", ".sass": "sass --style expanded", ".cu": "cd $dir && nvcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", ".ring": "ring", ".sml": "cd $dir && sml $fileName", ".mojo": "mojo run", ".erl": "escript", ".spwn": "spwn build", ".pkl": "cd $dir && pkl eval -f yaml $fileName -o $fileNameWithoutExt.yaml", ".gleam": "gleam run -m $fileNameWithoutExt" }, "code-runner.saveAllFilesBeforeRun": true, "code-runner.saveFileBeforeRun": true, "code-runner.languageIdToFileExtensionMap": { "bat": ".bat", "powershell": ".ps1", "typescript": ".ts" }, "code-runner.terminalRoot": "'/mnt/", } 检查是哪个设置出了问题使得coderunner持续的使用unix风格命令
最新发布
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值