Questions tagged [sed]
sed is a command-line stream editor for filtering and transforming text.
6,746
questions
0
votes
3
answers
78
views
Search and Remove Lines of Code from all files
I need help and need to search for and remove a 3 line bit of code from all files in a directory.
Code is:
if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) )...
-2
votes
1
answer
104
views
Select first n words of each line of a file
Using sed or awk, I want to select the first n words of each line of a file.
I have a file with three lines, say
one two three four
one two three four
one two three four
and I want the output file to ...
0
votes
2
answers
38
views
How to modify all lines in a file
I have a file called index.js with more than 200 lines :
AcademicCapIcon.svelte
AdjustmentsIcon.svelte
AnnotationIcon.svelte
ArchiveIcon.svelte
...
I'd like to change this to the following:
import ...
1
vote
1
answer
46
views
Advanced sed command in pyenv installation instructions
Pyenv repository, has the following sed command for bash:
sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{...
0
votes
1
answer
47
views
Does the regex engine have an associated executable?
I'm working with a tool that uses a whitelist to allow certain commands to be executed. The whitelist works using file paths like so:
command1=/bin/echo
command2=/bin/sed
...
The problem is commands ...
-1
votes
2
answers
49
views
Replace empty spaces with TAB except in last column
I have a file that looks like this.
How can I replace each empty space with a TAB except in the last column that starts with ID?
chr1 13513 14763 Medtr1g004950 . + JCVI gene . ID=Medtr1g004950;Note=...
2
votes
4
answers
166
views
Unable to exclude lines containing ^M from grep
I have the following file from which I want to extract only Removed '2022-01-30_01-00-05', at the end.
Removing '2022-01-30_01-00-05'...
0.46% complete (00:03:45 remaining)^M 5.49% complete (00:00:...
4
votes
7
answers
324
views
awk: deal with newline separation in wrong place
I have some data that looks like this:
abc
123
456
789
def
111
222
333
ghi
999
888
777
666
i.e. the records are separated by multiple newlines but in the wrong place. What I want is to get it like ...
2
votes
3
answers
224
views
Replace dynamic word using sed command
I have a requirement where we need to replace a word in a file which can be different everytime I execute the script, the content of the file is
class DynamicContentToReplace > Something
Here I ...
0
votes
0
answers
32
views
How to break a long line into max length by word and use that programatically in a git commit [duplicate]
I have a scenario where I need to convert a long string of text and break it by words not exceeding a set line length (70 characters). I need to replace the space with a '\n' where the line break ...
0
votes
5
answers
75
views
Move the line matching to the Immediate next line to the end of matching line
I have input like this ,i need move the line after *_job to the end of the *_job line, if it is job_type and print the entire file.
IF the next line is not job_type just print them.
i able to print ...
1
vote
2
answers
70
views
sed replacing a string with itself and a new line
I have the following string multiple times in a html file
<br> <h4 id=...
Now, I know sed might not be perfect for html editing, but could tell me how can I replace br> <h4 id with br&...
2
votes
4
answers
300
views
sed + add string on the beginning of line but ignore if already exist
here is example how to add string on the beginning of line when match the UUID number in fstab
sed -e "/UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a/ s/^/###FAULTY_DISK###/" -i /etc/fstab
and ...
0
votes
2
answers
70
views
I want to replace \ with \\\\ using sed
I have tried using sed to read \.
Unable to read \ and replace it with \\\\.
I want to replace single \ with 4 \\\\.
1
vote
1
answer
70
views
Using double quotes in double quoted string (for sed)
I am adding additional functionality to an install script and decided to edit another file for an upgrade. To accomplish this I will need to get a working sed command using the following on Linux:
sed ...
0
votes
1
answer
47
views
Add parameter to existing config file if present
Using a script I want to modify the /etc/pam.d/common-session file based on the existing value:
if session required pam_mkhomedir.so exist, then add skel=/etc/skel/ umask=0022
if it's not ...
-2
votes
1
answer
37
views
sed is not working as expected for a string with quotes [duplicate]
I need to change 's3_bucket': 'pass' to hello
but it is not working
ex:
echo "'s3_bucket': 'pass'" | sed 's/'s3_bucket':/hello/'
outputs:
's3_bucket': 'pass'
Please help understand what am ...
0
votes
0
answers
28
views
I need to replace placeholders in a yaml file with the lines from another file [duplicate]
I have a text file looking like this
Name: somename
username:some alphanumeric string
password:some alphanumeric string
And a yaml file looking like this
apiVersion: v1
kind: secret
metadata:
name: ...
1
vote
2
answers
133
views
Add prefix to line that is the pattern of text after dot using sed or other command
I am trying to edit linux file using sed or other command.
This is my File t:
first.xlsx
second.txt
third.ksh
I want to change File t to be:
xlsx/first.xlsx
txt/second.txt
ksh/third.ksh
So text ...
0
votes
3
answers
53
views
I need to replace 00 with a blank in a pipe delimited file after pipe 5 In Linux
I need to replace 00 with a blank in a pipe delimited file, but only if the 00 appears after the fifth pipe. I am using Linux. I tried: {sed 's/00/\ /6' input.txt > output.txt}
Sample data:
FIELD1|...
1
vote
2
answers
151
views
How to programmatically maintain umask in /etc/profile and /etc/bashrc
I am looking for a way to programmatically maintain a consistent umask in the files /etc/profile and /etc/bashrc. They have an entry such as follows:
if [ $UID -gt 199 ] && [ "`/usr/bin/...
1
vote
4
answers
177
views
comment the specific line in fstab file that contained the relevant UUID number
We want to comment the specific line in fstab file that contained the relevant UUID number
Example:
Disk=sde
UUID_STRING=` blkid | grep $Disk | awk '{print $2}' `
echo $UUID_STRING
UUID="...
0
votes
1
answer
40
views
How can one pipe a random string to sed for substitution in a file? [duplicate]
I've tried this:
echo $RANDOM | md5sum | head -c 20 | { read val; sed -i 's/__SALT__/$val/g' app.txt; }
But this replaces __SALT__ with the string $val instead of the value in the variable.
2
votes
4
answers
187
views
Appending word in certain lines by using a list with line numbers with SED
I have a file example.txt which contains multiple lines:
Larry is funny!
Funny, I've no glue!
Look here!
Tom has no pants.
The underpants are in the drawer.
Pants are in the closet!
After creating a ...
1
vote
1
answer
78
views
Using sed to substitute text and add text after certain pattern
I'm looking to substitute text inside a file after a certain pattern.
For example:
The content of example.txt is
Something==x.y.z
I'd like to change it to
Something>=x.y.z,<x.y.z+1.0
I know I ...
0
votes
6
answers
94
views
linux change date format in file with full year
I am trying to change the date format in file with the full year. This is my data.
06/30/21 07/01/21 05436841182400056721972 random text
07/06/21 07/07/21 05436841188400057034635 random text
07/17/21 ...
4
votes
1
answer
177
views
sed: replace multi-line block with standard input
Given is input_file:
1
2
START
foo
END
3
Goal is to replace content inside including START ... END block with multi-line content from stdin:
1
2
hello
world
3
What I tried:
sed '/^START$/,/^END$/d;...
0
votes
1
answer
101
views
Awk-Relational processing. Editing on one column must reflect on column on another line number specified by third column of actual line
relational column processing using awk sed
File 1 ---> Scrambled data in column (Old stale files with intermediate test data)
mL9A7hajHyuVIQr1HNP7ThYfj9yBUd
Iq4iqnH4UftLgGUSobLeti0hkmdMn7
...
1
vote
2
answers
468
views
Is there a way for 'jq' to read json with single quotes instead of double?
I use jq as an efficient way to parse out large nested json files. I sometimes want to us jq with a json formatted file which has single quotes instead of double around keys and values like below:
{
...
0
votes
5
answers
162
views
Delete columns that contains only numbers
I have a table that containes some data and I need to delete the columns that contains only number (Nr.,Cod,Tiraj):
Nr. Autor Titlu Cod Tiraj Tara
1 Gauss Carl ...
1
vote
0
answers
32
views
sed + how to be sure that sed replaced the value correctly
here is a nice example how to replace the value on match key on file
str="192.2.34.40:2181,192.2.34.34:2181,192.2.34.32:2181,192.2.34.52:2181,192.2.34.53:2181"
sed -i "s~^[[:space:]]*...
0
votes
1
answer
52
views
sed + remove line with word match and only if comment appears in beginning of line
delete lines with word match is easy
for example when we want to delete the line that match the word - max.connections
sed '/max.connections/d' /home/conf.txt
but how to delete the match lines as ...
0
votes
1
answer
57
views
Write sed command to input file
I'm building a script to automate boilerplate. In it, I append a string:
"compile": "browserify js/main.js > ./dist/bundle.js -t babelify",
"watch": "watchify js/*...
-1
votes
4
answers
108
views
How to add text before the Nth occurrence of a text using sed only?
I searched on previous questions but they were only with appending so adding after the occurrence. i need it to be before:
world world
world world
world world
So sed must add "hello" for ...
0
votes
4
answers
75
views
How might I comment out with sed or awk a block of two lines (a unique line followed by an indented nonunique one)?
I wrote the following code:
#!/usr/bin/bash
wireless_state=$(ip l |grep "3: wlp" |sed -E 's/^.*state ([DOWNUP]{2,4}).*/\1/')
ethernet_state=$(ip l |grep "2: en" |sed -E 's/^.*...
3
votes
2
answers
231
views
How to substitute non printable characters with space character in a file
I have read nearly every answer about this topic on this website or Stackoverflow but didn't manage to solve the issue below.
When I copy the text from a PDF file and paste it into a text file file....
0
votes
1
answer
76
views
Show only dates bigger than the search pattern even if the search pattern does not exist in the (log) file
I want to search the syslog for dates bigger than a specific search pattern. For example I want to find every syslog entry where the date is bigger than JAN 6 16:24:00. I know I could use sed (sed -n ...
0
votes
2
answers
63
views
Using sed to display character string containing one or more pattern characters in a line and ignore other strings
I want to pass the result of a sed command to a variable to only read part of each line of a file. For example, here is the content of my input file named "fic1.txt":
-->cat fic1.txt:
/...
0
votes
1
answer
52
views
How can I uncomment line with empty space?
Suppose I have a file with this content:
#plugin = eosio::producer_plugin
#plugin = eosio::producer_api_plugin
And if I try to uncomment value by using sed, it will fail:
sed -E '/plugin.*(...
6
votes
8
answers
498
views
Make change operations on substring only
In a file that has any garbled text before and after a section that is marked by patterns START and END (specific strings that occur only once each and in the correct order and on the same line). I ...
-2
votes
1
answer
183
views
add header to a big csv file >5GB
i have a big csv file (5gb) with 300 columns, and i want to add a simple header via the commande line :
row-0;row1;row2 ... row299
i used sed (without a loop) :
sed -i 1i'row-0;row-1;row-2 .... ; row-...
2
votes
1
answer
91
views
sed with here-string fails, but succeeds when echo output piped to sed
After updating sed to 4.4 version, sed doesn't replace spaces with commas in the output of find command given to it as here-string:
sed --version
sed (GNU sed) 4.4
ls -l /tmp/test/
total 0
-rw-r--r-- ...
2
votes
1
answer
95
views
show only next line after the matched one with sed
https://stackoverflow.com/a/7451478/15603477
I understand grep -A1 'blah' logfile | grep -v "blah"
but I don't understand sed -n '/blah/{n;p;}' logfile
So far I checked manual sed command ...
1
vote
2
answers
329
views
Add new line after a string match in JSON file
I need a help doing the below task.
In a JSON file I have a part something like below
"Principal": {
"AWS": [
"arn:aws:iam::12345677890:...
4
votes
5
answers
437
views
Merge every two lines after the pattern until next pattern
File.txt
String1?
word1
word2
word3
word4
String2?
word5
word6
word7
word8
Desired Output:
String1?
word1 | word2
word3 | word4
String2?
word5 | word6
word7 | word8
Only pattern is Lines with /?$/
...
0
votes
1
answer
48
views
SED - what is the diff between 1) and 2) - why no match in 2) [duplicate]
echo "123456xx111"| sed '{s/\([x]\)/{\1}/}'
123456{x}x111
echo "123456xx111"| sed '{s/\([x]+\)/{\1}/}'
123456xx111
0
votes
2
answers
84
views
Manipulating CSVs in Unix - best native Unix tools to learn [closed]
I am new in the Unix environment. I am looking to learn manipulating CSV files (for example: adding new columns, getting values from one CSV by index and appending the value in a new CSV file etc.) in ...
0
votes
2
answers
76
views
sed complicated script
I'm trying to understand this great answer that explains how to implement "pop" command to a file using sed.
I build some test data with
printf "Line #%2d\n" {1..7} >file
then ...
1
vote
3
answers
155
views
Modify and Replace $1 (awk) or \1 (sed) Values from Decimal to Hexadecimal Globally in a String?
Is it possible to Modify and Replace $1 (awk) or \1 (sed) Values from Decimal to Hexadecimal Globally in a String? It is possible that the string may contain any decimal value, which needs to be ...
7
votes
6
answers
816
views
Uppercasing first letter of a line after period in previous line
I have many little poems to edit like this:
Io avevo due sorelle,
una bionda e l’altra mora,
tutte e due leggiadre e belle
e gentil come l’aurora.
ma la bionda mi è sparita,
se ne è andata all’altra ...