ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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
sed '/string/{N;d}' filename > newfilename
If you don't want to re-direct into a new file and then rename it back to the original, you can use the -i (or --in-place) switch to sed will cause it to modify the files specified on the command line directly. You can also request that the original file is backed up with some suffix, provided as an argument to the -i/--in-place option. With the input you provided, this should fo what you want and backs up the old file to test.txt.original (works with GNU sed 4.1.5):
Code:
sed --in-place=.original '/^\[fskiosk1\]/,/^\[/ d' test.txt
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,
cat filename|sed '/string/{N;N;d;}' >newfilename
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
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.
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.
As for escaping, you can see some of the suggestions in this thread include sed expressions with $ in them. It signifies end of line in regular expressions. If you use those ones inside "", you have to make sure to put \$ instead of $, or else the shell will try to substitute a variable for that part:
Code:
sed -i "/$USER/,/^\$/d" test.txt
becomes
sed -i /jviola/,/^$/d
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
Code:
lines_to_delete=2
string=[fskiosk1]
awk -v s="$string" -v ltd=$lines_to_delete '
index($0,s) { ++n; next }
n && ++n > ltd { n = 0 }
n { next }
{print}' "$FILE"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.