Skip to Content
PowerShellクックブック 第4版
book

PowerShellクックブック 第4版

by Lee Holmes
March 2025
Intermediate to advanced
1002 pages
14h 55m
Japanese
O'Reilly Media, Inc.
Content preview from PowerShellクックブック 第4版

第5章 文字列と非構造化テキスト

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

5.0 はじめに

を作成し、テキストを操作することは、長い間、スクリプト言語や従来のシェルの主要なタスクのひとつだった。実際、Perl(言語)はテキスト処理のために設計されたシンプルな(しかし便利な)ツールとして始まった。しかしその人気は、Perlがその必要性を満たしていることの強力な証拠となっている。

テキストベースのシェルでは、この強いフォーカスが続く。システムとのインタラクションのほとんどが、プログラムのテキストベースの出 力を操作することで行われる場合、強力なテキスト処理ユーティリティが重要になる。awksedgrep などのテキスト解析ツールは、テキストベースのシステム管理の要となる。

PowerShellのオブジェクトベース環境では、この伝統的なツールチェーンはそれほど重要な役割を果たさない。以前はこれらのツールが必要だったタスクのほとんどを、他のPowerShellコマンドを使ってはるかに効率的に達成できる。ただし、オブジェクトベースのシェルになったからといって、PowerShellがテキスト処理のサポートをすべてやめたわけではない。文字列や構造化されていないテキストを扱うことは、システム管理者にとって重要な役割を果たし続ける。PowerShellでは、(コマンドレットとオブジェクトを使用して)システムの大部分を完全に忠実に管理できるため、テキスト処理ツールは再び実際のテキスト処理タスクに集中することができる。

5.1 文字列を作成する

問題

テキストを保持する変数を作成したい。

解決策

PowerShellの文字列変数を、テキストを保存して扱う方法として使う。

変数展開やエスケープ文字に対応した文字列を定義するには、二重引用符で囲む:

$myString = "Hello World"

リテラル文字列(変数展開やエスケープ文字を解釈しない文字列)を定義するには、シングルクォートで囲む:

$myString = 'Hello World'

ディスカッション

文字列 リテラルには、リテラル文字列(非拡張文字列)と拡張文字列の2種類がある。リテラル文字列を作成するには、文字列をシングルクォート ($myString = 'Hello World') で囲む。展開文字列を作成するには、文字列を二重引用符 ($myString = "Hello World") で囲む。

リテラル文字列では、一重引用符で囲まれたすべてのテキストが文字列の一部になる。展開文字列では、PowerShellは変数名($replacementString など)とエスケープシーケンス(`n など)をその値(それぞれ$replacementString の内容や改行文字など)で展開する。

PowerShell文字列内のエスケープシーケンスと置換規則の詳細については、「文字列」を参照のこと。

リテラル文字列内のテキストはすべてリテラルである」という規則に対する例外として、引用符文字そのものがある。どちらのタイプの文字列でも、PowerShellではその文字列の引用文字を2つ一緒に配置することで、引用文字そのものを追加することができる:

$myString = "This string includes ""double quotes"" because it combined ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

生成AI時代の新プログラミング実践ガイド Pythonで学ぶGPTとCopilotの活用ベストプラクティス

生成AI時代の新プログラミング実践ガイド Pythonで学ぶGPTとCopilotの活用ベストプラクティス

松本 直樹
デジタルアイデンティティのすべて ―安全かつユーザー中心のアイデンティティシステムを実現するための知識

デジタルアイデンティティのすべて ―安全かつユーザー中心のアイデンティティシステムを実現するための知識

Phillip J. Windley, Drummond Reed, 富士榮 尚寛, 柴田 健久, 花井 杏夏, 宮崎 貴暉, 塚越 雄登, 田島 太朗, 名古屋 謙彦, 村尾 進一, 瀬在 翔太, 松本 優大, 安永 未来, 池谷 亮平
Pythonクイックリファレンス 第4版

Pythonクイックリファレンス 第4版

Alex Martelli, Anna Martelli Ravenscroft, Steve Holden, Paul McGuire, 鈴木 駿, 株式会社クイープ

Publisher Resources

ISBN: 9798341626737