Shell:截取带空格的列【对MySQL结果集的处理】
(本文所讨论的问题,是昨天晚上很困惑我的问题)
通常,在Linux中,我们是通过空格或者特定字符作为标识符,使用【sed / awk / cut】来做列操作的;如下:
但有时候,如果是对一段带格式的文本做操作的时候,就会比较麻烦。
之前,在做Oracle的相关操作的时候,就困惑过,后来[……]
Adamhuan's Data Center - 【逻辑驱动数据】
数据玩物、代码屋、1/0游戏:(零和博弈)/ 禅宗意志 / 规则战争 / 解放数据力量 / 技术的飞速发展并没有改变这个世界,因为,这个世界从没有变,它只是越来越趋近于它本来的模样。
(本文所讨论的问题,是昨天晚上很困惑我的问题)
通常,在Linux中,我们是通过空格或者特定字符作为标识符,使用【sed / awk / cut】来做列操作的;如下:
但有时候,如果是对一段带格式的文本做操作的时候,就会比较麻烦。
之前,在做Oracle的相关操作的时候,就困惑过,后来[……]
在Linux中,我们通常会用以下的方法去重建一批文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
[root@localhost script]# pwd /script [root@localhost script]# [root@localhost script]# ls -ltr total 0 [root@localhost script]# [root@localhost script]# mkdir -p the_world/{human/{iron_man},angel,god/{thorns,odin}} [root@localhost script]# [root@localhost script]# tree ../script/ ../script/ └── the_world ├── angel ├── god │ ├── odin │ └── thorns └── human └── {iron_man} 7 directories, 0 files [root@localhost script]# [root@localhost script]# mkdir -p numbers/{1,2,3,4,5,6} [root@localhost script]# [root@localhost script]# tree ../script/ ../script/ ├── numbers │ ├── 1 │ ├── 2 │ ├── 3 │ ├── 4 │ ├── 5 │ └── 6 └── the_world ├── angel ├── god │ ├── odin │ └── thorns └── human └── {iron_man} 14 directories, 0 files [root@localhost script]# |
假设场景:
1. 我需要创建文件名连续的一组文件
2. 但是因为数量过大,我不能够一一列出来
比较正常的思路是通过【for】循环来实现。
这种方式是一定可以[……]
其实,本文是一个TIPS。
当SHELL通过【$1 / $2 / $3 …】这种方式传递参数的时候你可能会遇到这样的问题:
当参数个数达到两位数,比方说:【$10】
这时候,你直接使用:
1 |
variable1=$10 |
你是拿不到第十个参数[……]
直接看脚本吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# convert data # 说明: # 使用该脚本前,你准备的数据是通过UltraEdit的列编辑模式编辑的Excel的数据 # variable # 要操作的目标 data_target=$1 # function display_mission(){ echo "转换任务详情" echo "=====================" echo "操作的对象:$data_target" # 本脚本的场景一般都是文本文件 echo "-------" echo "对象类型" file $data_target echo "-------" echo "对象大小" du -sh $data_target echo "-------" echo "对象行数" cat $data_target | wc -l # end echo "" } # do # display # 显示当前操作详情 display_mission # 每行末尾增加符号【"】 echo "每行尾部追加符号:[\"]" sed -i 's/$/\"/g' $data_target # 去掉所有空格 echo "去掉所有空格" sed -i 's/ //g' $data_target # 增加分割双引号的空格 echo "增加区别调用脚本的参数的双引号的空格" sed -i 's/\"\"/\" \"/g' $data_target # 增加sh之行时候的空格以及第一个参数的空格 # 在这个场景中:我的引用脚本的文件临时命名为:1.sh echo "增加sh之行时候的空格以及第一个参数的空格" sed -i 's/sh1.sh\"/sh 1.sh \"/g' $data_target # 处理类似【" ""】这种格式的问题 echo "处理类似【\" \"\"】这种格式的问题" sed -i '/" ""/s/" ""/" " "/g' $data_target # done |
原Excel表,可能是类似这样的:
其中,我关心的是:
1. 客户名
2. 邮箱
3. 电话
4. 公司
然后,我通过UltraEdit,列编辑的可能是这样的:
[crayon-6045[……]
场景:
在MySQL中,有两张表:
1. 用户表person
2. 组织表company
用户表中有一个到组织表的外键:id_company
当前,组织表中已经有数据。
我希望在用户表录入数据的时候,根据组织名自动的获取组织的id然后插入用户表。
为了实现这个需求,通过SH[……]