Hot answers tagged

34

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


24

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


21

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


19

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


19

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


18

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


17

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.


15

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


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


13

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>


12

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


11

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


11

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


11

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


10

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


10

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


10

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


9

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.


9

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


9

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


8

egrep can save you the use of cat. In other words, create less processes (egrep vs cat+egrep) and use less buffers (pipe from cat to egrep vs no pipe). It is generally a good idea to limit the use of cat if you simply want to pass a file to a command that can read it on its own. With this said, the following command will remove comments, even if they are ...


8

You can type V to select the line, then p to replace it.


8

Yes, it's possible, \& can be used in replace expression to represent the entire match, similarly \#& can be used to represent the entire match as number. More concretely: M-x query-replace-regexp \b[0-9]+\b RETURN \,(+ 3 \#&) And a quote from the documentation You can use Lisp expressions to calculate parts of the replacement string. To ...


8

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


7

Check udev config files. A file like this: /etc/udev/rules.d/70-persistent-net.rules ties the NAME (ethX) to the Mac address. You probably have the old cards MAC tied to eth0. Remove its line and change the new card to eth0.


7

see http://stackoverflow.com/questions/5858200/sed-replace-every-nth-occurrence The solution uses awk rather than sed, but "use the right tool for the job". It may or may not be possible to do in sed but, even if it is, it will be a lot easier in a tool like awk or perl.


7

;; query-replace current word (defun qrc (replace-str) (interactive "sDo query-replace current word with: ") (forward-word) (let ((end (point))) (backward-word) (kill-ring-save (point) end) (query-replace (current-kill 0) replace-str) ))


7

Using perl: perl -ple 's/\s+//g unless /\d/' file


7

You can for example use this: $ awk '/H/{sub("H", "H"++v)}1' file 1562 first part 1563 H1 col3 H col4 1564 H2 col3 H col4 3241 H3 col3 H col4 3242 third part ... This looks for those lines containing H and replaces that H with H together with a variable we keep incrementing. Note you could use gsub() instead of sub() if you wanted to perform ...


7

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



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