Linux下将数据文件的指定域读取到shell脚本中

运维 系统运维
这个例子说明了怎样在Linux下shell脚本中从数据文件读取特定的域(field)并进行操作。例如,假设文件employees.txt的格式是{employee-name}:{employee-id}:{department-name},以冒号进行划分,如下所示。

这个例子说明了怎样在Linux下shell脚本中从数据文件读取特定的域(field)并进行操作。例如,假设文件employees.txt的格式是{employee-name}:{employee-id}:{department-name},以冒号进行划分,如下所示。

$ cat employees.txt 
Emma Thomas:100:Marketing 
Alex Jason:200:Sales 
Madison Randy:300:Product Development 
Sanjay Gupta:400:Support 
Nisha Singh:500:Sales

下面的shell脚本说明了如何从这个employee.txt文件中读取特定的域(field)。

$ vi read-employees.sh 
#!/bin/bash 
IFS=: 
echo "Employee Names:" 
echo "---------------" 
while read name empid dept 
do 
    echo "$name is part of $dept department" 
done < ~/employees.txt

赋予脚本可执行权限后执行该脚本

$ chmod u+x read-employees.sh 
$ ./read-employees.sh 
Employee Names: 
--------------- 
Emma Thomas is part of Marketing department 
Alex Jason is part of Sales department 
Madison Randy is part of Product Development department 
Sanjay Gupta is part of Support department 
Nisha Singh is part of Sales department

 

【编辑推荐】

  1. Clojure世界:如何做性能测试
  2. Linux服务器的16个监控命令
  3. IE8安全新功能实测,到底给不给力?
责任编辑:赵宁宁
相关推荐

2021-04-21 08:03:34

脚本Shell读取

2021-08-20 10:46:25

Shell脚本文件Linux

2009-12-03 10:06:33

Ubuntushell脚本

2017-12-18 10:12:48

LinuxShell命令

2010-07-13 14:09:07

SQL Server数

2015-07-16 17:13:13

shell分页读取MySQL数据脚本

2022-08-30 08:52:04

shell脚本Linux

2011-08-03 11:11:01

本地组脚本

2024-02-19 16:15:07

2020-05-28 15:35:21

Linux数据文件图像

2010-03-23 16:35:31

shell 脚本编程

2013-08-07 10:04:37

MySQL数据恢复

2017-10-11 16:23:59

Linuxinode删除文件

2016-12-16 09:23:29

LinuxShell脚本

2018-11-12 14:35:47

BashhereLinux

2011-08-25 14:26:40

LUA数据文件

2010-04-30 16:01:17

2010-10-29 14:03:39

Oracle移动数据文

2017-08-25 17:41:17

Paradox数据文件格式

2010-10-13 14:02:01

MySQL数据文件
点赞
收藏

51CTO技术栈公众号