Questions tagged [replace]
The replace tag has no usage guidance.
443
questions
0
votes
0
answers
22
views
replace both filename and filename's content relying solely on pattern found on the name-of-the-file
I'd like to know if it's possible to replace both filename.ext and a matched pattern inside its content regarding a pattern founded on the filename itself using sed (and grep also, maybe?).
Let me ...
-3
votes
2
answers
81
views
How can find special row and replace special strings
I wrote a todo list script with "add","clear" and "find" argument. I need to pass a "done" argument and script find related row and replace its first column ...
0
votes
4
answers
115
views
sed: Replacing successful as the condition
I bumped into an excellent use of sed to do replacing, and use its successful state as the condition to print the line:
$ seq 3 | sed -n 's/2/B/ p'
B
I'm wondering if this short form can be extended ...
0
votes
3
answers
178
views
Adding 10 whitespace characters using sed
I have written a script that replaces a string, but I need to add the 10 whitespace characters back. I can add it back by pressing space 10 times, and that works, but this looks hideous in my script.
...
1
vote
4
answers
221
views
Insert one file's content into another file and replace it between pattern
I have a file1.html:
<!doctype html>
<html>
<head>
</head>
<body>
text
<!-- start-replacing -->
<p>more text1</p>
<p>more text2</p>
...
0
votes
1
answer
80
views
Replacing multiple substrings in a filename with one (bash or zsh)
I have a text file with filenames, where the filenames contain information in various formats (e.g., resolution 4k, 4K, 2160, XxY etc) and I want to rename files to a singular consistent format.
When ...
1
vote
1
answer
134
views
Find and replace a special pattern for all files in a folder
I have a series of files in a common bioinformatic format (FASTA) that consist of a string of: 1 special character ">", text as the sample name (length of the string varies, can include ...
3
votes
5
answers
349
views
Replace empty lines in one file with lines from another file
I have file1.csv
"word 1"
""
"word 3"
""
"word 5"
"word 6"
and file2.csv
"replacement text 1"
"replacement text 2"
&...
0
votes
1
answer
109
views
How do I find and replace something AFTER a specific term in a terminal output
I need to run blkid and copy a specific UUID after a specific known partition name.
Then, I need to paste this into an exists config file, replacing a UUID that it's already there (parent_device_uuid)....
0
votes
2
answers
77
views
Merge two two-column files based on a common first column prefering values from the second column of a given file
I have two text files 1.txt and 2.txt that contain two tab separated columns. The first column of both files consists of tokenized text. In both files, the first column is the same. The word order ...
1
vote
3
answers
2k
views
Replace empty string with text using sed or similar tool
Is it possible to replace an empty string with some text using sed or similar tool?
sed 's/^$/sometext/'
will match an empty line but that is not what I am looking for. I thought that
sed 's//...
0
votes
1
answer
117
views
Find and replace everything inside bracket using unix shell - several lines
I need to find and replace a big part of a json file using shell.
For exemple I have this file :
{
"Balise1": true,
"Balise2": true,
"OtherThingEnabled": false,
...
1
vote
2
answers
84
views
sed -Extended RegEx question here
I feel dumb as a sack of hammers asking this here, but it's been a long day and I simply CANNOT figure out what I'm doing wrong here.
I have a file; we'll call it textfile.txt. It's contents are (in ...
0
votes
1
answer
133
views
Add one second to yyyymmddhhmmss with awk
I have this file where the seventh field is a yyyymmddhhmmss. I need to increase just one second this number.
So far, I've come up with:
awk 'BEGIN{FS=OFS="|"} $7=$7+1 ' <file>
And ...
0
votes
1
answer
532
views
ansible lineinfile module to match variable_value
how can one possibly match value of a variable through regex parameter in lineinfile module?
- name: emptyline before search
lineinfile:
dest: ~/file
insertbefore: '^{{ search_text }}$'
...
1
vote
1
answer
32
views
Show application list with description, ommitting "nothing appropriate"
I've been working on a script with a variety of functions.
I'm trying to add a function that show's the output of the whatis command for all applications in the "/usr/share/applications" ...
2
votes
3
answers
347
views
Sed replace the middle string
I have this this String
22<>22
And i have this list
test
dev
too
The output should be like
22test22
22dev22
etc..
This is the commands i use for do that
cat list | sed 's/$/22/g' | sed 's/^/...
2
votes
2
answers
441
views
Delete multiline blocks with sed non-greedy
How can one remove code blocks starting with /** START */ and ending with /** END */ using sed non-greedily considering that code blocks are multiline and empty lines may occur within START-END blocks?...
0
votes
1
answer
71
views
uncomment the line upon search string while ignoring case in a file
Below is my cron file entries:
#Ansible: test2
*/15 * * * * ansible-playbook /web/playbooks/automation/detect401MORTEN/va_action.yml | tee -a /web/playbooks/automation/detect401MORTEN/cron.out
# #...
0
votes
3
answers
1k
views
How to replace all matched strings in the files recursively?
There are about 50 HTML/js files in the folder name site,
some of the files contain (below lines are combined from files)
{"rendered":"http:\/\/localhost:4542\/?page_id=854"}
...
0
votes
2
answers
161
views
insert block of lines only before first matched pattern
i need to insert the below lines (after variable substitution) before the first matched string which is server { in /etc/nginx/nginx.conf
Variable:
website=www.hello.com
Block of lines:
server {
# ...
0
votes
1
answer
140
views
Replace text in a file after nth line till the end of file in unix
I have a file
File.txt
LV_A=1;
LV_B=2;
LV_C=rr;
jfffkf LV_A dndd LV_B
hjhf LV_C
hjhf LV_Chjhf LV_C
hjhf LV_C
hjhf LV_C
hjhf LV_C
etc
i will create file call replcae.txt
LV_A,1
...
0
votes
1
answer
334
views
How do recursive replace of string with '?
In a zsh Terminal window under macOS, I'm trying to replace all instances of (Y, d') with \opair{Y, d'} recursively on all .tex files starting in the current directory.
The following seems to do ...
0
votes
4
answers
96
views
can sed add a new line after matching a pattern while not editing line pattern is on?
I need to find a way to add a line to files after a pattern, but I don't want to edit the line the pattern is on.
For example, my original file will look something like
group: misc
host: server1
job: ...
0
votes
1
answer
824
views
Include spaces and tabs in "awk" search and replace
Another user helped me earlier to fix something I'm doing with awk, where I search for a string at any point in all files and replace two numbers in the same line when I find it.
awk -i inplace '/^...
1
vote
3
answers
97
views
Different $RANDOM results in sed when reading lines in multiple files
I'm trying to run a command that will read multiple files in a directory, look at each line of each file, and whenever it reads a specific string ("gene_height") on any line it replaces 2 ...
1
vote
2
answers
560
views
awk get column value from file 2 for file 1 if value matches in 2 columns [duplicate]
I have 2 files, trying to set column value that is taken from file2 into file1 when their columns match
file1:
signup||20200620|A3332|en|LA||ACTIVE
signup||20200620|B4443|en|CA|66001|ACTIVE
signup||...
0
votes
1
answer
97
views
How to replace a string containing date and IP with sed command
I have the following string of characters, including the IP address and date. For some security reasons, I need to hide the first two digits of the IP address.
text 200.200.10.2 2022.05.07 15:32:43 ...
-1
votes
2
answers
129
views
awk bring same field value from file1 and set in file2 [closed]
File1 (parent)
2210 1110 Earth
2211 1111 Marse
2212 1112 Saturn
2213 1113 Pluto
File2 (child)
3310 2210 Earth
3311 2210 Space
3312 2211 Mars
3313 2211 Space
3314 2212 Saturn
3315 2212 Space
3316 ...
-1
votes
3
answers
334
views
awk print only modified lines
I have file like this
d1000 1000
d1001 100
d1002 10
d1003 1
I want to modify second column where length is not equal 4.
But I want to print only lines that are modified, so original text in column 2 ...
13
votes
8
answers
2k
views
How can I replace a character within a specific context in each line of the whole file?
I have a large file which contains hundreds of English phrases in the following form:
\phrase
{. . . * * }
{I shoul-d've stayed home.}
{aɪ ʃʊd‿əv ˈsteɪd ˈhoʊm.} <- only replace on ...
0
votes
1
answer
103
views
How to replace text in file - conditions and arithmetic required
I need a script to change content in some text files. This a bit beyond my limited bash skills. Maybe perl or python would be a better approach, I have no experience with either of these.
I have a ...
-1
votes
1
answer
50
views
Search & replace for a string separated by whitespace upon substring match found
Below is a sample file content:
cat sample.txt
-server -XX:+UseParallelGC -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+AggressiveHeap -XX:+PrintHeapAtGC -Djava.util.logging.FileHandler....
0
votes
2
answers
197
views
sed and awk in sh script not replacing text
The desired work to find a string, and replace to another (all occurrencies), and rewrite/update the file too.
The file is an pbxproj file. I linked a sample of this file
sample of the file (please ...
1
vote
5
answers
1k
views
Replace hyphen(s) with underscore(s) within angle brackets in a file
I’m aware of how to replace string;
e.g., replace hyphen with underscore in a file.
However, I wish to replace all hyphens with underscore
for all text that falls within angle brackets in a given file....
0
votes
4
answers
212
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
3
answers
955
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 ...
1
vote
2
answers
581
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&...
3
votes
2
answers
3k
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....
1
vote
3
answers
722
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 ...
0
votes
1
answer
610
views
Ansible - replace first occurrence of certain expression in file - path includes hostname
I need to replace the first occurrence of a certain string in a specific file.
I think about to use the replace module of Ansible for this.
- hosts: abc
tasks:
- name: Replace first occurence of ...
0
votes
1
answer
93
views
How can I replace a word with another word in a whole hierarchy of files
I have a hierarchy of files and two words: word1 and word2.
In this hierarchy of files, some of them can contain word1. I want to replace every occurrence of word1 with word2. It is possible using a ...
-1
votes
1
answer
79
views
How to search and replace text in files but keep/reuse a certain part of text?
I want to refactor many JavaScript files in many directories which contain access to an object in this format: myObj.something.somethingElse and I want it to be myObj.getSomething(somethingElse) how ...
2
votes
3
answers
2k
views
find and sed to replace special characters
I'm looking for an easy way to rename many files on my Synology NAS. find and sed are available, but not rename.
I can get a list of these files and folders with
find . \( -type d -name "@*" ...
-1
votes
2
answers
402
views
Search and replacing a string on specific file extensions with the folder name
I want to find a string ("AAA") in an specific file extension ("*.txt") inside a directory tree (../MyParentFolder), and replace it with the subfolder name (MySubfolder). I know a ...
2
votes
1
answer
378
views
How to add closing XML tags with sed
I have the following example in the XML doc:
<meta name=Originator content="text/html; charset=iso-8859-6">
<meta name=ProgId content=Word.Document>
<meta name=Generator ...
0
votes
0
answers
46
views
Searching in one file and replacing it in another file
I am searching a substring in one file and replacing it with every occurrence of word "PASSWORD" in another file. Let say I have file1 with content:
{
"outcome" => "...
1
vote
2
answers
3k
views
Alternative to sed -i that does not write temporary files
I have several scripts that edit template text files, by removing a "tag" and replacing it with e.g. a number. To do this I use the
sed -i
command. However, I have an issue with write/read ...
0
votes
2
answers
923
views
Replace specified strings in file with strings from other file with sed [duplicate]
I have two files like this.
file1
line 1
line 2
... (more lines)
aaa PLACEHOLDER bbb ccc
... (more lines)
ddd PLACEHOLDER eee fff
... (more lines)
etc
file2
NEWTEXT1
NEWTEXT2
... (more lines)
There ...
-1
votes
4
answers
422
views
How to replace a part of the word using sed command
I have a file with just one word in each line. I want to delete some part of the word in each line. How can I delete that part using sed command?
Ex:
u_fox/u_snake/u_snail/u_plus/u_core/u_vector/...