+ -
当前位置:首页 → 问答吧 → 求助 shell删除文档里的指定行

求助 shell删除文档里的指定行

时间:2011-05-10

来源:互联网

不能用 awk sed 和grep等等工具
怎么删除 文档中的制定行
例如 test文档中有
1 Mary;xxxx;1377777777
2 Harry;xxxx;138888888
3 Barry;xxxx;139999999
根据关键字 比如Harry 删除文档 第二行
test文档变成
1 Mary;xxxx;1377777777
2 Barry;xxxx;139999999

作者: hanxu588   发布时间: 2011-05-10

无语的帖子:-L

作者: shplpy   发布时间: 2011-05-10

  1. $ grep -Fv Harry urfile
复制代码
  1. $ sed '/Harry/d' urfile
复制代码
  1. $ awk '!/Harry/' urfile
复制代码

作者: blackold   发布时间: 2011-05-10

回复 shplpy
我不会啊 不知道怎么写 只会用awk grep写
我知道这样很费劲 但是没办法 写不出来

作者: hanxu588   发布时间: 2011-05-10

回复 blackold

前提要求就是 不能用 grep awk sed 来写

作者: hanxu588   发布时间: 2011-05-10



QUOTE:
回复  shplpy
我不会啊 不知道怎么写 只会用awk grep写
我知道这样很费劲 但是没办法 写不出来
hanxu588 发表于 2011-05-10 09:08




    直接在文件中删除似乎不行,只能是把你想要的文本重新输出到新的文件

作者: shplpy   发布时间: 2011-05-10

回复 shplpy
我也是这么想的 就想 问问有没有更简单的方法
暂时 没写出来
初学shell 有很多不懂的 多谢了

作者: hanxu588   发布时间: 2011-05-10

楼主误解了shell脚本的含义吧

作者: shplpy   发布时间: 2011-05-10

回复 hanxu588


    我晕,

QUOTE:
不能用 awk sed 和grep等等工具



可以要用什么工具?

作者: blackold   发布时间: 2011-05-10

回复 blackold
就是最基本的shell语言 不能用任何tools
应为用awk sed grep很简单 所以......

作者: hanxu588   发布时间: 2011-05-10

回复 shplpy
没有啊 我只是在写一个 shell作业
对你们来说应该是 很简单的作业
教授有很多限制 所以 不是很好写

作者: hanxu588   发布时间: 2011-05-10

回复 hanxu588


    那就用纯shell实现。

作者: blackold   发布时间: 2011-05-10

回复 blackold
恩 你会吗 我想的很麻烦 所以 想写的简单的

作者: hanxu588   发布时间: 2011-05-10

本帖最后由 dzhxc 于 2011-05-10 09:36 编辑
  1. cat file4.txt |while read line ;do line=`echo $line|cut -d" " -f2|cut -d";" -f1`;[ "$line" != "Harry" ] && echo "$line" ; done
复制代码
另外,你给的例子里面有中文字符,我改成英文的了……
简单的方法你都不让用了,只能用麻烦的了

作者: dzhxc   发布时间: 2011-05-10

bash:
  1. while read line;do if [[ ! ($line =~ Harry ) ]];then echo "$line";fi;done <urfile
复制代码
如不支持 =~ 可用参数展开。

作者: blackold   发布时间: 2011-05-10

回复 dzhxc
cat 和 cut 可以用吗
你看我作业要求
Implement in shell a "telephone book" program, which works according to the description given below (lines starting with "--" are comments, user inputs are in bold face, programs responses are in lower case. In the actual execution, user commands can be in lower/upper case). Note: do not use tools such as sed or awk, only standard shell.

作者: hanxu588   发布时间: 2011-05-10

回复 blackold
bash 不可以
这个 试过了 不行

作者: hanxu588   发布时间: 2011-05-10

回复 hanxu588


    也不能用bash吗?

作者: blackold   发布时间: 2011-05-10

回复 blackold
Implement in shell a "telephone book" program, which works according to the description given below (lines starting with "--" are comments, user inputs are in bold face, programs responses are in lower case. In the actual execution, user commands can be in lower/upper case). Note: do not use tools such as sed or awk, only standard shell.
这是我的作业要求 应该不可以用bash

作者: hanxu588   发布时间: 2011-05-10

我晕,看不懂英文啊,翻译一下。

作者: blackold   发布时间: 2011-05-10

本帖最后由 dzhxc 于 2011-05-10 09:53 编辑

回复 hanxu588


    这作业里说的很明确啊,awk sed 不可以用,但是grep没有提到。可以用grep啊,还要求区分大小写。
   如果真的不让用grep,至少cat和cut这些都是所谓的 标准shell命令……
   要是cat和cut都不可以用,那你们老师可以去专门写个shell了

作者: dzhxc   发布时间: 2011-05-10

回复 blackold
就是写一个类似电话本的东西
可以添加  删除 用户 等等
我都写完了 就剩一个 删除 不知道怎么删除

#!/bin/sh
不能用 shell工具 例如sed awk 去删除保存在文档中的用户 只能用最基本的shell

作者: hanxu588   发布时间: 2011-05-10

回复 hanxu588


    用黑哥写的吧
   这里面真的是纯shell……

作者: dzhxc   发布时间: 2011-05-10

回复 hanxu588


    这个不懂了。

作者: blackold   发布时间: 2011-05-10

回复 dzhxc
没 你理解错了
是不能用shell 工具 例如awk 和 sed, 只能用最基本的shell来写

作者: hanxu588   发布时间: 2011-05-10

回复 dzhxc
cat 和 cut 倒是是shell的tools  还是包含在最基础的shell 命令那
我很 困惑
例如cp rm alias 都是shell 最基本的指令 awk sed 不是

作者: hanxu588   发布时间: 2011-05-10

回复 dzhxc


    你看不懂中文吗?人家要求只能用shell,不能用bash。

作者: blackold   发布时间: 2011-05-10

回复 blackold


    看懂了,这种要求简直是无理取闹啊

作者: dzhxc   发布时间: 2011-05-10

回复 blackold
这个无语了
老师一牛逼 出的题目 都不同凡响
郁闷

作者: hanxu588   发布时间: 2011-05-10

彻底崩溃

作者: blackold   发布时间: 2011-05-10