Remove text from a file
How can I remove text from a file from a script?
Here is what I'm trying to do. Using awk I locate the lines in the text file I want to remove, but I don't know how to actually remove it? For Example: This a very short version of the actual file. Let's say the file is called test.txt and I want to pull out [fskioski] and the ports assigned ports=252-253. How do I update the file without these 2 lines from within my script? [bptruck2] ports=246-247 [bpretail1] ports=248-249 [bpretail2] ports=250-251 [fskiosk1] ports=252-253 [fstruck1] ports=254-255 [fstruck2] ports=256-257 Thanks, |
SED is what you want for this.....
cat filename|sed '/string/{N;d;}' >newfilename This reads "filename" and looks for "string"--when found, it deletes that line + the next and writes to "newfilename" I just learned something new!!! The "N" command brings another line into the pattern space, and then "d" deletes the combo---neat!! |
Super
Hey that is great. However, when I run it, I get 2 blank lines instead of 1 between where the lines were deleted and the next one is. Is there a way to delete one more line?
thanks, |
These two produce the same output, the second saving the invocation of one cat process, but it's probably more important to use whichever is easier to understand than worrk about an extra cat (so choose according to your aesthetic preference):
Code:
cat filename|sed '/string/{N;d;}' >newfilename Code:
sed --in-place=.original '/^\[fskiosk1\]/,/^\[/ d' test.txt |
Quote:
deletes a total of 3 lines including the one with the pattern match cat filename|sed /^$/d >newfilename deletes all blank lines To combine them: cat filename| sed -e /string/{N;d;} -e /^$/d >newfilename deletes two lines beginning with the match, and THEN deletes all blank lines |
You can use a range for the pattern match:
sed -i '/fstruck1/,/^$/d' test.txt This can be useful to extract a section from a command output such as lspci: lspci -v | sed -n '/Broadcom/,^$/p' |
Getting there
Ok. I need one more thing. Can I substitute a variable in the string? If so how. I'm running a if then statement for a variable "user". It doesn't seem to work when I put in $user.
Thanks |
use "" instead of '' you'll probably need to escape any $'s that are part of your sed command.
|
You should read up on the quoting rules in the manual page for your shell. It might seem like an abstract problem right now, but it will save you many hours of confusion in the long run.
|
Ok, as you can already tell.....
Quote:
As you can already tell, I'm not a programmer. I'm not sure what that means ...need to escape any $'s that are part of you sed command. It sounds like you are saying it is possible to use a variable? Thanks, |
Quote:
Code:
Code:
sed -i "/$USER/,/^\$/d" test.txt http://www.linuxcommand.org/writing_shell_scripts.php http://rute.2038bug.com/index.html.gz are some good sources |
Quote:
|
Hi,
How i can remove below line from file(configure) using sed? Code:
{ (exit 1); exit 1; }; TIA PS: it solved, Code:
sed -e 's/{ (exit 1); exit 1; };//g' test.txt > result.txt |
configure files are generated by autoconf - modifying them is usually asking for a world of pain.
|
one more,
Code:
awk '{ if( match($0, "fskiosk1") ) { getline; getline } else { print } }' filename |
All times are GMT -5. The time now is 10:40 PM. |