> For the complete documentation index, see [llms.txt](https://huataihuang.gitbook.io/cloud-atlas-draft/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://huataihuang.gitbook.io/cloud-atlas-draft/develop/shell/sed_awk/remove_header_and_footer.md).

# 删除文件的首行和尾行

需要移除文件的头尾行，最简单的方法是使用 `sed` 行编辑命令：

* `1d` 删除第一行; `2d` 删除第二行；依次类推
* `$d` 删除最后一行

举例:

```bash
#To remove the first record in the original file
sed -i '1d' FF_EMP.txt

#To create a new file with header removed
sed '1d' FF_EMP.txt > FF_EMP_NEW.txt 
```

```bash
#To remove the last record in the original file
sed -i '$d' FF_EMP.txt


#To create a new file with trailer removed
sed '$d' FF_EMP.txt > FF_EMP_NEW.txt 
```

* 可以同时结合起来删除头尾行

```bash
#To remove the header & footer
sed -i '1d;$d' FF_EMP.txt


#To create a new file with header & trailer removed
sed '1d;$d' FF_EMP.txt > FF_EMP_NEW.txt 
```

## 参考

* [How to remove header and footer records of a flat file using UNIX shell script?](https://exploreinformatica.com/how-to-remove-header-and-footer-records-of-a-flat-file-using-unix-shell-script/)


---

# 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:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/shell/sed_awk/remove_header_and_footer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
