0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

windows シェル芸 環境構築 WSLでAWKが使えるまで 

Last updated at Posted at 2024-08-22

前提条件

・WSLをwindowsにインストール済み
※確認
powershellにて、
wsl -l -v でバージョンや、選択状況を確認

PS C:\Users\natsume> wsl -l -v

結果 Ubuntu-22.04(バージョンは他でも大丈夫です。)になっていればOK。

PS C:\Users\natsume> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-22.04           Stopped         2
  Debian                 Stopped         2
  docker-desktop-data    Stopped         2

WSLでAWKのテスト

・1行目の(縦)合計を表示

root@JIM288:~/tossy_01# cat data_02.txt
23 55 3339 289 32 2 3 4 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5
8 8 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9 9 9 9 9
1 2 3 4 5 6 7 8 9 10 11 12 13
root@JIM288:~/tossy_01# cat data_02.txt  | awk '{sum += $1} END {print sum}'
46
root@JIM288:~/tossy_01#

●テキストファイルからディレクトリ作成

・ディレクトリ名をawkで作成

root@JIM288:~/tossy_01/24_0827_001# awk 'BEGIN{for(i=1;i<=10;i++)printf"sagyou_%02d\n",i}' > directory.txt

・テキストファイル(directory.txt)からディレクトリ作成

root@JIM288:~/tossy_01/24_0827_001# cat directory.txt | while read -r line; do mkdir ${line}; done
root@JIM288:~/tossy_01/24_0827_001# ls -lt
total 44
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_10
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_09
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_08
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_07
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_06
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_05
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_04
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_03
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_02
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_01
-rw-r--r-- 1 root root  100 Aug 27 14:29 directory.txt

・テキストファイル(directory.txt)からディレクトリ作成(末尾日付あり)

cat directory.txt | while read -r line; do mkdir "${line}_$(date +%Y%m%d)"; done

root@JIM288:~/tossy_01/24_0827_001# ls -lt
total 84
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_10_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_08_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_09_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_07_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_06_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_05_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_04_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_01_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_02_20240827
drwxr-xr-x 2 root root 4096 Aug 27 15:25 sagyou_03_20240827
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_10
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_09
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_08
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_07
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_06
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_05
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_04
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_03
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_02
drwxr-xr-x 2 root root 4096 Aug 27 14:34 sagyou_01
-rw-r--r-- 1 root root  100 Aug 27 14:29 directory.txt

・awk で指定したファイルの行数の値を出力
(NR 現在の行番号)

root@JIM288:~/tossy_01# cat aisatu.txt
1.いつもお世話になっております。
2.こんにちは。お世話になっております。
3.いつもご愛顧いただき、誠にありがとうございます。
4.平素よりお世話になっております。
5.お忙しい中、失礼いたします。
6.いつもお力添えいただき、ありがとうございます。
7.先日はご対応いただき、ありがとうございました。
8.連絡が遅くなりまして申し訳ございません。
9.こんにちは。いかがお過ごしでしょうか?
10.急なご連絡失礼いたします。
root@JIM288:~/tossy_01# awk 'NR == 3' aisatu.txt
3.いつもご愛顧いただき、誠にありがとうございます。
root@JIM288:~/tossy_01#
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?