58 votes

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

The Vim way is to learn how to intentionally use the yank, delete and other registers. Once you know these, you will easily find your own key sequences to do this. Register "0 is the yank ...
00prometheus's user avatar
50 votes

Replace text quickly in very large file

You can try: sed -i '/old text/ s//new text/g' gigantic_file.sql From this ref: OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to large input files or slow processors or ...
Ketan's user avatar
  • 9,226
50 votes
Accepted

using sed with ampersand (&)

& is special in the replacement text: it means “the whole part of the input that was matched by the pattern”, so what you're doing here replaces user=&uidX with user=user=&uidXsysuserid.. ...
Gilles 'SO- stop being evil''s user avatar
26 votes
Accepted

In `sed` how can I put one "&" between characters in a string?

With GNU sed: sed 's/./\&&/2g' (substitute every (g) character (.) with the same (&) preceded with & (\&) but only starting from the second occurrence (2)). Portably: sed 's/./\...
Stéphane Chazelas's user avatar
20 votes
Accepted

Editing a child JSON element value with jq

(Posting @glennjackman comment as a community answer to prevent system from autodeleting the question) jq '.body.test2 = ["hi"]' will do it
20 votes

Is it possible to replace duplicates of a character with one character using tr

Using tr -s: $ echo 'ab ### cde fghi## jklm' | tr -s '#' ab # cde fghi# jklm -s Squeeze multiple occurrences of the characters listed in the last operand (either string1 or string2) in the ...
jesse_b's user avatar
  • 37.1k
18 votes

Save file after using sed command

Try this: sed 's/yyyymmdd/YYYYMMDDHH24MISS/g' filename > changed.txt Or, to keep the same filename: sed 's/yyyymmdd/YYYYMMDDHH24MISS/g' filename > changed.txt && mv changed.txt ...
AReddy's user avatar
  • 3,172
18 votes

How can I replace a character within a specific context in each line of the whole file?

an awk version (you'll need a relay file, you can one-line it) awk '/\\phrase/ { p=NR ; } NR == p+3 { gsub("r","ɹ") ; } {print;} ' old-file.tex > new-file.tex where ...
Archemar's user avatar
  • 31.6k
17 votes

Replace all values in one column to 1

awk '{print $1, $2, "1"}' inputfile
user1700494's user avatar
  • 2,202
15 votes

In `sed` how can I put one "&" between characters in a string?

Unix utilities: fold -w1|paste -sd\& - Explained: "fold -w1" - will wrap an each input character to its own line fold - wrap each input line to fit in specified width -w, --width=WIDTH ...
zeppelin's user avatar
  • 3,822
12 votes

Replace all values in one column to 1

try awk awk '{$3=1 ; print ;}' oldfile > newfile $3 = 1 will set third field to 1 sed (here GNU or busybox sed with its -i option for in-place editing) sed -i 's/[0-9.]*$/1/' file [0-9.]*$ is a ...
Archemar's user avatar
  • 31.6k
11 votes

In `sed` how can I put one "&" between characters in a string?

Use sed sed 's/./&\&/g;s/.$//'
αғsнιη's user avatar
  • 41.4k
11 votes

How can I replace a character within a specific context in each line of the whole file?

awk 'c&&!--c {gsub(/r/,"ɹ")} /\\phrase/ {c=3} 1' file > newfile c&&!--c is a common awk idiom, implementing the while getline logic, see reference. The action following ...
thanasisp's user avatar
  • 8,132
10 votes

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

To reserve the original Field-Seprator, I did this. The column I wanted to blank-out was number $12. awk -F"\t" '{OFS=FS}{ $12="" ; print }' infile.txt > outfile.txt With gawk -i , if you have ...
z atef's user avatar
  • 1,068
10 votes
Accepted

Btrfs RAID1: How to replace a disk drive that is physically no more there?

Update: According to @mkudlacek, this problem has been fixed. For prosperity, here is my answer to why in 2017, I could not rebuild a RAID with a missing drive. Turns out that this is a limitation of ...
Hans Deragon's user avatar
10 votes

Btrfs RAID1: How to replace a disk drive that is physically no more there?

replace needs the filesystem to be mounted rw to operate. In a degraded BTRFS RAID1 filesytem, you have one and only one chance to mount the filesystem rw using -o degraded: degraded (...
Tom Hale's user avatar
  • 30.5k
10 votes
Accepted

Rename multiple files with 2 conditions/replacements in one line?

Maybe you need to be using the perl rename command. On my CentOS box, it's called 'prename'. $ ls IMG_1.JPG IMG_2.JPG IMG_3.JPG $ prename 's/^IMG/img/;s/\.JPG$/\.jpg/' *JPG $ ls img_1.jpg img_2....
steve's user avatar
  • 21.9k
10 votes
Accepted

Bash string range and replace

You can't stack/nest parameter expansion in Bash, so not even echo "${${q:16:6}/,/.}" will work. (Nested expansions like that do work in Zsh, though.) If you want to stay within Bash, you'll need ...
nohillside's user avatar
  • 3,251
10 votes
Accepted

Add one second to yyyymmddhhmmss with awk

With GNU awk or @ThomasDickey's mawk¹: $ gawk -F'|' -v OFS='|' '{ gsub("..", "& ", $7) sub(" ", "", $7) $7 = strftime("%Y%m%d%H%M%S", ...
Stéphane Chazelas's user avatar
9 votes

In `sed` how can I put one "&" between characters in a string?

sed 's/\B/\&/g' \B - Matches everywhere but on a word boundary; that is it matches if the character to the left and the character to the right are either both “word” characters or both “non-...
MiniMax's user avatar
  • 4,123
9 votes
Accepted

Replacing backslashes with forward slash within double quotes

find + GNU sed solution: find . -type f -name "*.[ch]" -exec sed -i '/^#include / s|\\|/|g' {} + "*.[ch]" - wildcard to find files with extension .c or .h -i: GNU sed extension to edit the files in-...
RomanPerekhrest's user avatar
9 votes
Accepted

Is there a way to replace last occurrence of match using a shell variable substitution?

Note: this replaces a trailing occurrence - not quite the same as "the last occurrence" From the Bash reference manual Section 3.5.3 Shell Parameter Expansion : ${parameter/pattern/string} ...
steeldriver's user avatar
  • 81.1k
9 votes
Accepted

How can I replace a character within a specific context in each line of the whole file?

Since you're on Emacs... The Evil/Vim Way If you have evil-mode installed (or you switch to Vim), you can do this: :g/^\\phrase/+3s/r/ɹ/g That's the simplest. The Keyboard Macro Way Staying with ...
JoL's user avatar
  • 4,755
8 votes

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

Go to visual mode by pressing v to capture the interesting text, and copy it by pressing y. Now capture the text you want delete, and press p for pasting.
Purkhalo Alex's user avatar
8 votes
Accepted

Running several sed commands on same files simultaneously

You can give several expressions to sed in one invocation: sed -e 'expr' -e 'expr' -e ... In your case: sed -e 's/foo/bar/g' -e 's/NAME/name/g' The expressions will be applied to each line of ...
Kusalananda's user avatar
  • 334k
8 votes
Accepted

How to replace last occurence of a character?

For your given data in a file called input: $ awk -F\; 'BEGIN {OFS=";"} { if( $3 == 1 ) { print $1,$2,"inactive" } else { print $1,$2,"active" } }' input 199240050;0180209199240050;active 199240241;...
DopeGhoti's user avatar
  • 76.1k
8 votes
Accepted

A posix-compliant function for replace text with parameters and regex

Escaping the / only is very difficult to do with sed because for instance it would have to be escaped in: Foo/bar Foo[XY]/ Foo\[/x\] Foo\\/bar But not in Foo [/x]bar Foo [^]/x]bar Foo [x[:blank:]/...
Stéphane Chazelas's user avatar
7 votes

Case-preserving search and replace in vim?

You can try this plugin. https://github.com/tpope/vim-abolish This plugin can help you to match not just the case sensitive text, also its variants too. Like /good{,ies} will match both good as well ...
SibiCoder's user avatar
  • 183
7 votes

SED command to replace "[Word1 Word2]" to nothing

The square brackets [ and ] are special characters in sed. You must escape them with a backslash, like so: sed -i -e 's/\[Word1 Word2\]//g'
anaotha's user avatar
  • 866
7 votes
Accepted

Replacing a Part of URL String in a Linux File With Another String

/ is default sed subexpression separator, use another one: sed -i 's~http://www.test.org/name~http://www.test.org/archive/name~g' user.html
RomanPerekhrest's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible