Questions tagged [replace]

442 questions
172
votes
16 answers

How can I do a 'change word' in Vim using the current paste buffer?

I have some text in my paste buffer, e.g. I did a yw (yank word) and now I have 'foo' in my buffer. I now go to the word 'bar', and I want to replace it with my paste buffer. To replace the text manually I could do cw and then type the new word. How…
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
101
votes
5 answers

Remove line containing certain string and the following line

I use this cat foo.txt | sed '/bar/d' to remove lines containing the string bar in the file. I would like however to remove those lines and the line directly after it. Preferably in sed, awk or other tool that's available in MinGW32. It's a kind of…
jakub.g
  • 3,153
  • 5
  • 20
  • 17
66
votes
3 answers

Replace using VIM, reuse part of the search pattern

I am working with VIm and trying to set up a search and replace command to do some replacements where I can re-use the regular expression that is part of my search string. A simple example would be a line where I want to replace (10) to {10}, where…
Bernhard
  • 11,992
  • 4
  • 59
  • 69
49
votes
5 answers

How to replace the content of a specific column with awk?

Given: there are 40 columns in a record. I want to replace the 35th column so that the 35th column will be replaced with the content of the 35th column and a "$" symbol. What came to mind is something like: awk '{print $1" "$2" "...$35"$…
Marcus Thornton
  • 1,091
  • 3
  • 13
  • 16
41
votes
1 answer

using sed with ampersand (&)

I'm using sed to find and replace patterns within SAS files I have rather than changing them individually. The problem is I am trying to replace macro variables and when I use the ampersand it is not processing correctly. Here's my code: sed -ie…
DukeLuke
  • 513
  • 1
  • 4
  • 6
40
votes
12 answers

How can I "cat" a file and remove commented lines?

I'd like to know if there is a way that I could cat file like php.ini and remove all lines starting with ; For example, if the file contained this: ; - Show all errors, except for notices ; ;error_reporting = E_ALL & ~E_NOTICE ; ; - Show only…
cwd
  • 44,479
  • 71
  • 146
  • 167
36
votes
3 answers

Replace text quickly in very large file

I have 25GB text file that needs a string replaced on only a few lines. I can use sed successfully but it takes a really long time to run. sed -i 's|old text|new text|g' gigantic_file.sql Is there a quicker way to do this?
eisaacson
  • 461
  • 1
  • 4
  • 3
30
votes
5 answers

How to report "sed" in-place changes

When using sed to replace strings in-place, is there a way to make it report the changes it does (without relying on a diff of old and new files)? For instance, how can I change the command line find . -type f | xargs sed -i 's/abc/def/g' so I can…
ricab
  • 692
  • 1
  • 9
  • 15
27
votes
10 answers

How to replace current word under cursor in Emacs

How do I replace current word under cursor in Emacs? I know that I can use query-replace or replace-string but every time I do so I have to type entire string to be replaced, this is just annoying. Vi has equivalent command cword and I can use a…
Think Pl
  • 473
  • 1
  • 5
  • 7
26
votes
3 answers

Replace string with contents of a file using sed

I have two different files: File1 /home/user1/ /home/user2/bin /home/user1/a/b/c File2 I want to replace the of File2 with the contents of File1 using sed. I tried this command, but not getting proper output: cat…
chanchal1987
  • 686
  • 3
  • 9
  • 16
24
votes
5 answers

How can I replace text after a specific word using sed?

I have a file named .ignore. In need to replace the projdir. For example: ignore \..* ignore README projdir Snake I need to replace Snake with, for example, "PacMan". I read the man page, but I have no idea what to do.
Jakub Baski Gabčo
  • 343
  • 1
  • 2
  • 5
16
votes
2 answers

Fastest & Most Core way to replace "\r\n" with "\n" in a file?

I suppose tr is a more core method and thus probably a faster way to replace things within a given file. However tr can only replace equal amounts of characters. meaning... 2 characters can only be replaced with 2 characters which means replacing…
user73235
  • 163
  • 1
  • 1
  • 4
15
votes
2 answers

Case-preserving search and replace in vim?

In vim, I know I can search with or without case sensitivity. But if I want to search for a string in either upper or lower case, and replace it with a replacement of the same case, is that possible in a single :s///? For example, I want to change…
Kevin
  • 40,087
  • 16
  • 88
  • 112
14
votes
4 answers

Vim :s replace first N < g occurrences on a line

In vim, I sometimes have occasion to replace the first few occurrences of a match on a line, but not every one like g would. e.g.: a a a a a to b b b a a I know I could use :s/a/b/[enter]:[up][enter]:[up][enter], but that's tedious enough at…
Kevin
  • 40,087
  • 16
  • 88
  • 112
14
votes
3 answers

Replace multiple strings in a single pass

I'm looking for a way to replace placeholder strings in a template file with concrete values, with common Unix tools (bash, sed, awk, maybe perl). It is important that the replacement is done in a single pass, that is, what is already…
Ambroz Bizjak
  • 749
  • 2
  • 6
  • 8
1
2 3
29 30