Linked Questions

310
votes
2answers
1.6m views

Using 'sed' to find and replace [duplicate]

I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. I am looking for help with an ...
27
votes
2answers
2k views

Security implications of running perl -ne '…' *

Apparently, running: perl -n -e 'some perl code' * Or find . ... -exec perl -n -e '...' {} + (same with -p instead of -n) Or perl -e 'some code using <>' * often found in one-liners ...
6
votes
3answers
11k views

How to delete all occurrences of a list of words from a text file?

I have a file containing a list of words. I want to remove all occurrences of all the words in this file from a big text file. Example: File 1 queen king Text file sample Both the king and queen ...
4
votes
4answers
205 views

Double quote value assignments stored in a CSV?

I have a file that contains text as follows: dt=2016-06-30,path=path1,site=US,mobile=1 dt=2016-06-21,path=path2,site=UK,mobile=0 I want to convert it to text with double-quoted values in the key-...
3
votes
2answers
365 views

How do I delete a line only if it is at the specified line number and it matches the pattern? [duplicate]

How do I delete a line only if it is at a specified line number and it matches the pattern? For example: I want to delete (d); the third line (3); if it's blank (^$); The following syntax: cat ...
3
votes
2answers
619 views

Replace only in particular range string

In the canonical How can I replace a string in a file(s)? in 3. Replace only if the string is found in a certain context, I'm trying to implement the replace of a pipe with white spaces in file with ...
2
votes
1answer
169 views

What does **/*(D.) mean in (zsh) sed arguments?

I see this command being given as how to search and replace strings in files in this answer: sed -i -- 's/foo/bar/g' **/*(D.) What does the pattern */*(D) mean? I am not sure what they are called, ...
2
votes
2answers
3k views

Using regular experision in “rpl” command

I need to replace text in lots of files. I'm using rpl for this purpose, but I can't find any way to make it use regular expressions. Can rpl use "regex"? how? Edit: I'm using rpl 1.5.2 from Ubuntu ...
2
votes
1answer
178 views

Changing text in text file [duplicate]

Is there a command line way to change text in the file? Like say I would like to change all strings "lukuunottamatta" to the form "lukuun ottamatta" and "Lukuunottamatta" to the form "Lukuun ottamatta"...
1
vote
1answer
334 views

Inserting delimiter into file

I have a file with a similar format to the following: 1.1.1.5 Ensure mounting of hfsplus filesystems is disabled 1.1.1.6 Ensure mounting of squashfs filesystems is disabled 1.1.15 Ensure nodev option ...
1
vote
1answer
825 views

Grep global find replace [duplicate]

Is there a way to grep for a regular expression on an Ubuntu server and then find and replace it with another string? I am using the following command right now, but it doesn't account for actually ...
1
vote
2answers
88 views

SED Search and Replace on Linux [duplicate]

I want to change the value from 1521 to 1525 in FROM_HOST only in file gexhosts.cat: FROM_HOST db201521 TO_HOST db201521 Is there a way to do that or some work around? I would really prefer ...
1
vote
0answers
174 views

Search For a Word and Replace It In Entire File Using Egrep [duplicate]

How do I search an entire file for a word and replace it with another word using egrep?
1
vote
1answer
652 views

How to search-replace-backup files recursively in linux?

I'd like to recursively search for a string within all files in a folder. If the string was found, backup the file to the same location (copy to <filename>-orig) and replace the found string ...
1
vote
1answer
364 views

Escape >');} < / in sed

In 30 files in 30 directories, I want to find: script>');} < /script> and replace it with: script>');} < /script--> What special characters do I need to escape in the find field? ...
0
votes
1answer
196 views

Running several sed commands on same files simultaneously

To change all occurences of the string foo to bar in all files in a directory I use sed -i -- 's/foo/bar/g' * Found that here But I also want to change NAME to name etc. I tried, sed -i -- 's/foo/...
0
votes
3answers
101 views

How to modify file content? [duplicate]

Ubuntu 16.04 I have some pretty large file so modifying it by hand is not possible. I'd like to change all occurences of <tag1>true</tag1> to <tag1>false</tag1>
0
votes
1answer
625 views

Why won't my change to the sshd_config file persist? [duplicate]

I am writing a script to modify settings on my Ubuntu server. I have the following line in it: sudo sed 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config The problem is ...
0
votes
1answer
221 views

Search and replace something contained in ' (single quotes)

In various files in lots of sub folders I have the following text in large text files, each unique Linux.Secret = 'XYZZYXZYXZYXZYXZYXZ' I would like to make it so all the Linux.Secret = '' are the ...
0
votes
2answers
182 views

Insert character at the beginning of specific lines [duplicate]

Let's say I have file: % This is first line % This is second line This is content % This is the end How can I insert a % character at the beginning of every line which already starts with %? ...
0
votes
1answer
3k views

How to replace the last octet of a valid network address with the number 2 [duplicate]

Lets say I have a string that can be any possible/valid network address like so: STR="192.168.1.0/24" What I want to do is replace the last octet (in this case it's 0/24, however it can be anyting) ...
0
votes
1answer
795 views

Modify part of line if keyword is found using sed? [duplicate]

I want to modify a line if a specific keyword is found in a line. CS_CODE, SM_NUM, PORT_NUM, DEV_PORT_NUM, DIRNUM, MAIN_DIRNUM, BILL_NO, ADR_NAME In above line, if 'PORT_NUM' is present then '...
0
votes
1answer
101 views

Replace copyrights recursively [duplicate]

I have many files under the current directory. Each file contains copyrights headers like, # # Copyright 2013 Company, Inc. All rights reserved. This software # is property of Company, ...
0
votes
1answer
2k views

How to change a single line in multiple files [duplicate]

I have ~750 .php files in the same directory that contain the line include("path/to/file.php"); I would like to change this line in every file to require_once("path/to/file.php"); What would be an ...
0
votes
2answers
53 views

Remove occurrence of a pattern from string everywhere [duplicate]

I want to remove occurrence of a string from a given string. Like /hello: from a string like /yo:/sup:/hello:/yo:/hello:/yup. And one thing for sure if string contains a colon in end then it must be ...
0
votes
1answer
132 views

Substituting words in parenthesis using sed [duplicate]

I have a basic python script that looks like this: my_name = 'Zed' my_age = 35 my_height = 74 my_weight = 180 my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown' print "let's talk about %s" % ...
-1
votes
1answer
1k views

How to edit a file from a script using a loop instead of find and replace with sed? [duplicate]

Here is an example of what I am trying to do: #!/bin/bash Update=$1 if [$Update == Value] then NewValue= $Update vi %s/Value/$NewValue/g > Updatedfile fi exit 0 Is there a way to ...
-1
votes
1answer
732 views

How do i create a sed script to prompt the user to replace a number in a file? [duplicate]

I'm new in Unix, and I'm learning about sed command. I need to write a very small script for searching a number in a file and replacing it. I tried doing grep, and than deleting the specific phone ...
-1
votes
1answer
5k views

how to replace a specific pattern from all files [duplicate]

suppose I want to replace abc with xyz in all files in a directory. How do I write a script for this ?
-1
votes
2answers
887 views

How to use sed on all matching files?

I am trying to run sed on the output from find | grep \.ext$ However, sed requires input file and output file. How can I save the output line to a variable, or pass it to sed?
-1
votes
1answer
207 views

Replace mutiple strings with different values in a file using unix [duplicate]

I have a file in which the data is like below, 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ 03,test1,41203016,,/ 01,0000000,xxxxxxx/ ...
-1
votes
1answer
122 views

Replace a value in a file, in a certain context, with an operation result

I have an array of JSON object with inside local urls and speed parameters like [{server:"192.168.0.100", speed:34}, {server:"192.168.0.130", speed:52},...] I need to update the speed value for each ...
-2
votes
1answer
40 views

How do I convert all occurrences of the word “OLD” in some file with the word “NEW” in Linux [duplicate]

Can someone give me a command that convert all occurrences of the word “OLD” in some file with the word “NEW” in Linux? Thanks in advance
-3
votes
1answer
3k views

How can i replace a specific string within a line inside a text file [duplicate]

$ cat text.txt my name is Steven my age is 10 i like dogs What command will be fitting so that echo "Kate" will replace Steven on text.txt?
-4
votes
3answers
1k views

find and replace strings with another string command line

I have a Ad-blocking hosts file with 0.0.0.0 IPs pointing to URLs. How to replace 0.0.0.0 with 127.0.0.1 command line without any visual text editor ? Manually doing this with nano , vi etc. is ...
-4
votes
1answer
3k views

How to change certain word in certain line in a file [duplicate]

i have a file in which i have to change a word and then save it by same name. for ex- for file "name"- my name is joe (original data) my name is roy (changed data) how to do it in best possible ...