Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch file find words in text file

Posted on 2016-09-22
2
Medium Priority
?
38 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
2 Comments
 
LVL 25

Accepted Solution

by:
NVIT earned 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

971 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