Solved

Batch file find words in text file

Posted on 2016-09-22
2
30 Views
Last Modified: 2016-11-07
Read all the record from a home inventory text file,  find the first space reading from left to right, define key or word as the string to the left of the space. Write that key to new file then sort the file and remove duplicates. The input file is a list of inventory items for a home. First word is like Bed RM1, Bed RM2, Utility Rm, Garage and etc followed by item descriptiom, cost, and age.Steve King had a solution on the code for removing dips in 5-14-2012
0
Comment
Question by:Sherwood Lemoine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 41811840
Assumes your input file named Rooms.txt which looks like:
Bed RM1, description, cost, 10
Bed RM2, description, cost, 10
Utility Rm, description, cost, and age
Garage, description, cost, and age
Storage Rm, description, cost, and age

Open in new window

Make a unique.bat file of code below.

@echo off
setlocal enabledelayedexpansion

set FNInput=Rooms.txt
set FNOut1=Output_temp.txt
if exist "%FNOut1%" del /q "%FNOut1%"
for /f "tokens=1 delims=," %%a in ('type "%FNInput%"') do (
  for /f "tokens=1" %%A in ('echo %%a') do (
    >> "%FNOut1%" echo %%A
  )
)
if exist "%FNOut1%" (
  set "prev="
  for /f "delims=" %%F in ('sort "%FNOut1%"') do (
    set "curr=%%F"
    setlocal enabledelayedexpansion
    if !prev! neq !curr! echo !curr!
    endlocal
    set "prev=%%F"
  )
)

Open in new window


Run it like so:
unique.bat>output.txt

Open in new window


Resultant output.txt:
Bed
Garage
Storage
Utility

Open in new window

0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question