How to strip text after pipe using dos batch

April 12, 2011 at 14:36:03
Specs: Windows Vista
Hi,

I would be very thankful if anyone can help me with the file below. I need a DOS batch script to convert file below to strip text after pipe including pipe and up to next field separator ~ to give an output file as shown. This will help me complete a horrendous task. Many thanks

input file
001~Y~20~9999~TEXT FIELD ONE| AN1 ~001.19~1~002
001~Y~20~8888~TEXT FIELD TWO| BN2 ~017.40~1~003
001~Y~20~7777~TEXT FIELD THREE| CN3 ~012.34~1~002

output file
001~Y~20~9999~TEXT FIELD ONE~001.19~1~002
001~Y~20~8888~TEXT FIELD TWO~017.40~1~003
001~Y~20~7777~TEXT FIELD THREE~012.34~1~002


See More: How to strip text after pipe using dos batch

Report •

#1
April 13, 2011 at 02:17:04
:: ==========================================
::
:: dosmann.bat Wed 13-04-2011 16:06:32.75
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "tokens=1-5 delims=| " %%a in (input) do (
echo.%%a %%b %%c%%e
) >> newfile


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
April 13, 2011 at 14:10:03
Do you really mean DOS or just some "DOS-shell" under e.g. Windows Vista?

If it's really DOS, e.g. MS-DOS 6.22 or the like, then you could try the utility FAM. You find this utility as part of the package DOSUTILS.ZIP which in-turn you will find here: http://www.bttr-software.de/product...


Report •

#3
April 14, 2011 at 09:46:26
Its DOS shell within Vista and XP,
The reply above works fine for the example file but when I run it with the actutal data file I am getting problems

input file
1~1~U~7348~LOOSE MACKEREL FILLETS KG| 1KGM ~010.49~1~002
1~1~U~7727~PIE MINCE BEEF AND ONION| 1PCE ~001.75~0~001
1~1~U~8682~JUMBO SAUSAGES| 1PCE ~001.05~0~001
1~1~U~8697~HOT BREADED CHICKEN FILLET | 1PCE ~002.99~0~001
1~1~U~7751~SOUTHERN FRIED DRUMSTICKS| 1PCE ~001.40~0~001


Output file
1~1~U~7348~LOOSE MACKEREL FILLETS1KGM
1~1~U~7727~PIE MINCE BEEFONION
1~1~U~8682~JUMBO SAUSAGES 1PCE
1~1~U~8697~HOT BREADED CHICKEN1PCE
1~1~U~7751~SOUTHERN FRIED DRUMSTICKS~001.40~0~001

missing lots of data. Is number 1 is being read as a pipe?
thanks!


Report •

Related Solutions

#4
April 14, 2011 at 09:47:43
Thanks for reply
works fine for the example file but when I run it with the actutal data file I am getting problems, example below

input file
1~1~U~7348~LOOSE MACKEREL FILLETS KG| 1KGM ~010.49~1~002
1~1~U~7727~PIE MINCE BEEF AND ONION| 1PCE ~001.75~0~001
1~1~U~8682~JUMBO SAUSAGES| 1PCE ~001.05~0~001
1~1~U~8697~HOT BREADED CHICKEN FILLET | 1PCE ~002.99~0~001
1~1~U~7751~SOUTHERN FRIED DRUMSTICKS| 1PCE ~001.40~0~001


Output file
1~1~U~7348~LOOSE MACKEREL FILLETS1KGM
1~1~U~7727~PIE MINCE BEEFONION
1~1~U~8682~JUMBO SAUSAGES 1PCE
1~1~U~8697~HOT BREADED CHICKEN1PCE
1~1~U~7751~SOUTHERN FRIED DRUMSTICKS~001.40~0~001

missing lots of data. Is number 1 is being read as a pipe?
thanks!


Report •

#5
April 14, 2011 at 14:46:38
@echo off & setlocal EnableDelayedExpansion > FileOut.txt
for /F "tokens=1,* delims=|" %%A in (fileIn.txt) do (
   set row=%%B
  set row=!row:*~=!
  echo.%%A~!row!>> FileOut.txt
)


Report •

#6
April 15, 2011 at 13:03:21
Thanks a lot IVO, works perfectly

Report •

Ask Question