CMD脚本:读取并保存每行的最后一个单词(多行) [英] CMD script: Reading and saving the last word of each line (multiple lines)

查看:95
本文介绍了CMD脚本:读取并保存每行的最后一个单词(多行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于查找和保存每行的最后一个单词。

My question is about finding and saving the last word of each line.

我有多行。此信息保存在 .txt 文件中。因此,我需要构建一个CMD脚本,该脚本可以扫描txt文件,查找每行的最后一个单词并将其保存到新的 .txt 文档中。分隔符将在空间中。

I have multiple lines. This information is saved in a .txt file. Thus, I am required to build a CMD script that can scan through the txt file, find the last word of each line and save it to a new .txt document. The delimiters here will be the space.

例如:


It's a very small window<34>.
The small birds were singing softly.
There are three small rooms up stairs (4)
The house has but two small second story bedrooms.


我希望看到的结果保存在单独的txt文档中需要如下所示:

The result I would like to see saved in a separate txt document needs to look like the following:


window<34>.
softly.
(4)
bedrooms.


我尝试了很多选择和方法,但不幸的是没有运气。 / p>

I have tried many options and methods but unfortunately no luck.

推荐答案

此纯Batch方法可以正确处理所有字符,但每行字数受限制(此版本为26) ,尽管可以通过常用技巧

This pure Batch method correctly process all characters, but it is limited in the number of words per line (26 in this version), although such a limit may be increased via the usual tricks.

@echo off
setlocal DisableDelayedExpansion

for /F tokens^=1-26^ eol^= %%a in (input.txt) do (
   set "last="
   for %%T in (z y x w v u t s r q p o n m l k j i h g f e d c b a) do (
      if not defined last call :sub %%T
   )
)
goto :EOF

:sub token
for %%? in (-) do (
   set "last=%%%1"
   if defined last echo %%%1
)
exit /B

这篇关于CMD脚本:读取并保存每行的最后一个单词(多行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆