Hi guys, I am new here. I have done many scripts in VisualBasic and batch.
Now going to *inux and learning bash and perl, quite amazing commands I find in linux, like sed, grep, awk and so on...
the task is simple, find a string in folders and remove the line where that string exists.
I can find all lines with that string doing: grep -R "STRING" /home/comm/nagios/confs/*
witch outputs: filename.ext line
But now I actually need to remove these lines. I tried something like: sed '/^.* {/ {:a /}/ !{N;ba} }; s/.*STRING.*//' test.cfg
That just ouputs without the lines with string X but i need to write into that same file, and need to do in many files
thanks but that was not what i needed.
I need to remove an entire line where I find certain STRING.
But I shuold not have to specify the filename, just the folder. I have to do this
in multiple files.
The right way is to combine two tools you are already using and glue them together via shell. This is how most of this class of problems are solved in Unix. (Welcome to the fun side of the isle, btw. ;-)) )
Your first task is to find all the files containing a specific string, the second task is to feed the list of filenames found that way one by one to sed which deletes the respective lines.
First, how would sed do that with one file:
Not quite. The mechanism is correct (the lines containing "some_expression" would be deleted) but sed would put its results to <stdout> because habitually it does not change the input file. So we need to save the results to a file and then replace the original file with the result-file:
This does exactly what you want to do, it is the solution to the second task. Now we want this to work on one file after the other: You already noticed that using "grep" you can produce a list of filenames on which the mechanism above should work. Lets feed this list to the command sequence above. This is done with a "while"-loop. Since we nee only the filenames we trim the output of grep to display only the filenames by using the "-l" switch:
Consider the grep-command as a sort-of file. You can display its contents by simply execute the command. You can feed it to other commands by using the pipeline ("|") operator. The while-loop takes the content of this "file", one line by one, and assigns this to the variable "$filename" which is used throughout the loop.
So, that was it, yes? Hold on: the sed-expression doesn't even touch lines which do not contain expressions. What would happen, if the file consists only of such lines? Correct! Nothing. So we could skip the whole grep-part and directly modify all the files found:
This list is now longer but looks similar: filenames, one on a line each. But as files not containing "EXPRESSION" do pass through the mechanism unchanged this is as good as the first solution.
Still, as every file has to be copied it may or may not put considerably more load on the machine. That depends on the ratio of number of files to be changed to files to go unchanged, the number of files as a whole, etc. You will have to decide. You might decide to fine-tune the second program by using seds exit-codes to determine if it has changed anything at all and skip the "mv"-step accordingly. But this is another story....
Hello,
When my lines contain question mark, I use below command to delete the portion of the matching line coming after question mark:
sed 's/?.*//' SampleFile
SampleFile:
helloworldfirstline?mdksmyymsss
hellosecondlineworld?mdksmkkmsss
thirdhelloworld?mdksmccmsss
Output:... (2 Replies)
Hi Everyone,
I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file.
I am very new to unix. Kindly help... (10 Replies)
Hi ,
i have a file with data as below.This is same file. But actual file contains to many rows.
i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ?
Field 004... (7 Replies)
Hi,
How to delete the last line if is match the below string else no action...
String checking "END OF FILE. ROW COUNT: "
9f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|2
9f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|0
229f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|3
END OF... (2 Replies)
I have code that accepts input from a user, and when the user hits enter it is supposed to delete that whole line from the file.
echo "Which record? "
read record
sed '/$record/d' file
However, it does not delete it. Any help? (1 Reply)
I am using a while loop to read a file and i have to delete a string from the last line. Here is the code so far:
1. Read line by line from file new.html using while loop and store in a variable data
2.Now, need to replace '||chr( from last line in data
I am able to replace that character from... (12 Replies)
let me start out by saying i have ZERO exp with any kind of scripting, so sorry if this is really basic stuff.....
For example, I need to have a script that will search a file and find this line in the file:
*.cat;dog;kennel;house;barn;horse;hay;coat hat
and remove the "coat" from the... (12 Replies)
Hi All,
I have a file from Mainframe which has one of the lines with so many words... i tried to fold, format to 80 chararcter.. stil did not work. So i have decided to search for a string in that line Ex.FLIGHT PLAN and once if it is found i want to delete the entire line.
Please help... (2 Replies)
hi
i have a file contains data
session terminated
Line timeout: 120
Change state START->SIGNON_REPLY, RC=0
Change state SIGNON_REPLY->SIGNON, RC=0
i need to remove "Change state" and write to the same file
please help
thanks
Satya (3 Replies)