I am trying to find a clever way to figure out if the file passed to sed has been altered successfully or not. Basically, I want to know if the file has been changed or not without having to look at the file modification date. The reason why I need this is because I need to do some extra stuff
Tag: sed
how to replace a block of text after a match in sed
In sed, I’d like to replace a multi-line block of text after a match, for example, after matching “foo”, supposing its line number is 0. I want to replace the text block from line -3 to line +5, i.e. the text bock between the third line proceeding the matching line and the fifth line after the matching line, by another
sed replace with hex
UTF-8 file test.txt: hex is sed s/A/B/g test.txt works sed s/x41/B/g test.txt does not work Some characters are unprintable so I must use their hex, A is just an example. Answer the shell preprocesses it, use single quotes.
How do I add a line of text to the middle of a file using bash?
I’m trying to add a line of text to the middle of a text file in a bash script. Specifically I’m trying add a nameserver to my /etc/resolv.conf file. As it stands, resolv.conf looks like this: My goal is to add nameserver 127.0.0.1 above all other nameserver lines, but below any text above that. In the end I want to
Sed – How to Print Regex Groups in Multi-Line?
Input file (test): Desired output: What I have done: Question: How do you print the regex groups in sed (multiline)? Answer If you use sed like this: then it will print in different lines: But is this what you are trying to print? Or you want to print text in hrefs? Update 1: To get hrefs between well formed <a
Delete the first five characters on any line of a text file in Linux with sed
I need a one-liner to remove the first five characters on any line of a text file. How can I do that with sed? Answer means replace (“s”, substitute) beginning-of-line then 5 characters (“.”) with nothing. There are more compact or flexible ways to write this using sed or cut.
Using sed to get an env var from /proc/*/environ weirdness with x00
I’m trying to grovel through some other processes environment to get a specific env var. So I’ve been trying a sed command like: sed -n “s/x00ENV_VAR_NAME=([^x00]*)x00/1/p” /proc/pid/environ But I’m getting as output the full environ file. If I replace the 1 with just a static string, I get that string plus the entire environ file: sed -n “s/x00ENV_VAR_NAME=([^x00]*)x00/BLAHBLAH/p” /proc/pid/environ I
Whats the difference between sed -E and sed -e
I’m working on some old code and I found that I used to use but I now try It seems to do the same thing, or does it? I kinda remember there being a reason I done it but I can’t remember and doing “man sed” doesn’t help as they don’t have anything about -E only -e that doesn’t make
Replacing String Columnwise Using Sed
Given this data I want to replace the string at 2nd column into “” if it is “qux”. Resulting: How do you do that with sed? In particular the data is very big with ~10^7 lines Answer I wouldn’t actually do it with sed since that’s not the best tool for the job. The awk tool is my tool of
How can I add a line to a file in a shell script?
I want to add a row of headers to an existing CSV file, editing in place. How can I do this? and I want to end up with Changing the initial CSV output is out of my hands. Any standard command will do. The important thing is the file is edited in place, and the line is inserted at the