Hot answers tagged

175

Option 1 You could use registers to do it and make a keybinding for the process. Yank the word you want to replace with yw. The yanked word is in the 0 register which you can see by issuing :registers. Go to the word you want to replace and do cw. Do Ctrl+r followed by 0 to paste the 0 register. The map for that would look something like this (...


45

You can do like this: awk '$35=$35"$"'


37

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 hard disks), substitution will be executed more quickly if the "find" expression is specified before giving the "s/.../.../" instruction. Here is a ...


35

& 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.. To insert an actual ampersand in the replacement text, use \&. Another thing that looks wrong is that . in the search pattern stands for any character (...


28

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 register. Anything you yank will be put here, but neither deletes or change removals will ever touch register "0. So, in your example, you had just yanked a word. ...


26

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/./\&&/g;s/&//' (replace every occurrence, but then remove the first & which we don't want). With some awk implementations (not POSIX as the ...


25

With sed, you can do: sed 's/\r$//' The same way can do with tr, you only have to remove \r: tr -d '\r' although this will remove all instances of \r, not necessary followed by \n.


23

Search for a line that starts with projdir, and replace the whole line with a new one: sed -i 's/^projdir .*$/projdir PacMan/' .ignore ^ and $ are beginning/end-of-line markers, so the pattern will match the whole line; .* matches anything. The -i tells sed to write the changes directly to .ignore, instead of just outputting them


20

You could do it in two passes using the print action on the first pass with: find . -type f | xargs sed --quiet 's/abc/def/gp' where --quiet makes sed not show every line and the p suffix shows only lines where the substitution has matched. This has the limitation that sed will not show which files are being changed which of course could be fixed with ...


20

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 input into a single instance of the character. This occurs after all deletion and translation is completed. Your original problem could have ...


19

You could use sed's w flag with either /dev/stderr, /dev/tty, /dev/fd/2 if supported on your system. E.g. with an input file like: foo first second: missing third: foo none here running sed -i '/foo/{ s//bar/g w /dev/stdout }' file outputs: bar first third: bar though file content was changed to: bar first second: missing third: bar none here So in ...


19

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


17

awk '{print $1, $2, "1"}' inputfile


15

Use sed. Here is an example: sed "s/current_date/`date +%Y%m%d`/" infile > copyfile


15

Using sed: sed '/[0-9]/!s/ //g' filename This would remove spaces on all lines that do not contain a digit. Using awk: awk '!/[0-9]/{gsub(" ", "", $0)};1' filename For removing the space only between the first two words (here using GNU sed for -r, use -E instead on BSDs): sed -r '/[0-9]/!s/([^ ]+) ([^ ]+)/\1\2/' filename


15

There are probably more efficient ways to do this. With that caveat: awk '{$35 = $35"$"; print}' infile > outfile


15

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 use WIDTH columns instead of 80 %echo 12345|fold -w1 1 2 3 4 5 "paste -sd\& -" - will merge the input lines together, using & as a ...


13

yw to yank your word, then move the cursor to the word you wish to replace and use "_dw to delete it, sending the text to the null register (so it doesn't overwrite the contents of the " register, where yanked/cut text goes by default), and then simply paste with p. You could use the following mapping to make things a little easier: nnoremap <leader>...


13

I would use translate command tr eg. tr ab ba < input_file


12

If you only want to replace individual characters, not longer strings, use sed -e 'y/ab/ba/' or the equivalent tr command from X Tian's answer. For arbitrary strings, you have to work harder: If there is any character that does certainly not occur in the input, such as # (even a control character will do), you can use something like sed -e 's/a/#/g;s/b/a/...


12

OK, a general solution. The following bash function requires 2k arguments; each pair consists of a placeholder and a replacement. It's up to you to quote the strings appropriately to pass them into the function. If the number of arguments is odd, an implicit empty argument will be added, which will effectively delete occurrences of the last placeholder. ...


12

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 filename


12

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 sequence from 0 to 9 and . up to the end of line. sed (golfed 4 bytes) sed -i 's/[^ ]*$/1/' file [^ ]*$ any char other than space, until end of line.


11

In Kate 3.8.5- Go to Settings -> Configure Kate -> Plugins and enable there Search & Replace. Then use the "Search and Replace" button that appears at the bottom of the Kate main window to find the desired functionality.


11

You can use rsync to do this: $ rsync -abviuzP src/ dest/ -a archive mode; equals -rlptgoD (no -H,-A,-X) -i turns on the itemized format, which shows more information than the default format -b makes rsync backup files that exist in both folders, appending ~ to the old file. You can control this suffix with --suffix .suf -u makes rsync transfer skip files ...


11

There are a few approaches using either tr, awk or sed TR: iconv -l | grep ISO |head -5 |tr '/' '-' AWK: iconv -l | awk '/ISO/{gsub("//","--"); print $0}' |head -5 SED: iconv -l | grep ISO |head -5 | sed 's/\//-/g' # or, to avoid needing to escape the backslashes: iconv -l | grep ISO |head -5 | sed 's#/#-#g'


11

Use sed sed 's/./&\&/g;s/.$//'


10

Took me a long time to find this solution using var replacement. All sed solutions did not work for me, as they either delete complete lines or replace incorrectly. FILE2=$(<file2) FILE1=$(<file1) echo "${FILE2//TEXT1/$FILE1}" Replaces all occurences of TEXT1 in file2 against content of file1. All other text remains untouched.


10

Turns out that this is a limitation of btrfs as of beginning of 2017. To get the filesystem mounted rw again, one needs to patch the kernel. I have not tried it though. I am planing to move away from btrfs because of this; one should not have to patch a kernel to be able to replace a faulty disk. Click on the following links for details: Kernel patch ...


10

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.jpg img_3.jpg $ $ prename -h Usage: prename [OPTION]... PERLEXPR FILE... Rename FILE(s) using PERLEXPR on each filename. -b, --backup make ...


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