Need to Copy Columns in Tab Delim Text File

April 17, 2010 at 18:11:45
Specs: Windows 7
I have a tab delimited text file that I need to be able to copy specific columns from and then paste them into a new tab delimited text file in a certain order. For example:

If I have a tab delimited file with columns a b c d e f g h i j, I need to be able to created a new tab delimited file with columns b g h a d j.

It unfortunatley has to be tab delimited instead of csv because it is being uploaded to a website that only reads tab delimited. Thanks for the help!


See More: Need to Copy Columns in Tab Delim Text File

Report •


#1
April 17, 2010 at 22:29:03
I think there's a better way to do this, and i hope someone here shows me (other than using debug).
debug method (one):
@goto 10
e100 9 2b
rcx
2
ntab
w
q
:10
@echo off & setlocal
if exist tab goto 20
debug < %0.bat > nul
:20
::----- end of method one
method two:
@echo off & setlocal enabledelayedexpansion
if exist tab goto :ok
echo please type [TAB], [ENTER], [CTRL-Z]
for /f "tokens=* delims=" %%a in ('type con') do (
set xx=%%a
)
>tab echo %xx%.
:ok
for /f "tokens=* delims=" %%a in (tab) do set ff=%%a
set ff=%ff:~0,1%
:20
:: from here down same script
for /f "tokens=1,2,4,7,8.10" %%a in (file) do >> new echo %%b%ff%%%g%ff%%%h%ff%%%a%ff%%%c%ff%%%j
::-------- end
ps: if you have an absolutely known, invariable location of a tab in your inputfile, the tab can be gleaned from there without all the other bs listed above. f/e: if line two colum 8 is always a tab.

Report •

#2
April 17, 2010 at 23:20:18
I would have thought that just using the tabs in text would be fine(you just need to use pre tags for the site to actually output tabs).

The only problem I can see would be empty columns, which is always a problem with separated values in for loops.

for /f "usebackq tokens=1,2,4,7,8,10 delims=	" %%a in ("yourtsvfile") do (
    >> "newtsv.tsv" echo %%b	%%d	%%e	%%a	%%c	%%f
)


Report •

#3
April 18, 2010 at 03:40:41
Hi Judago,

I like the way you think.

What I did a while back to deal with empty columns in a csv was change ,, to , ,.

[comma][comma] to [comma][space][comma]


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
April 18, 2010 at 05:52:08
Hi M2,

That is probably the best way to go about it, of course it all goes pair shaped when the values can contain the delimiter if double quoted......

A really bad design, but think about a nested csv with quotes:

"first csv, colum1 of,, another csv", "second, nested csv,ha,", .....

Worse still if they can contain escaped quotes \".



Report •

#5
April 18, 2010 at 05:58:33
"pair shaped"

hmmm


=====================================
Helping others achieve escape felicity

M2


Report •

#6
April 18, 2010 at 06:10:20
Are you mocking my engrish?

I'm a notoriusly bad speeller, I can put too words togthar that sound ok(franettically), but firefox fixs it for me. Homafones relly screw me over because firefox doesn't give me a squiwggly line.

Edit: If I make a mistake I'm drunk, if not I have uncanny clarity for a drunk person.


Report •

#7
April 18, 2010 at 06:42:43
download gawk for windows (see my sig), and use this one liner

c:\test> gawk.exe -F"\t" "{print $2,$7,$8,$1,$4,$12}" OFS="\t" file > newcsv.csv


GNU win32 packages | Gawk


Report •

#8
April 18, 2010 at 10:58:57
ha! yeah, i am so used to having my tabs ripped up into 9 spaces that i forget things are more civilised now.

when it becomes as complicated as quotes, escaped quotes, and quotes inside of quotes, (ie: when things becom "pair shaped") i pitch out scripting and go with a language or a gnu solution as ghostdog hinted at.
you just waste too much time trying to sculpt parsing with batchscript - like trying to shave with an axe.


Report •

#9
April 18, 2010 at 18:37:19
if your csv is complicated, as in there are things like embedded tabs between quotes and all that jazz, its better to use a programming language that have modules/api for parsing csv correctly eg Perl/Python. don't use batch for that.! period.

GNU win32 packages | Gawk


Report •

#10
April 19, 2010 at 09:54:49
"ha! yeah, i am so used to having my tabs ripped up into 9 spaces that i forget things are more civilised now."

LOL

I disabled TABs in my editor many moons ago.


=====================================
Helping others achieve escape felicity

M2


Report •


Ask Question