Skip to main content
63 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
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

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
  • 41.5k
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
  • 32.3k
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,972
11 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,123
11 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
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.9k
11 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
  • 33.3k
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,532
10 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} The ...
steeldriver's user avatar
  • 83.8k
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
  • 22.3k
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 to ...
nohillside's user avatar
  • 3,344
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
10 votes
Accepted

removing braces statements containing nested braces inside

Typical approach is to use perl's recursive regex capability: perl -0777 -pe 's/\\SomeStyle(\{((?:(?1)|[^{}])*)\})/$2/gs' file.tex Or if you have to account for braces escaped as \{ (and \ escaped as ...
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,245
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

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

You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example ...
Captain Wobbles's user avatar
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
  • 5,019
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 input ...
Kusalananda's user avatar
  • 356k
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
  • 79.2k
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:]/y]...
Stéphane Chazelas's user avatar
7 votes

Change delimiter in a CSV file

You basically have a CSV file that you would like to replace the delimiter in, from , to ~. Using csvkit: $ csvformat -D '~' file.csv >newfile.csv $ cat newfile.csv 123~ABC, DEV 23~345~534.202~...
Kusalananda's user avatar
  • 356k
7 votes
Accepted

Replacing two patterns in a text files at the same time

I think, since you are only swapping singular characters, tr may be a good tool for the job. Try something like this: tr '0?' '?0' < log.txt
Christian Gibbons's user avatar
7 votes

Bash replace string at multiple places in a file from command line

According to the information you have provided, you want to replace it at two places, so you can use: sed -e /^Run/s/chrome/chromeBackup/g -e /^Vol/s/chrome/chromeBackup/g file_name Edit As @...
Prvt_Yadav's user avatar
  • 6,032
7 votes

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

If you always have a blank line between each section, you can try perl's "paragraph" mode to read each section as a single "line": $ perl -F'\n' -00ane '$F[3]=~s/r/ɹ/g; print join &...
terdon's user avatar
  • 252k
6 votes
Accepted

Replace a string in a CSV file column with another string with a single quote

You need to set the field separator (FS) properly. By default, awk uses any horizontal whitespace(s) as the field separator, so in your case SAMS becomes one field and CLUB becomes another. Hence, {...
heemayl's user avatar
  • 58.1k
6 votes

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

To answer you question in the generic, rename multiple files with 2 conditions/replacements in one line? you would typically use capture groups, referring to them in the replacement expression using ...
steeldriver's user avatar
  • 83.8k
6 votes

Replacing two patterns in a text files at the same time

Since you're looking to swap single characters, you can use the y command in sed to transliterate: sed 'y/?0/0?/' infile
Benjamin W.'s user avatar
6 votes

Replace a string in a file preceeded by another string

Using sed's predecessor ed: ed -s input <<< $'/^reader_1 =\n/^port =\nc\nport = 22443,\n.\nw\nq' This sends ed an ANSI-quoted here-string of commands to edit the file. The commands are, in ...
Jeff Schaller's user avatar
  • 68.8k

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