?
Solved

Simple batch script to remove first word from each line

Posted on 2011-10-21
6
Medium Priority
?
2,440 Views
Last Modified: 2012-05-12
I require help with the writing of a simple batch script that will read an input file "input.txt" and remove the first word from each line.

The results should be written to an output file named "output.txt".

Example input.txt:

Word1 Word2 Word3.......Wordn
Word1 Word2 Word3.......Wordn
Word1 Word2 Word3.......Wordn
Word1 Word2 Word3.......Wordn
Word1 Word2 Word3.......Wordn

Example output.txt:

Word2 Word3 ........Wordn
Word2 Word3 ........Wordn
Word2 Word3 ........Wordn
Word2 Word3 ........Wordn
Word2 Word3 ........Wordn

Thank you!
0
Comment
Question by:Barbulescu
  • 3
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
paultomasi earned 2000 total points
ID: 37005410
@echo off
(for /f "tokens=1,*" %%a in (input.txt) do echo %%b)>output.txt
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37005434
If you're using quotes around your input filename (because it contains spaces) then you can do it like this:
@echo off
(for /f "tokens=1,* usebackq" %%a in ("input.txt") do echo %%b)>"output.txt"

Open in new window

Note: The option 'usebackq' is only needed if you use quotes around input.txt.
0
 

Expert Comment

by:yvovolders
ID: 37005443
One line:

cat input.txt | cut -d " " -f 2-
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

Expert Comment

by:yvovolders
ID: 37005444
One line:

cat input.txt | cut -d " " -f 2-
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37005463
Can also do it directly from the command prompt with the following command:

(for /f "tokens=1,*" %a in (input.txt) do @echo %b)>output.txt

Note: The difference is one '%' character instead of two as used in batch files. I've also slipped in a '@' character in front of 'echo' otherwise DOS displays each line prior to executing it.

0
 

Author Closing Comment

by:Barbulescu
ID: 37005474
Thank you for your support!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

862 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