Tag Info

Hot answers tagged

20

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


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


14

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


12

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.


12

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


11

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>


10

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


10

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


9

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


8

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


8

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


8

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


7

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


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

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


7

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


7

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


7

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


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


6

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.


6

Not a "Bash-only" answer, but perhaps useful: echo "$PWD///" | tr -s '/'


6

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


6

You will want to make use of sed's scripting capabilities to accomplish this. $ sed -e '/bar/ { N d }' sample1.txt Sample data: $ cat sample1.txt foo bar biz baz buz The "N" command appends the next line of input into the pattern space. This combined with the line from the pattern match (/bar/) will be the lines that you wish to delete. You can ...


6

I don't think that's possible, but a workaround might be to use perl instead: find . -type f | xargs perl -i -ne 's/abc/def/ && print STDERR' This will print the altered lines to standard error. For example: $ cat foo fooabcbar $ find . -type f | xargs perl -i -ne 's/abc/def/ && print STDERR' foodefbar You can also make this slightly ...


6

find . -name TheFileName -type f -exec sh -c 'for i do echo SomeText > "$i"; done' sh {} +


6

If there's only one occurrence of <ex>...</ex> per line: sed -e :1 -e 's@\(<ex>.*\)&\(.*</ex>\)@\1#\2@;t1' If there may be several occurrences and they don't nest (or they nest and you want to replace the & only in the deepest occurrences): sed ' s|_|_u|g # replace all underscores with "_u" s|(|_o|g # ...


5

Possible in "pure bash", but only with extglob active. $ shopt -s extglob $ A="////qsd/////sdfgfg//qsd//////" $ echo ${A//\/+(\/)/\/} /qsd/sdfgfg/qsd/


5

I'm quite certain this can't be done directly. However, I came up with a function for you. Put this in your ~/.vimrc: function! Toggle() s!^\(\s*/\?[^/\s]/\?\)!xxx//\1!e s!^\(\s*\)//!\1!e s!^xxx//!//!e endfunc (This will change any xxx// you already have at the beginning of a line into //, but I'd imagine this is a rare occurrence). You could ...


5

Try the following key sequence: c e Unix Esc (note the space character before "Unix") Here's a brief explanation: c: "change" command, similar to delete but ends in "insert" mode. e: from the cursor to the end of the following word. " Unix": the replacement text Esc: Return to command mode (always return to command mode!)


5

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



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