Avatar of zhshqzyc
zhshqzyc

asked on 

Output selected columns

Hi, I have a text file which have many columns.
I want to extract column1 which is ID
Then column 4,8,12,16,20...column number can be devided by 4.
The delimiter is a tab.

Thanks for dos or awk command.
Microsoft DOSShell Scripting

Avatar of undefined
Last Comment
Ben Personick (Previously QCubed)
Avatar of ReneGe
ReneGe
Flag of Canada image


@ECHO OFF

FOR /F "usebackq tokens=1,4,8,12,16,20 delims=	" %%A IN ("a text file.txt") DO (
	ECHO %%A,%%B,%%C,%%D,%%E,%%F>>Output.txt
	ECHO %%A,%%B,%%C,%%D,%%E,%%F
)

PAUSE

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
dammit renge beet me to the bunch by mere minutes..
Avatar of ReneGe
ReneGe
Flag of Canada image

Got you ;-)

@zhshqzyc
Please equally split points!

Cheers,
Rene
Thats mighty nice of your ReneGe, cheers!
Avatar of zhshqzyc
zhshqzyc

ASKER

Well, I am glad to split the points to everybody.

But the question is that we don't know there are how many columns.
I mean that it may have coulmn 4,8,12,16,20,....80.
It also could have 4,8,....200.
It also could have 4,8,12,.....4000

If it is too hard to you, that is okay. Just let me know please.
Avatar of ReneGe
ReneGe
Flag of Canada image

I feel that it would require VBScript.

Have any ideas Q?
Avatar of ReneGe
ReneGe
Flag of Canada image

@zhshqzyc

Would it be possible that there could be data in a following empty column?

For example, if "12" is empty, we know for sure that there are no further column.
Avatar of zhshqzyc
zhshqzyc

ASKER

No empty column, I already used c# to finish it.
If no other way, that' s okay. I will split points soon.
Avatar of ReneGe
ReneGe
Flag of Canada image

I asked because, I know that it can't be done with a batch file but it most likely can be done with VBScript.

I don't know VBScript, so I was thinking doing a batch file that would do what is required, despite of it's limmitations and asking Q to make an equivalent in VBscript.

Are your happy with your C version?

Cheers,
Rene
SOLUTION
Avatar of ReneGe
ReneGe
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of ReneGe
ReneGe
Flag of Canada image

Would you mind sharing your c# script?
Avatar of ReneGe
ReneGe
Flag of Canada image

Glad I could help and thanks for the points.

Cheers,
Rene
Just an FYI, I knew I had code that would better handle the unknown number of tabs somewhere, and I found it, so I just anted to give the author this (UNTESTED) update.
::- Script Name: TabColumnOutput
::- Finds only the selected columns (The first, plus those divisable by four) from a tab deliniated file and outputs them to the command prompt
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "SrcFile=C:\Admin\TabFile.txt"
SET "TAB=	"

FOR /F "usebackq tokens=*" %%A IN ("%SrcFile%") DO (
	SET "RawData=%%~A"
	SET "Count=0"
	FOR %%B IN ("!RawData:%TAB%=","!") DO (
		SET /A "Count=!Count!+1"
		SET /A "Div=!Count! %% 4"
		IF !Count!==1 ECHO %%~B
		IF !Div!==0 ECHO , %%~B
	)
)
ENDLOCAL

Open in new window

Avatar of ReneGe
ReneGe
Flag of Canada image

Nice script Q! Thanks for sharing ;-)
Thanks ReneGe! =)
Microsoft DOS
Microsoft DOS

Microsoft Disk Operating System (MS-DOS) was an operating system for x86-based personal computers, and traces of it are still found in the Windows operating system. DOS is still used in some embedded systems and for certain legacy 16-bit networks.

14K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo