为什么不会sed的删除线从文件? [英] Why wont sed remove line from file?

查看:151
本文介绍了为什么不会sed的删除线从文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件中删除特定的行,然后追加编辑行到文件中。我得到的最后一部分的权利,但我的sed命令来删除旧的线是行不通的。

  sed的'/ ^ $ userinput / D'FILE1.TXT> FILE2.TXT

这最终将所有的文件内容到新文件。相反,只是我想保持线。用户输入的是,我想删除由用户给定的线。这是我工作的一类项目BASH。我想完全delete一个它从文件中删除该行。

 的sed/ ^ $ {1} $ / D文件> otherfile

这增加了所有线路有用和无用

 的sed/ ^ $ 1 / D文件> otherfile

创建空文件

继承人是我与干活的声明。最后,把它粘贴到

 更新(){
阅读-p进入课程的课程编号,你想更新:updateInput
grep的$ updateInput my_course.txt> UPDATE.TXT
  排版-i状态
  排版级
  如果[$? -eq 0]
    然后
        阅读-p状态:状态
        而(真)
          做
                如果[$状态-eq 0]
                然后
                  打破
                ELIF [$状态-eq 1]
                然后
                  打破
                其他
                  printf上输入一个0或1 \\ n
                科幻
          DONE
        阅读-p级级
        而(真)
          做
                如果[$状态-eq 0]
                然后
                  打破
                ELIF [$状态-eq 1]
                然后
                  打破
                其他
                  printf上输入一个0或1 \\ n
                科幻
          DONE
        阅读-p级级
        而(真)
          做
            万一$级
                    一个)
                    打破
                    ;;
                  B)
                  打破
                    ;;
                  C)
                    打破
                    ;;
              *)
                    阅读-p输入要么是A,B,C级
                ;;
                ESAC
          DONE
          SED/ ^ $ updateinput / Dmy_course.txt> my_course1.txt
          awk的-F
            / ^ CSC / {printf的$ 1,$ 2,$ 3,> update1.txt}'UPDATE.TXT
          $ printf的地位,$级>> update1.txt
          猫my_course1.txt> my_course.txt
          猫update1.txt>> my_course.txt
          RM UPDATE.TXT
          RM update1.txt
    科幻
    }

我的道歉不早点张贴。曾经一次,我试图将其粘贴它告诉我有错误。

将两行文本文件中并调用上面的脚本这地位是1年级后为A,这是TE输出


  

CSC3320,系统级编程,3,1,A


这实质上是改变文件的脚本。与加入的grep在函数的开头。

 的grep $ updateInput my_course.txt> UPDATE.TXT(。这是在函数的开头其他的一切是在底部)
      SED/ ^ $ updateinput / Dmy_course.txt> my_course1.txt
      awk的-F
        / ^ CSC / {printf的$ 1,$ 2,$ 3,> update1.txt}'UPDATE.TXT
      $ printf的地位,$级>> update1.txt
      猫my_course1.txt> my_course.txt
      猫update1.txt>> my_course.txt
      RM UPDATE.TXT
      RM update1.txt


解决方案

在这种情况下,你需要做的,是因为 $ 指行定期EX pressions结束。

尝试:

 的sed/ ^ $ {} userinput / DFILE1.TXT>> FILE2.TXT

还有>方式> 将追加到文件

I'm trying to remove a specific line from a file and then append the edited line to the file. I get last part right but my sed command to remove the old line is not working.

sed '/^$userinput/d' file1.txt > file2.txt

this ends up adding all of the file contents into the new file. Instead of just the lines that I want to keep. User input is the line that I want to remove that is given by the user. This is for a BASH project I'm working on for class. I want to remove that line from the file completely by deleteing it.

sed "/^${1}$/d" file > otherfile  

This adds all lines wanted and unwanted

sed "/^$1/d" file > otherfile

Creates empty file

Heres the statement that I'm workin with. Finally get it paste in.

update(){
read -p "Enter the course number of the course you would like to update: " updateInput  
grep $updateInput my_course.txt>update.txt  
  typeset -i status  
  typeset grade  
  if [ $? -eq 0 ]  
    then  
        read -p "Status: " status  
        while(true)  
          do  
                if [ $status -eq 0 ]  
                then  
                  break  
                elif [ $status -eq 1 ]  
                then  
                  break  
                else  
                  printf "Enter either a 0 or 1\n"  
                fi  
          done  
        read -p "Grade: " grade  
        while(true)  
          do  
                if [ $status -eq 0 ]  
                then  
                  break  
                elif [ $status -eq 1 ]  
                then  
                  break  
                else  
                  printf "Enter either a 0 or 1\n"  
                fi  
          done  
        read -p "Grade: " grade  
        while(true)  
          do  
            case $grade in  
                    A)  
                    break  
                    ;;  
                  B)  
                  break  
                    ;;  
                  C)  
                    break  
                    ;;  
              *)  
                    read -p "Enter either an A, B, C: " grade  
                ;;  
                esac  
          done  
          sed "/^$updateinput/d" my_course.txt>my_course1.txt  
          awk -F, '  
            /^CSC/{printf$1","$2","$3"," > "update1.txt"}' update.txt  
          printf $status","$grade >> update1.txt  
          cat my_course1.txt > my_course.txt  
          cat update1.txt >> my_course.txt  
          rm update.txt  
          rm update1.txt  
    fi  
    }  

My apologizes for not posting it sooner. Ever time I tried pasting it in it told me there were errors.

After placing two lines of text in the file and calling the above script for which status was 1 and grade was A this was te output

CSC3320,SYSTEM-LEVEL PROGRAMMING,3,1,A

This is essentially the script that alters the files. With the addition of a grep at the beginning of the function.

      grep $updateInput my_course.txt>update.txt  (This is at the beginning of the function. Everything else was at the bottom. )


      sed "/^$updateinput/d" my_course.txt>my_course1.txt  
      awk -F, '  
        /^CSC/{printf$1","$2","$3"," > "update1.txt"}' update.txt  
      printf $status","$grade >> update1.txt  
      cat my_course1.txt > my_course.txt  
      cat update1.txt >> my_course.txt  
      rm update.txt  
      rm update1.txt  

解决方案

In this case you need do this because $ means end of line regular expressions.

Try:

sed "/^${userinput}/d" file1.txt >> file2.txt

Also >> will append to the file.

这篇关于为什么不会sed的删除线从文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆