The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Hot answers tagged


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 (...


Append line after match sed '/\[option\]/a Hello World' input Insert line before match sed '/\[option\]/i Hello World' input Additionally you can take backup and edit input file in-place using -i.bkp option to sed


If you have GNU sed (so non-embedded Linux or Cygwin): sed '/bar/,+1 d' If you have bar on two consecutive lines, this will delete the second line without analyzing it. For example, if you have a 3-line file bar/bar/foo, the foo line will stay.


\0 is the whole match. To use only part of it you need to set it like this and use \1 .s/(\([0-9]*\))/{\1}/ More detailed instruction you can find here or in vim help.


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


Yes, it is possible with sed: sed '/pattern/a some text here' filename An example: $ cat test foo bar option baz $ sed '/option/a insert text here' test foo bar option insert text here baz $


You can use: sed -e '/^;/d' php.ini


Incremental search has this feature, but the replace functions don't. Fortunately, incremental search does have a way to switch to replace mode once you've selected a search term. So: Press C-s to switch to incremental search mode Press C-w to yank the current word into the search buffer You can keep pressing it to append multiple words, and you can also ...


& 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 (...


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 ...


You don't need to pipe a file thru grep, grep takes filename(s) as command line args. grep -v '^#' file1 file2 file3 will print all lines EXCEPT those that begin with a # char. you can change the comment char to whatever you wish. If you have more than one comment char (assuming its at the beginning of a line) egrep -v '^(;|#|//)' filelist


Here's a sed script solution (easier on the eyes than trying to get it into one line on the command line): /<TEXT1>/ { r File1 d } Running it: $ sed -f script.sed File2 /home/user1/ /home/user2/bin /home/user1/a/b/c <TEXT2>


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 ...


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.


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 ...


If bar may occur on consecutive lines, you could do: awk '/bar/{n=2}; n {n--; next}; 1' < infile > outfile which can be adapted to delete more than 2 lines by changing the 2 above with the number of lines to delete including the matching one. If not, it's easily done in sed with @MichaelRollins' solution or: sed '/bar/,/^/d' < infile > ...


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 ...


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


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 ...


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


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


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 separator paste -...


sed 's/pattern/replacement/2' Will replace the second occurrence on every line that has the pattern. if you have GNU sed: sed '1~2N ; s/pattern/replacement/2' Starting from line one 1, the line after it will be added to the pattern space N then the s command will replace the second occurrence of the pattern. then sed will move two lines down ~2 and ...


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


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


I am not fluent in sed, but it is easy to do so in awk: awk '/bar/{getline;next} 1' foo.txt The awk script reads: for a line containing bar, get the next line (getline), then skip all subsequent processing (next). The 1 pattern at the end prints the remaining lines. Update As pointed out in the comment, the above solution did not work with consecutive ...


sed expects a basic regular expression (BRE). \s is not a standard special construct in a BRE (nor in an ERE, for that matter), this is an extension of some languages, in particular Perl (which many others imitate). In sed, depending on the implementation, \s either stands for the literal string \s or for the literal character s. In your implementation, it ...


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>...


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/...


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.

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