Output selected columns

zhshqzyc
zhshqzyc used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:

@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

Lead SaaS Infrastructure Engineer
Commented:
Easy, we can use FOR to do this.  I made thsi command work for 7 columns but we can add or remove columns whatever you need.
::- 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
SET "TAB=	"
FOR /F "tokens=1,4,8,12,16,20,24 Delims=%TAB%" %%A IN (C:\Admin\TabFile.txt) DO ECHO "%%A, %%B, %%C, %%D, %%E, %%F, %%G."

Open in new window

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
dammit renge beet me to the bunch by mere minutes..
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Commented:
Got you ;-)

@zhshqzyc
Please equally split points!

Cheers,
Rene
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Thats mighty nice of your ReneGe, cheers!

Author

Commented:
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.

Commented:
I feel that it would require VBScript.

Have any ideas Q?

Commented:
@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.

Author

Commented:
No empty column, I already used c# to finish it.
If no other way, that' s okay. I will split points soon.

Commented:
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
Commented:
Here I've made a logic representation of what I feel is needed to be done in VBscript.

Let's see if someone is up to the task.

Cheers,
Rene

@ECHO OFF

SET File=a text file.txt
SET Num=1

CALL :Line "%Num%"
SET Num+=4
CALL :Line "%Num%"

:Home
SET /a Num+=4
CALL :Line "%Num%"
GOTO Home

:Line
FOR /F "usebackq tokens=%~1 delims=	" %%A IN ("%File%") DO (
	IF "%%A" == "" EXIT
	IF "%%A" == "CRLF" ECHO %CRLF%>>Output.txt 
	ECHO %%A>>Output.txt
)
EXIT /b
PAUSE

Open in new window

Commented:
Would you mind sharing your c# script?

Commented:
Glad I could help and thanks for the points.

Cheers,
Rene
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
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

Commented:
Nice script Q! Thanks for sharing ;-)
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Thanks ReneGe! =)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial