当前位置: 首页 > news >正文

awk FS or -F 的使用

 

ls *.txt

得到如何结果:

Chapter_10.Programming_Problems_Using_File.7_pages.txt
Chapter_11.Array_Index,_Security,_and_Trends.6_pages.txt
Chapter_12.Version_Control.14_pages.txt
Chapter_13.Concept.11_pages.txt
Chapter_14.Recursive_C_Functions.15_pages.txt
Chapter_15.Integer_Partition.10_pages.txt
Chapter_16.Programming_Problems_Using_Recursion.18_pages.txt
Chapter_17.Programmer-Defined_Data_Types.26_pages.txt
Chapter_18.Programming_Problems_Using_Structure.16_pages.txt
Chapter_19.Linked_Lists.9_pages.txt
Chapter_1.Program_Execution.5_pages.txt
Chapter_20.Programming_Problems_Using_Linked_List.9_pages.txt
Chapter_21.Binary_Search_Trees.13_pages.txt
Chapter_22.Parallel_Programming_Using_Threads.21_pages.txt
Chapter_23.Unit_Test.22_pages.txt
Chapter_24.Find_the_Exit_of_a_Maze.9_pages.txt
Chapter_25.Sudoku.20_pages.txt
Chapter_26.Image_Processing.14_pages.txt
Chapter_27.Huffman_Compression.27_pages.txt
Chapter_2.Stack_Memory.16_pages.txt
Chapter_3.Prevent,_Detect,_and_Remove_Bugs.8_pages.txt
Chapter_4.Pointers.22_pages.txt
Chapter_5.Writing_and_Testing_Programs.15_pages.txt
Chapter_6.Strings.8_pages.txt
Chapter_7.Heap_Memory.11_pages.txt
Chapter_8.Programming_Problems_Using_Heap_Memory.13_pages.txt
Chapter_9.Reading_and_Writing_Files.9_pages.txt

 

问题是,如何sort,保证按chapter1,2,3这样来排序:

 

可以使用awk,把1,2,3,4...给单独提取出来,放在文档的头部,再使用sort -n排序:

如何提取出1,2,3,4呢?awk的FS提供了方法:

 

$ ls *.txt | awk -F"_\|." {print $2} 

如何这样,运行后,会发现结果不对。

看gemini的分析的:

 

The main issue with your command is how the shell and awk interpret special characters, specifically the pipe (|) and the backslash (\). Because you haven't quoted the field separator argument, the shell is likely trying to treat | as a pipe for the entire command rather than a character for awk to use.

Here is a breakdown of what’s going wrong and how to fix it.


1. Missing Quotes

In your original code, -F"_\|.", the shell sees the | and gets confused. To awk, the field separator -F should be wrapped in single quotes so the shell passes the string literally to the awk engine.

2. The Regex Logic

Inside awk, the -F flag defines a Regular Expression.

  • If you want to split by an underscore OR a literal dot, you need to escape the dot (because . in regex means "any character").

  • The pipe | is the "OR" operator in regex.

3. Escape Sequences

To represent a literal pipe or a literal dot in an awk regex string, you often need a double backslash because of how the string is parsed. However, using a bracket expression [_.] is much cleaner and more reliable.


The Corrected Solutions

Option A: The "Bracket" Approach (Recommended)

This is the cleanest way to say "split at either an underscore or a dot."

Bash
 
ls *.txt | awk -F '[_.]' '{print $2}'

Option B: The "Regex OR" Approach

If you prefer the "OR" logic, you must quote the separator and escape the dot.

Bash
 
ls *.txt | awk -F '_|\\.' '{print $2}'

Important Note on ls

While this fixes your awk syntax, parsing the output of ls is generally considered "fragile" in scripting because it can break if filenames contain spaces or newlines. If you are doing this inside a script, a for loop is usually safer:

Bash
 
for file in *.txt; doecho "$file" | awk -F '[_.]' '{print $2}'
done

Quick Tip: If your filename is data_v1.txt, $2 will give you v1. If your filename is report_2023_final.txt, $2 will give you 2023. Make sure the field index corresponds to the specific part of the name you're hunting for!

 

最终的版本:

$ ls *.txt | awk -F'[_.]' ' {print $2 " " $0}' | sort  -n

1 Chapter_1.Program_Execution.5_pages.txt
2 Chapter_2.Stack_Memory.16_pages.txt
3 Chapter_3.Prevent,_Detect,_and_Remove_Bugs.8_pages.txt
4 Chapter_4.Pointers.22_pages.txt
5 Chapter_5.Writing_and_Testing_Programs.15_pages.txt
6 Chapter_6.Strings.8_pages.txt
7 Chapter_7.Heap_Memory.11_pages.txt
8 Chapter_8.Programming_Problems_Using_Heap_Memory.13_pages.txt
9 Chapter_9.Reading_and_Writing_Files.9_pages.txt
10 Chapter_10.Programming_Problems_Using_File.7_pages.txt
11 Chapter_11.Array_Index,_Security,_and_Trends.6_pages.txt
12 Chapter_12.Version_Control.14_pages.txt
13 Chapter_13.Concept.11_pages.txt
14 Chapter_14.Recursive_C_Functions.15_pages.txt
15 Chapter_15.Integer_Partition.10_pages.txt
16 Chapter_16.Programming_Problems_Using_Recursion.18_pages.txt
17 Chapter_17.Programmer-Defined_Data_Types.26_pages.txt
18 Chapter_18.Programming_Problems_Using_Structure.16_pages.txt
19 Chapter_19.Linked_Lists.9_pages.txt
20 Chapter_20.Programming_Problems_Using_Linked_List.9_pages.txt
21 Chapter_21.Binary_Search_Trees.13_pages.txt
22 Chapter_22.Parallel_Programming_Using_Threads.21_pages.txt
23 Chapter_23.Unit_Test.22_pages.txt
24 Chapter_24.Find_the_Exit_of_a_Maze.9_pages.txt
25 Chapter_25.Sudoku.20_pages.txt
26 Chapter_26.Image_Processing.14_pages.txt
27 Chapter_27.Huffman_Compression.27_pages.txt

 

上面的命令再添加 awk '{print $2}'

$  ls *.txt | awk -F'[_.]' ' {print $2 " " $0}' | sort  -n | awk '{print $2}'

Chapter_1.Program_Execution.5_pages.txt
Chapter_2.Stack_Memory.16_pages.txt
Chapter_3.Prevent,_Detect,_and_Remove_Bugs.8_pages.txt
Chapter_4.Pointers.22_pages.txt
Chapter_5.Writing_and_Testing_Programs.15_pages.txt
Chapter_6.Strings.8_pages.txt
Chapter_7.Heap_Memory.11_pages.txt
Chapter_8.Programming_Problems_Using_Heap_Memory.13_pages.txt
Chapter_9.Reading_and_Writing_Files.9_pages.txt
Chapter_10.Programming_Problems_Using_File.7_pages.txt
Chapter_11.Array_Index,_Security,_and_Trends.6_pages.txt
Chapter_12.Version_Control.14_pages.txt
Chapter_13.Concept.11_pages.txt
Chapter_14.Recursive_C_Functions.15_pages.txt
Chapter_15.Integer_Partition.10_pages.txt
Chapter_16.Programming_Problems_Using_Recursion.18_pages.txt
Chapter_17.Programmer-Defined_Data_Types.26_pages.txt
Chapter_18.Programming_Problems_Using_Structure.16_pages.txt
Chapter_19.Linked_Lists.9_pages.txt
Chapter_20.Programming_Problems_Using_Linked_List.9_pages.txt
Chapter_21.Binary_Search_Trees.13_pages.txt
Chapter_22.Parallel_Programming_Using_Threads.21_pages.txt
Chapter_23.Unit_Test.22_pages.txt
Chapter_24.Find_the_Exit_of_a_Maze.9_pages.txt
Chapter_25.Sudoku.20_pages.txt
Chapter_26.Image_Processing.14_pages.txt
Chapter_27.Huffman_Compression.27_pages.txt

 

问题解决了

 

当然可以使用python或者perl等程序来处理,但是使用命令行工具,可以很快解决。

 

如何使用perl,更简单:

$ ls -1 *.txt  | perl -ne ' map { /_(\d\d?)\./; print "$1  $_"; } $_; ' | sort -n

 

http://www.jsqmd.com/news/588814/

相关文章:

  • Lepton AI元数据管理:模型版本控制与服务追踪
  • 效果-AutoFill2 识别填充
  • Net Insight推出可编程视频制作网络解决方案
  • 5分钟掌握Speakeasy:Google Authenticator集成完整教程
  • WebGL最佳实践清单:遵循官方规范的10个关键要点
  • OmX错误处理指南:理解并解决AI助手的常见问题
  • C++ 学习计划
  • nas-tools与Emby/Plex无缝对接:构建家庭影院媒体中心的完美方案
  • Paper2Slides自定义样式:从学术风格到动漫主题的完整教程
  • 如何用 Splinter 在 5 分钟内完成第一个 Web 自动化测试
  • UniApp开发者薪资行情分析:掌握跨平台开发技能的市场价值究竟有多高?
  • 题解:[JOI Final 2026] 稻草人 2 / Scarecrows 2
  • 深入Angular Spotify架构:Nx Workspace最佳实践解析
  • 破解8大效率陷阱:设计师必备的自动化工具系统
  • OpenClaw 报错大全:2026 年我踩过的 12 个坑 + 完整解决方案
  • 论文写作的几条常识
  • Thrust事件处理机制:全面解析窗口、键盘和鼠标事件响应
  • 汉中旧房改造全攻略:为什么选择本地靠谱品牌?——汉府人家装饰老房翻新实战指南 - 一个呆呆
  • SAP借助“网络安全维基百科“平台破解威胁数据难题
  • ThorUI-uniapp插件生态解析:如何扩展你的开发能力
  • 解锁游戏新境界:Sunshine自托管串流服务器完全指南
  • GoHTTPServer 性能优化秘籍:提升文件传输速度的10个方法
  • Kandinsky-5.0-I2V-Lite-5s教学视频:B站UP主用它批量生成知识类动态图解
  • OpenClaw如何做好记忆持久化的 · 四、设计哲学:三个核心架构决策
  • AI Agent开发快速入门:awesome-ai-resources中的智能代理学习资源
  • Cortex源码解析:深入理解C++ AI服务器的实现原理
  • 【LeetCode刷题日记】:反转链表(面试基础考察)
  • 突破网盘下载限制:多平台直链解析工具的技术实现与效率优化指南
  • 如何用Charticulator快速创建专业级定制图表:5个简单技巧让你成为数据可视化高手
  • 基于PLC的门禁系统自动电气控制设计:“详解带图解的梯形图、接线图与原理图IO分配及组态画面