General :: Using Grep To Remove Line And Write Back To Same File?
Feb 10, 2010
remove a line starting with specific word with grep. Here is what I found
grep -v '^cc$' data.txt
Here I remove all lines with on 'cc' in that line. But I want the result write back to data.txt
I try several ways
grep -v '^cc$' data.txt > output.txt # works but to another file
echo `grep -v '^cc$' data.txt` > data.txt # didn't work, all carets gone, become one line
grep -v '^cc$' data.txt > data.txt # data.txt is empty after running this
How can I save the result of grep to the input file?
When I ls -l /etc/passwd, -rw-r--r-- 1 root root /etc/passwd When I login as myself, and rm /etc/passwd, it asks: rm: remove write-protected file '/etc/passwd'? If I say yes, will it actually delete the passwd file?
I have a dataset (see example below) that I would like to go through and copy all lines containing a certain string ("LGIG") plus the line immediately following that line to a new file. I have no problem grepping lines containing the string LGIG but I'm lost how to translate that to line number and shift up one line number for each instance of that string.
On one of my servers, it appears that a bunch of html files got the following code added to it...Quote:[URL]I was going to try to remove this line using grep & sed... as sample grep -lr -e 'apples' *.html | xargs sed -i 's/apples/oranges/g'I can get the grep portion to work...
Code: grep "<script src='http://b.rtbn2.cn/E/J.JS'[>][<]/script[>]" * But not the sed
How can I list the following with grep. I want to extract 2 lines fron a text file The fixed known part if it exists will static text and the text line after it will change.
A sample file . . textline1
[code]....
If the fixed part does Not exist how can I return error code 1
This has to also show the line count. I can get it to show the files but not the line count. What is the single command used to identify only the matching count of all lines within files under the /etc directory that contain the word „HOST? List only the files with matches and suppress any error messages.
I am trying to prepare my PowerEdge 2950 before CentOS 5.3 installation(web server). I have hardware RAID 1/10 so I will have 2 virtual disks(VD). First VDRAID 1) - 2 physical disks Second VDRAID 10) - 4 physical disks What should be write policy for the VD(RAID 1) and for the VD(RAID 10)? I would have "/var" and maybe "/tmp" on RAID 10.
someone once told me that use can pass a file to grep and use that to search the contents of another file. if that is the case I'm not entirely sure why the following isn't working for me.
How would i write a command that can find all the objects under the etc directory that have group write permission enabled and have not been accessed in the last X days. This is what i got from internet souce but i m not able to modify it according to my distribution. find /etc -perm -0070 -a -mtime +X ! -type l?print Here is the exact statement from link i m referring to.
Let me *try* and explain what I'm trying to do, and keep in mind aside from a little command line stuff I'm a beginner to any of what I'm asking about.
So that whatever was captured in the () in the first part of the statement would be used in the 1 in the back part of the statement for every n.chatlog that might be in any of the /webserver directories at that time.
I had a desktop pc running XP home, my daughter has an Acer Aspire 100 running a Linux OS which developed a problem so I attempted to create a recovery disk from the supplied DVD onto a memory stick via my desktop PC. It transpires that the recovery wasmade to my desktop pc in error and now my desktop does not boot up at all. Is there a way to remove this 'recovery' at all and revert back to XP??
I'm storing a list of strings in a file and would like to read the file and pipe each line returned to grep which in turn searches a directory for files containing the string.However this is not returning any output.
I ran into a bit of trouble making a bash script. (Desktop is a directory, and I try to get it's modification date)
Code:
lamp:~# cmd='ls -l Desktop | grep -o "....-..-.. ..:.."' lamp:~# $cmd ls: cannot access |: No such file or directory ls: cannot access grep: No such file or directory
[code]....
When I type in the command directly, without using an inbetween variable, it works fine.
I would like to write a newline delimeted rules file using PCREs for use with the grep command. Grep has the option -f to obtain the search pattern from a file, and option -P to search using PCREs. However, these two options do not work together. The -f option only seems to work with fixed string rules.A friend previously helped me get around this limitation somehow, but I can't remember how he did it. I also would like the ability to add comments at the end of each rule in the file.
I have a number of files:FooBlahhFooI only want to be able to grep for names in a file that contain Foo and not BlahhFoo. However I am not able to pull only those files away. How can this bee done. My grep/zgrep knowledge only goes this far at this point. I'm still learning but I'm stuck on how to make my arguments more precise zgrep 'Foo' SomeFileIMade.gz > /home/user/FOOFILE
I am trying to write a script that takes an input file ($FileName) and an intermediate file ($FileName.info) and removes lines from $FileName if the value in $2 of $FileName.info is <75.
I can't figure out how to feed only one line of the .info file to the if statement at a time so that it will perceive it as an integer instead of a list.
The error I am getting now is ./script.sh: line 6: [: : integer expression expected
I've written a script to parse a file and print each line that ends with matching pattern, if the next line is blank. The pattern lines are the result of md5sum $i|sed 's/path///g' so that only md5 and filename appear. Here's what I'm using.
Quote: for fline in `sed -n '/.*.ext$/p' file1` do if [ "`sed -n -e '/'"$fline"'/ {n; p;}' file1`" == "" ] then echo ""$fline" has no info" >>file2 fi done [Code]....