0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShellでCSV(TSV)ファイルの列を削除する

Posted at

タブ区切りファイルの列を削除します。
Splitしているだけなのでカンマ区切りファイルにも対応できます。

# 対象のファイル
get-content("test.tsv")
a	b	c
a1	b1	c1
a2	b2	c2
a3	b3	c3

タブは[ `t ]で指定
ちなみに、CRLFは[ `r `n ]らしい

# タブで分割してみる
(get-content("test.tsv")).Split("`t")
a
b
c
a1
b1
c1
a2
b2
c2
a3
b3
c3
# 2列目(列b)を抜いてみる
for($i=0; $i -lt $csv.Length; $i++){$csv[$i].Split("`t")[0] + "`t" + $csv[$i].Split("`t")[2]}
a	c
a1	c1
a2	c2
a3	c3

# ファイルに出力してみる
for($i=0; $i -lt $csv.Length; $i++){add-content -path new.tsv -value ($csv[$i].Split("`t")[0] + "`t" + $csv[$i].Split("`t")[2])}

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?