29,004 questions
-1
votes
1
answer
92
views
Replace special characters in file
I want to change the \]\[ to , in the string below:
\[\["2026.04.05 17:52","Powerloss",24\]\]\[\["2026.04.05 17:52","Powerloss",24\]\]
How can I do that? I ...
Best practices
0
votes
6
replies
100
views
Using sed, is it possible to proceed addings, delition of lines and substitution in one shot
Working on the frontmatter of Markdown notes, I'm trying to upgrade the frontmatters with sed.
I can do several substitutions and addings together :
sed -i.bak \
-e '/---/,/---/s/ creation/__dc/' \
...
Best practices
0
votes
4
replies
139
views
how to replace a string within a text with the contents of a file once or multiple times
I want to replace one or many occurrences of a certain string with the content of a file, e.g.
This is some text with a certain string to replaced with the content of a file. The string to replace ...
-4
votes
1
answer
93
views
How to find string and replace several sub lines using sed? [closed]
I have a file with the following content:
/opt:
dev = /dev/hd10opt
log = /dev/hd8
mount = true
check = false
/tmp:
dev = /dev/hd10opt
log = /dev/hd8
...
8
votes
6
answers
587
views
Extract selected data from multiple lines of text
Here is the output of my curl command (print headers only):
HTTP/1.1 200 OK
Server: CacheHTTPd v1.0
Date: Thu, 08 Jan 2026 19:21:11 +0000
Content-Type: application/octet-stream
Content-Length: ...
Best practices
1
vote
8
replies
152
views
using sed to replace multiple lines between two patterns
I have a file example.txt with the following content:
Begin of file
BEGIN
1
2
3
4
5
6
7
8
9
10
END
End of file
I want to replace every second line inside a matched space of sed. So I tired
cat ...
-1
votes
3
answers
109
views
sed outputs matching line twice
I want to delete all lines that end with OFF except if they contain override.
Input
1765193089 socket override DiningRoom/Socket/Work/state ON
1765206025 socket evening Hall/Socket/Lantern/state OFF
...
3
votes
4
answers
177
views
Array elements in sed operation
I am trying to replace elements on an array using a sed command to compare values between two arrays and replace them accordingly, with the following terminal input:
#!/bin/bash
icons=("" &...
-4
votes
2
answers
122
views
append to file and remove first lines if more than N
I want to edit a file in a single command to append a line and remove first lines if more than N.
The furthest I got was to use sed:
sed -i -e "\$a\[$(date '+%F %T')] $1" -e :a -e "\$q;...
1
vote
2
answers
111
views
How do I place the output of a command in the search pattern section of sed?
I'm trying to place the output of awk 'NR==4 {split($0, a, "$"); print a[3]}' filename in sed -i 's/1/2/' hyprpaper.conf such that 1 is replaced by the output of the command awk 'NR==4 {...
1
vote
2
answers
138
views
How to append each line of a file to the corresponding line of another file?
I have 2 files of data and I'd like to add data from one file to the other, but rather than appending to the file as a whole I'd like to append line-by-line.
E.g. I have:
ip.txt
192.168.0.10
192.168.0....
2
votes
5
answers
118
views
sed: skip match if has already been replaced
I am playing with text automatically converting to markup.
I can make sed enclose some inline code:
echo "this is inline_code test 1_2 (_)" | sed -re "s/([a-z0-9]+_[a-z0-9]+)/\`\1\`/g;s/...
1
vote
5
answers
153
views
sed failing to detect combination of literal and escape characters
I have a long file, where lines are sometimes split up with an equal sign, followed by \r\n. Example:
Hello wor=\r\nld.
I want to delete all instances of =\r\n, so that the offending lines are joined ...
0
votes
1
answer
108
views
bash aws cloudshell for loop only returns the first item, then hangs [closed]
Pulling my hair out, looping over 100K S3 files in AWS Cloudshell; to copy in place to kick off a file processing lambda. when I run my for loop with an aws s3 cp on the dataset I want to, the for ...
3
votes
2
answers
87
views
sed - combine two commands into one command
The following two sed commands works.
sed -E "s#Example(.*)#\nconst ex='Example\1';\n#; s#Input:(.*)#const\1;#" in.txt | sed '/\S/!d'
However, how to rewrite them so that only one sed ...
1
vote
2
answers
74
views
BSD-Sed regex filtering to strip IP addresses on tcsh shell
I've been struggling with this for quite a while. Little differences in escaping and the inner workings of sed between BSD and linux throwing me off.
I want to do something deceptively simple: I have ...
1
vote
0
answers
87
views
Usage of sed in Windows OS via Make
I am working on Windows OS. And here is my makefile snippet.
COMPILER_ROOT := C:/Users/kpt
DATE := $(COMPILER_ROOT)/build/busybox_glob.exe date
SED := $(COMPILER_ROOT)/build/busybox_glob.exe sed
...
1
vote
2
answers
92
views
Using sed -i within a loop
I'm reformatting a big file with sample metadata. I have a file (let's call it File2) with the group each sample belong to, with one id and pop per line. My idea was to while read over that file and ...
2
votes
4
answers
164
views
Can't get `sed` to work with text containing escape sequences
I want to be able to use sed (POSIX) to replace some text in the output from a command with a different message that includes escape sequences to allow me to set the colour.
To illustrate the problem.
...
3
votes
7
answers
395
views
Remove only odd lines' line breaks, with Vim, sed or awk
I have the following file:
line 1
abc
line 2
def
line 3
ghi
.....
.....
I need it to become:
line 1 abc
line 2 def
line 3 ghi
......
......
I know how to remove newlines, but not odd or even line ...
2
votes
8
answers
191
views
Remove a string character between 2 special characters in the headers of a fastq file
I have a fastq file containing several sequences with headers such as :
tail SRR11149706_1.fastq
@SRR11149706.16630586 16630586/1
CCCAACAACAACAACAGCAACCTCCTCACGCCAACGCCGATCCCGCCGCTGTTTTCCAA
@...
6
votes
5
answers
158
views
Unpivot a line in sed or awk retaining parent fields
Source lines formatting, for example:
Value11 | Value12 | ValueA,ValueB,ValueC
Value21 | Value22 | ValueA2
Desired output:
Value11 | Value12 | ValueA
Value11 | Value12 | ValueB
Value11 | Value12 | ...
-1
votes
1
answer
71
views
sed -e expression char 42 unterminated command `s' command [closed]
I have this sed command for adding a password to a file:
sed -i "s/password:/password: $escaped_password/" "$file"
If I replace the $escaped_password with the password, the script ...
7
votes
7
answers
146
views
How to filter word between two patterns - Linux
I would like to filter word between two patterns. In this case, between CN= and a comma (,)
Input:
Subject : "O=This is a test,CN=abc.def.com,L=North,ST=Arizona,C=CA"
Expected Output:
abc....
0
votes
4
answers
140
views
Is it possible to replace a digit with the next digit using regex?
What I want to do is say I have the multi-line string:
1.10...
1.11...
1.12...
1.13...
...
1.18...
I would like to match 1.1([0-9]) and replace with 1.1(\1+1) if \1 is the captured digit from the ...
-1
votes
4
answers
172
views
How to get substring between html tags in awk
I have a curl response with some plain strings like below -
"version":"1.1.8".
I would like to extract 1.1.8 from the raw text using sed or awk.
I tried the below ...
0
votes
1
answer
36
views
How to use insert function in BSD/macOS sed
I'm trying to figure out how the i command in BSD/macOS sed works. From the man page, it looks like sed -e '/^/i\' -e $'\t' <<< a should print: <tab>a (where <tab> is a tab char, ...
5
votes
4
answers
89
views
sed delete and replace on top of file is not working as expected
I want to replace top 3 lines of my file. For this task, these two commands work fine, but then I get two .bak (saved) files...
These work:
sed -i.orig1 '1,3d' /my/path/file;
sed -i.orig2 '1s/^/line1 ...
3
votes
4
answers
133
views
How to insert a new line after a certain row number in a file in environment z/OSS USS (OMVS)
I am working in a USS environment, under z/OS. I need to insert a new line in a file, the line has to be inserted after a certain row number.
I've tried to use the command
sed '4i\newtext' file.txt ...
-1
votes
1
answer
193
views
Suggestions for making a file from a bigger file with grep or?
Looking for a suggestion that would be much faster. I have a large (232GB) file mongo backup. I want to take out only the April 24th lines and make a new file containing only this date or any date of ...
2
votes
4
answers
147
views
Surrounding whitespace-separated URLs with quotes using sed
Problem
I was trying to get sed command to do the same thing I could do with Python regex flavour, but I encountered some problems
Python regex example: (tested it on regex101 and it was working fine)
...
-2
votes
1
answer
104
views
sed parsing substring with a percentage sign
I can't seem to get the needed string with sed. Can someone help me see my error? I have tried so many combinations, but no luck. I know it has to be simple, but I'm at my wits' end.
echo '/documents/...
2
votes
5
answers
204
views
sed does not what I expect it to do
I want to do simple string replacements with sed to rename some files. Unfortunately it does not work the way I expect.
My Input (these files):
'Djrum - Under Tangled Silence - 01 A Tune For Us.flac'
'...
0
votes
4
answers
92
views
Update maven dependency with sed in Jenkinsfile
I have a Jenkinsfile which analyses some local artifactory server for new versions of the parent and dependencies. When I confirm that there is a new version, I want to replace the old version with a ...
-2
votes
1
answer
131
views
Extract Jira ticket number in GitHub Actions with sed [closed]
I have a requirement where I have to extract the Jira ticket number using sed regex. I am using a self-hosted Windows runner. I have used solutions from other similar posts on Stack Overflow but ...
12
votes
7
answers
1k
views
Reformat numbers, inserting separators at fixed positions
There are many lines in a file, like the following:
000100667 ===> 000102833
005843000 ===> 005844000
011248375 ===> 011251958
I would like to insert specific separators into the numbers ...
0
votes
3
answers
229
views
How to replace a question mark in sed
I want to replace a? with b? where ? is a character, not a wildcard. Here is what I tried:
echo "a?c" | sed 's/a\?/b?/'
I expect b?c, but it returns b??c. I also tried two backslashes ...
-4
votes
1
answer
102
views
Find and replace XML tag and not it's value [closed]
I have an XML file that contains the following data:
<Extrinsic name="CommodityVendor">1234567</Extrinsic>
<Extrinsic name="buyerVatID">1122334455</Extrinsic&...
1
vote
1
answer
117
views
Easiest way to collapse mkfs output (backspaces and spaces)
I am dumping the output of the mkfs into the log file, but it displays its process interactively, printing backspaces to get cursor back, then prints spaces to erase, then doing backspaces again and ...
6
votes
6
answers
263
views
Extracting strings from a file with sed and regular expressions
I would like to ask about extracting specific strings from a file using sed and regular expressions.
Below is the example of the input text file (testfile.txt):
# This file contains a short ...
-2
votes
5
answers
130
views
Bash getting a specific section from a file that spans multiple lines [closed]
I am using bash on Ubuntu server. My goal is to get the lines from specific section. I can't think of what tool to use to accomplish this (awk, grep -P, sed, ???). It is the specific section that I ...
0
votes
4
answers
197
views
How can I do a large number recursive word substitutions in a large file using bash in fast time?
This question is based on How do I fix sed commands becoming extremely slow when load is high? with the advice of @markp-fuso, @jhnc, and @Jetchisel to avoid a chameleon question as many of the ...
4
votes
7
answers
554
views
How do I fix sed commands becoming extremely slow when load is high?
I have a bash script that takes a simple properties file and substitutes the values into another file. (Property file is just lines of 'foo=bar' type properties)
INPUT=`cat $INPUT_FILE`
while read ...
2
votes
2
answers
94
views
insert line with command from one bash file to another
I would like to inject to in docker bash file 2nd line (after #!/bin/bash -x) such a command: set -a && . .env && set +a. I've tried this inside source.sh:
TEXT_INSERT='set -a &&...
-1
votes
5
answers
108
views
Adding an additional line of code to a file after the logical end of a statement with a match string
I have a file that is full of lines like:
snprintf(log_buffer,...
...
...
...
);
If I use sed, I can find these lines with "snprint.*...
0
votes
3
answers
104
views
Issue with Regex and SED matching Date in Apache date format
I was trying to match date in apache log in many format as you can see below
Regex101
https://regex101.com/r/Cug8lX/1
The regex is working but with sed, I can't succeed to make it match. It don't say ...
0
votes
9
answers
219
views
linux sed expression to select text between markers
Here is a challenge for regex gurus. Need a very simple sed expression to select text between markers.
Here is an example text. Please mind it can contain any special chars, TABS and white spaces even ...
4
votes
5
answers
205
views
How to extract links from an html page
I have an html page that has data like so:
<td><a href="test-2025-03-24_17-05.log">test-2025-03-24_17-05.log</a></td>
<td><a href="PASS_report_test_2025-...
3
votes
4
answers
104
views
Delete string that spans multiple lines
I have some extraneous html table rows I'd like to remove using sed. I want to match and delete these two lines.
[tr]
[/tr]
I've tried sed -i '/\[tr\](\r|\n|\r\n|\n\r)\[\/tr\]/d' ./file which matches ...
2
votes
5
answers
212
views
Picking out file names from grep results where file name contains numbers and hyphens
I have a script that runs a grep command and formats the results nicely for me, asking if I want to open any of the resulting files in an editor etc.
The core of my script is a command like this:
grep ...