Skip to content
Advertisement

Tag: sed

How to check if sed has changed a file

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

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

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

Advertisement