> For the complete documentation index, see [llms.txt](https://applezulab.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://applezulab.netdpi.net/02-linux-xi-tong-cao-zuo/replace-using-awk-sed.md).

# 字串取代，使用 awk & sed

**AWK**

有些實驗數據僅需要某幾個欄位值，用 awk 可以簡單達到目的假設 Shell 環境中的字串 str 內容為1 2 3 4 5 6 7 8 9 10，要取出其中幾個數值，如 2 4 6 8 10

```bash
str="1 2 3 4 5 6 7 8 9 10"
echo ${str} | awk ' { print $2 $4 $6 $8 $10 }; '
```

若是檔案 (file) 中有好幾行的字串，分別將每行的其中幾個數值取出，如取出第 2 4 6 8 10 個數值：

```bash
cat file | awk ' { print $2 $4 $6 $8 $10 }; '
```

或是要調換 1 與 2

```bash
str="1 2 3 4 5 6 7 8 9 10"
echo ${str} | awk ' { print $2 $1 $3 $4 $5 $6 $7 $8 $9 $10 };'
```

**Sed**

因為 tr 似乎只能作一對一的字元轉換，所以找到 sed 的方法來轉換一對多的字串。

將一個 "M" 取代成 "000K"

```bash
echo 1M | sed 's/M/000K/'
```

將字串中的每一個 "M" 取代成 "000K"

```bash
echo '1M and 2M' | sed 's/M/000K/g'
```

將檔案 (input.txt) 中 'old' 字串取代成 'new' 字串

```bash
sed -i -e "s/old/new/g" input.txt
```

Reference:

1. Dale Dougherty & Arnold Robbins, sed & awk, Second Edition, O'Reilly, March 1997.
2. Vivek Gite, Bash Shell: Replace a string with another string in all files using sed and perl -pie,[http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/](https://web.archive.org/web/20200416073026/http://www.google.com/url?q=http%3A%2F%2Fwww.cyberciti.biz%2Ffaq%2Funix-linux-replace-string-words-in-many-files%2F\&sa=D\&sntz=1\&usg=AFrqEzf4y6mffOAjpyk1Wr5g2SIwMHPEqA), 2007.
3. monsanbu, [http://www.unix.com/unix-dummies-questions-answers/21866-replace-character-tr.html](https://web.archive.org/web/20200416073026/http://www.google.com/url?q=http%3A%2F%2Fwww.unix.com%2Funix-dummies-questions-answers%2F21866-replace-character-tr.html\&sa=D\&sntz=1\&usg=AFrqEzd-GlyIsusVF4b2zmBuIHB0Y99RJQ), 2005.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://applezulab.netdpi.net/02-linux-xi-tong-cao-zuo/replace-using-awk-sed.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
