22
17

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.

awk内でシングルコーテーションを使いたい

Posted at

結論

\047で囲ってあげる

使用例

mysql> desc user_data;
+---------+-----------+------+-----+---------+----------------+
| Field   | Type      | Null | Key | Default | Extra          |
+---------+-----------+------+-----+---------+----------------+
| id      | int(11)   | NO   | PRI | NULL    | auto_increment |
| user_id | int(11)   | YES  |     | NULL    |                |
| data    | int(11)   | YES  |     | NULL    |                |
| detail  | char(255) | YES  |     | NULL    |                |
+---------+-----------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

$ cat sample.list
3 300
4 400

# awkの特性上、どうしてもシングルコーテーションで囲わないとだめ。
# で、MySQLのinsert文を吐き出す場合にダブルコーテーションで囲う必要があるため、insert文中に文字列を含める場合にシングルコーテーションが使えない時がある

$ awk '{printf("INSERT INTO user_data (user_id, data, detail) VALUES (%d,%d,\047テストデータ\047);\n"),$1,$2;}' sample.list > sample.sql

# テストデータという文字列がシングルコーテーションで囲われていることがわかる
$ cat sample.sql
INSERT INTO user_data (user_id, data, detail) VALUES (3,300,'テストデータ');
INSERT INTO user_data (user_id, data, detail) VALUES (4,400,'テストデータ');
22
17
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
22
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?