linux 截取字符串 cut命令截取字符串
linux下scp远程拷贝包含空格的目录或者文件的解决方法
今天遇到个白痴问题,用了linux这么久了,竟然还出现如此低级的错误。
记录下,免得又忘记。
描述:
今天需要远程拷贝一些文件到电脑上,ubuntu的。远程电脑是debian的。
文件和目录包含大量的空格,采用scp拷贝,例:
scp-r root@192.168.0.51:/home/xxj/Documents/files/xx xx jj.tar.gz./
最开始就意识到了是空格问题,就采用了文件空格前加“/”,死活不行,
scp-r root@192.168.0.51:/home/xxj/Documents/files/xx/ xx/ jj.tar.gz./
然后又使用引号,单引号,双引号都用了,还是不行。
scp-r root@192.168.0.51:'/home/xxj/Documents/files/xx xx jj.tar.gz'./
scp-r root@192.168.0.51:"/home/xxj/Documents/files/xx xx jj.tar.gz"./
最后同时在空格前加斜杠,整个文件目录加引号才行
scp-r root@192.168.0.51:"/home/xxj/Documents/files/xx/ xx/ jj.tar.gz"./
linux下带空格文件批量cp和scp补充:
背景:主备机同步文件失败,现在需要人工手动的在备机上补齐文件,但是发现文件名竟然有空格
主机的IP:192.168.0.90主机文件清单名称:src_file.txt,备机文件清单名称:dst_file.txt,
从下面可以知道,主备机的文件路径和文件的名称是不一样的,同步的时候,从src_file.txt copy到dst_file.txt需要一一对应,另备机上的目录并不能保证都存在,所以需要先把备机上的目录结构先创建出来
[root@station90 file]# cat src_file.txt
/home/src/10329/g0197/k/19xiazai h264.3gp
/home/src/10327/g0194/10ng h264 xiazai.3gp
/home/src/10329/g0196/lh/10xiazai h263.3gp
/home/src/101117/060/090/1011.3gp
/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp
[root@station90 file]# cat dst_file.txt
/home/dst/1038/g07/k/3/10290upload h264.3gp
/home/dst/10327/g04/10ng h264 upload.3gp
/home/dst/1038/g06/lhppww/10upload h263.3gp
/home/dst/101117/06g090/1011.3gp
/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp
从dst_file.txt(备机文件)可以知道,目录的级别不是一样的,也就是说第一行为6级目录,第二行却为4级目录,如何取出目录结构,先创建出目录结构呢?执行如下命令即可:
[root@station90 file]# awk-F'/''{for(i=1;iNF;i++){printf$i"/"}printf"/n"}' dst_file.txt//注意iNF,即不需要循环到最后一列,printf$i是不打印回车的,一行循环完毕后,printf"/n"会打印回车,这样的结果刚好是我们需要的
/home/dst/1038/g07/k/3/
/home/dst/10327/g04/
/home/dst/1038/g06/lhppww/
/home/dst/101117/06g090/
/home/dst/1052g056/0590/33/
[root@station90 file]# awk'{print$1}' dst_file.txt| awk-F'/''{print substr($0,1,(length($0)-length($NF)))}'/tmp/dst_dir.txt
//根据dst_file.txt的特点,先执行awk'{print$1}' dst_file.txt以空格作为分隔符,取出第一列,这样取出的就没有空格,$0为文本里面包含的所有内容,再通过截取字符串的方式,substr($0(去除空格的文本本身),1(从第一个字符开始),(length($0)-length($NF))所有字符的长度减去最后一列的字符长度,就等于我们需要的字符长度,也就是从1,我们需要的字符长度,结果就是我们需要的了
[root@station90 file]# cat/tmp/dst_dir.txt
/home/dst/1038/g07/k/3/
/home/dst/10327/g04/
/home/dst/1038/g06/lhppww/
/home/dst/101117/06g090/
/home/dst/1052g056/0590/33/
[root@station90 file]# mkdir-p$(cat/tmp/dst_dir.txt)
[root@station90 file]# ls-ld$(cat/tmp/dst_dir.txt)/目录结构已经创建出来了
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/101117/06g090/
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/10327/g04/
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/1038/g06/lhppww/
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/1038/g07/k/3/
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/1052g056/0590/33/
part1:单纯的从本机copy到本机的其他目录,会比较简单,如下所示:
[root@station90 file]# sed-i-e"s/^/cp/"/g"-e"s/$//"/g" src_file.txt//在文本中的开头和结尾添加双引号
[root@station90 file]# cat src_file.txt
cp"/home/src/10329/g0197/k/19xiazai h264.3gp"
cp"/home/src/10327/g0194/10ng h264 xiazai.3gp"
cp"/home/src/10329/g0196/lh/10xiazai h263.3gp"
cp"/home/src/101117/060/090/1011.3gp"
cp"/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g" dst_file.txt//在文本中的开头和结尾添加双引号
[root@station90 file]# cat dst_file.txt
"/home/dst/1038/g07/k/3/10290upload h264.3gp"
"/home/dst/10327/g04/10ng h264 upload.3gp"
"/home/dst/1038/g06/lhppww/10upload h263.3gp"
"/home/dst/101117/06g090/1011.3gp"
"/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]#paste-d'' src_file.txt dst_file.txt cp.sh//src_file.txt和dst_file.txt文本以空格作为分隔符合成一个文件
[root@station90 file]#cat cp.sh
cp"/home/src/10329/g0197/k/19xiazai h264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
cp"/home/src/10327/g0194/10ng h264 xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
cp"/home/src/10329/g0196/lh/10xiazai h263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
cp"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
cp"/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i"1,1s/^//#/!//bin//bash/n/g" cp.sh//在脚本中添加#!/bin/bash
[root@station90 file]# cat cp.sh
#!/bin/bash
cp"/home/src/10329/g0197/k/19xiazai h264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
cp"/home/src/10327/g0194/10ng h264 xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
cp"/home/src/10329/g0196/lh/10xiazai h263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
cp"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
cp"/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# chmod+x cp.sh
[root@station90 file]# ls-l"/home/dst/1038/g07/k/3/10290upload h264.3gp"//执行cp.sh脚本前,文件是不存在的
ls:/home/dst/1038/g07/k/3/10290upload h264.3gp:没有那个文件或目录
[root@station90 file]#./cp.sh
[root@station90 file]# ls-l"/home/dst/1038/g07/k/3/10290upload h264.3gp"//ok,copy成功
-rw-r--r-- 1 root root 0 11-10 17:44/home/dst/1038/g07/k/3/10290upload h264.3gp
part2从远成主机scp文件过来,先举例一错误的例子,正确的总是慢慢的总结出来的
执行rm.sh,删除刚才copy过来的文件
[root@station90 file]# cat rm.sh
#!/bin/bash
rm-f"/home/dst/1038/g07/k/3/10290upload h264.3gp"
rm-f"/home/dst/10327/g04/10ng h264 upload.3gp"
rm-f"/home/dst/1038/g06/lhppww/10upload h263.3gp"
rm-f"/home/dst/101117/06g090/1011.3gp"
rm-f"/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]#./rm.sh
[root@station90 file]# sed-i-e"s/^/scp/ root/@192/.168/.0/.90/:/"/g"-e"s/$//"/g" src_file.txt
//在src_file.txt文件的开头和结尾添加双引号,同时添加root@192.168.0.90:,最终如下所示
[root@station90 file]# cat src_file.txt
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazaih264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ngh264 xiazai.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazaih263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_040_0_3.3gp"
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g" dst_file.txt//在文本中的开头和结尾添加双引号
[root@station90 file]# cat dst_file.txt
"/home/dst/1038/g07/k/3/10290upload h264.3gp"
"/home/dst/10327/g04/10ng h264 upload.3gp"
"/home/dst/1038/g06/lhppww/10upload h263.3gp"
"/home/dst/101117/06g090/1011.3gp"
"/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# paste-d'' src_file.txt dst_file.txt scp.sh//合成shell脚本步骤一
[root@station90 file]# cat scp.sh
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazaih264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ngh264 xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazaih263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_040_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i"1,1s/^//#/!//bin//bash/n/g" scp.sh//合成shell脚本步骤二
[root@station90 file]# cat scp.sh//看过去没有问题是吧,借鉴上面的part1部分cp得到scp嘛,其实是有问题的
#!/bin/bash
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazaih264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ngh264 xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazaih263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_040_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# chmod+x scp.sh
[root@station90 file]#./scp.sh//发现了吧,从远程主机找不到这些文件,只有没有空格的文件名称才可以复制过来
scp:/home/src/10329/g0197/k/19xiazai: No such file or directory
scp: h264.3gp: No such file or directory
scp:/home/src/10327/g0194/10ng: No such file or directory
scp: h264: No such file or directory
scp: xiazai.3gp: No such file or directory
scp:/home/src/10329/g0196/lh/10xiazai: No such file or directory
scp: h263.3gp: No such file or directory
1011.3gp 100% 0 0.0KB/s 00:00
scp:/home/src/10520/056/0590/99833/10x_04: No such file or directory
scp: 0_0_3.3gp: No such file or directory
继续错误的里程,想想,肯定是空格没有进行转义,遂src_file.txt文件和src_file.txt文件中的空格都进行转义,于是下面错误的过程来了:
[root@station90 file]# cat src_file.txt//原文件内容格式
/home/src/10329/g0197/k/19xiazai h264.3gp
/home/src/10327/g0194/10ng h264 xiazai.3gp
/home/src/10329/g0196/lh/10xiazai h263.3gp
/home/src/101117/060/090/1011.3gp
/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g" src_file.txt//文件的开头和结尾加上双引号
[root@station90 file]# cat src_file.txt
"/home/src/10329/g0197/k/19xiazai h264.3gp"
"/home/src/10327/g0194/10ng h264 xiazai.3gp"
"/home/src/10329/g0196/lh/10xiazai h263.3gp"
"/home/src/101117/060/090/1011.3gp"
"/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i"s//////g" src_file.txt//对空格进行转义
[root@station90 file]# cat src_file.txt
"/home/src/10329/g0197/k/19xiazai/ h264.3gp"
"/home/src/10327/g0194/10ng/ h264/ xiazai.3gp"
"/home/src/10329/g0196/lh/10xiazai/ h263.3gp"
"/home/src/101117/060/090/1011.3gp"
"/home/src/10520/056/0590/99833/10x_04/ 0_0_3.3gp"
[root@station90 file]# cat dst_file.txt///原文件内容格式
/home/dst/1038/g07/k/3/10290upload h264.3gp
/home/dst/10327/g04/10ng h264 upload.3gp
/home/dst/1038/g06/lhppww/10upload h263.3gp
/home/dst/101117/06g090/1011.3gp
/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g"-e"s//////g" dst_file.txt//在文件开头和结尾加双引号和空格进行转义
[root@station90 file]# cat dst_file.txt
"/home/dst/1038/g07/k/3/10290upload/ h264.3gp"
"/home/dst/10327/g04/10ng/ h264/ upload.3gp"
"/home/dst/1038/g06/lhppww/10upload/ h263.3gp"
"/home/dst/101117/06g090/1011.3gp"
"/home/dst/1052g056/0590/33/10x_04/ 0_0_3.3gp"
[root@station90 file]# sed-i"s/^/scp/ root/@192/.168/.0/.90/:/g" src_file.txt//对远程文件(192.168.0.90)src_file.txt加上scp root@192.168.0.90:
[root@station90 file]# cat src_file.txt
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazai/h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ng/h264/ xiazai.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazai/h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_04/0_0_3.3gp"
[root@station90 file]# sed-i"1,1s/^//#/!//bin//bash/n/g" scp.sh//对文件加上#!/bin/bash
[root@station90 file]# cat scp.sh
#!/bin/bash
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazai/h264.3gp""/home/dst/1038/g07/k/3/10290upload/ h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ng/h264/ xiazai.3gp""/home/dst/10327/g04/10ng/ h264/ upload.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazai/h263.3gp""/home/dst/1038/g06/lhppww/10upload/ h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_04/0_0_3.3gp""/home/dst/1052g056/0590/33/10x_04/ 0_0_3.3gp"
[root@station90 file]# chmod+x scp.sh
[root@station90 file]#./scp.sh//执行脚本,看过去是都成功了吧,但是……
19xiazai h264.3gp 100% 0 0.0KB/s 00:00
10ng h264 xiazai.3gp 100% 0 0.0KB/s 00:00
10xiazai h263.3gp 100% 0 0.0KB/s 00:00
1011.3gp 100% 0 0.0KB/s 00:00
10x_04 0_0_3.3gp 100% 0 0.0KB/s 00:00
[root@station90 file]#
[root@station90 file]# cd/home/dst/1038/g07/k/3/
[root@station90 3]# ls//看到没有,复制到本地的文件有了/
10290upload/ h264.3gp
[root@station90 dst]# pwd
/home/dst
[root@station90 dst]# find./-type f
./10327/g04/10ng/ h264/ upload.3gp
./1038/g07/k/3/10290upload/ h264.3gp
./1038/g06/lhppww/10upload/ h263.3gp
./1052g056/0590/33/10x_04/ 0_0_3.3gp
[root@station90 dst]# find./-type f-exec rm{}/;//删除复制过来错误的文件
[root@station90 dst]# find./-type f
[root@station90 file]# cat src_file.txt//下面的步骤是正确的,其实只需要对src_file.txt文件的空格加上/进行转义,对本地
dst_file.txt文件只需要在文件的开头结尾加上双引号,就好了
/home/src/10329/g0197/k/19xiazai h264.3gp
/home/src/10327/g0194/10ng h264 xiazai.3gp
/home/src/10329/g0196/lh/10xiazai h263.3gp
/home/src/101117/060/090/1011.3gp
/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp
[root@station90 file]# sed-i"s//////g" src_file.txt
[root@station90 file]# cat src_file.txt
/home/src/10329/g0197/k/19xiazai/ h264.3gp
/home/src/10327/g0194/10ng/ h264/ xiazai.3gp
/home/src/10329/g0196/lh/10xiazai/ h263.3gp
/home/src/101117/060/090/1011.3gp
/home/src/10520/056/0590/99833/10x_04/ 0_0_3.3gp
[root@station90 file]# sed-i-e"s/^/scp/ root/@192/.168/.0/.90/:/"/g"-e"s/$//"/g" src_file.txt
[root@station90 file]# cat src_file.txt
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazai/h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ng/h264/ xiazai.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazai/h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_04/0_0_3.3gp"
[root@station90 file]# cat dst_file.txt
/home/dst/1038/g07/k/3/10290upload h264.3gp
/home/dst/10327/g04/10ng h264 upload.3gp
/home/dst/1038/g06/lhppww/10upload h263.3gp
/home/dst/101117/06g090/1011.3gp
/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g" dst_file.txt
[root@station90 file]# cat dst_file.txt
"/home/dst/1038/g07/k/3/10290upload h264.3gp"
"/home/dst/10327/g04/10ng h264 upload.3gp"
"/home/dst/1038/g06/lhppww/10upload h263.3gp"
"/home/dst/101117/06g090/1011.3gp"
"/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i"1,1s/^//#/!//bin//bash/n/g" scp.sh
[root@station90 file]# cat scp.sh//最终的脚本样本
#!/bin/bash
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazai/h264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ng/h264/ xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazai/h263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_04/0_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# chmod+x scp.sh
[root@station90 file]#./scp.sh
19xiazai h264.3gp 100% 0 0.0KB/s 00:00
10ng h264 xiazai.3gp 100% 0 0.0KB/s 00:00
10xiazai h263.3gp 100% 0 0.0KB/s 00:00
1011.3gp 100% 0 0.0KB/s 00:00
10x_04 0_0_3.3gp 100% 0 0.0KB/s 00:00
[root@station90 file]# cd/home/dst
[root@station90 dst]# find./-type f//现在OK了,linux水平不行,被折磨了2天呐
./10327/g04/10ng h264 upload.3gp
./101117/06g090/1011.3gp
./1038/g07/k/3/10290upload h264.3gp
./1038/g06/lhppww/10upload h263.3gp
./1052g056/0590/33/10x_04 0_0_3.3gp
Linux的shell编程中,如何将一段字符串进行截取
简单介绍下Shell字符串截取的详细方法,如截取指定字数、按指定的字符串截取、按指定要求分割。
一、Linuxshell截取字符变量的前8位,有方法如下:二、按指定的字符串截取1、第一种方法:${varible##*string}从左向右截取最后一个string后的字符串${varible#*string}从左向右截取第一个string后的字符串${varible%%string*}从右向左截取最后一个string后的字符串${varible%string*}从右向左截取第一个string后的字符串“*”只是一个通配符可以不要三、按照指定要求分割:比如获取后缀名
linux查找文件字符串linux查找文件字符串
如何在Linux下查找文件内容包含某个特定字符串的文件?
概述
使用grep可以查找包含指定字符串的文件
步骤详解
格式:
grep“要查找的字符串”文件名
例子:
grep"192.168.0.1"/etc
文件名可以使用基本正则表达式(BRE),例如,查找test目录下的所有文件,是否包含www.dutycode.com字符串。
grep“www.dutycode.com”/root/zzh/test/*
小贴士:使用-n参数,可以显示字符串在文件中的行数
拓展内容
关于grep的命令的使用:
几个常用的查询指令:
1、查找时不区分字符串的大小写
grep-i“查找的字符串”文件名
2、查找时使用正则表达式,匹配符合的字符串
grep-e“正则表达式”文件名
3、查找不匹配指定字符串的行:
grep-v“被查找的字符串”文件名
4、查找时显示被查找字符串所在的行数
grep-n“查找的字符串”文件名
linuxC截取字符串?
简单介绍下Shell字符串截取的详细方法,如截取指定字数、按指定的字符串截取、按指定要求分割。
一、Linuxshell截取字符变量的前8位,有方法如下:
二、按指定的字符串截取1、第一种方法:${varible##*string}从左向右截取最后一个string后的字符串${varible#*string}从左向右截取第一个string后的字符串${varible%%string*}从右向左截取最后一个string后的字符串${varible%string*}从右向左截取第一个string后的字符串“*”只是一个通配符可以不要三、按照指定要求分割:比如获取后缀名
linux查找一个中文件是否有该字符串?
。含有某个字符串Linux查找文件内容的常用命令方法从文件内容查找匹配指定字符串的行$grep"被查找的字符串"文件名从文件内容查找与正则表达式匹配的行$grep_e“正则表达式”文件名查找时不区分大小写:$grep_i"被查找的字符串"文件名查找匹配的行数:$grep-c"被查找的字符串"文件名从文件内容查找不匹配指定字符串的行$grep_v"被查找的字符串"文件名从根目录开始查找所有扩展名为.log的文本文件,并找出包含”ERROR”的行find/-typef-name"*.log"|xargsgrep"ERROR"
Linux:用shell如何实现读取一个字符串的第n个字符呢?
$test='Ilovechina'$echo${test:5}echina$echo${test:5:10}echina${变量名:起始:长度}得到子字符串
在linux中查找字符串用什么命令?
Linux查找字符串用grep命令,可以查找文件,也可以在命令的结果中查找。如果是在文件中查找字符串,用法是:
grep用英文单引号括起来的字符串文件名
如果是在命令的显示结果中查找,需要用管道符将命令与grep连接起来,像这样:
last|grep'root'
(在last命令的显示结果中查找字符串root)