Commands matching search replace (68)

  • Given a file with the format of 'git log --pretty=short', search in last 100 commits for one with the same description. I used this when after a rebase I had to find out the new commit ids. The second sed replaces all special characters with dots so they don't mess up the grep later on. Show Sample Output

    cat /tmp/commit_list | { while read old_commit ; do msg="`git log --pretty=oneline $old_commit'^'..$old_commit | sed 's/[0-9a-f]* //' | sed 's/[^A-Za-z0-9]/./g'`"; git log --pretty=oneline HEAD@'{100}'..HEAD | grep "$msg" ; done ; }
    plexus · 2012-10-11 11:06:40 0
  • find . = will set up your recursive search. You can narrow your search to certain file by adding -name "*.ext" or limit buy using the same but add prune like -name "*.ext" -prune xargs =sets it up like a command line for each file find finds and will invoke the next command which is perl. perl = invoke perl -p sets up a while loop -i in place and the .bak will create a backup file like filename.ext.bak -e execute the following.... 's/ / /;' your basic substitute and replace.

    find . | xargs perl -p -i.bak -e 's/oldString/newString/;'
    RedFox · 2012-11-28 17:11:18 0
  • Does a search and replace across multiple files with a subgroup replacement.

    find . -name "*.txt" | xargs -n 1 perl -pi -w -e "s/text([0-9])/other\$1/g;"
    kennethjor · 2014-02-28 06:38:38 0
  • Replace the credentials to psql if necessary, and the my-query part with your query. Show Sample Output

    psql -U quassel quassel -c "SELECT message FROM backlog ORDER BY time DESC LIMIT 1000;" | grep my-query
    Tatsh · 2014-10-12 19:53:06 0
  • This sed command will search for in all lines of test.txt and replace comment symbol ";" . You can use it for other purpose also.

    sed -e '/ s/^;//' -i test.txt
    suyashjain · 2014-10-13 13:37:53 0
  • Use sed to search and replace pipes for tabs in file stream with backup

    sed -i.bak -e s/\|/\\t/g filepath.tsv
    johnmoxon · 2014-11-04 00:14:59 0
  • This is much easier to parse and do something else with (eg: automagically create ZFS vols) than anything else I've found. It also helps me keep track of which disks are which, for example, when I want to replace a disk, or image headers in different scenarios. Being able to match a disk to the kernels mapping of said drive the disks serial number is very helpful ls -l /dev/disk/by-id Normal `ls` command to list contents of /dev/disk/by-id grep -v "wwn-" Perform an inverse search - that is, only output non-matches to the pattern 'wwn-' egrep "[a-zA-Z]{3}$" A regex grep, looking for three letters and the end of a line (to filter out fluff) sed 's/\.\.\/\.\.\///' Utilize sed (stream editor) to remove all occurrences of "../../" sed -E 's/.*[0-9]{2}:[0-9]{2}\s//' Strip out all user and permission fluff. The -E option lets us use extended (modern) regex notation (larger control set) sed -E 's/->\ //' Strip out ascii arrows "-> " sort -k2 Sort the resulting information alphabetically, on column 2 (the disk letters) awk '{print $2,$1}' Swap the order of the columns so it's easier to read/utilize output from sed 's/\s/\t/' Replace the space between the two columns with a tab character, making the output more friendly For large ZFS pools, this made creating my vdevs immeasurably easy. By keeping track of which disks were in which slot (spreadsheet) via their serial numbers, I was able to then create my vols simply by copying and pasting the full output of the disk (not the letter) and pasting it into my command. Thereby allowing me to know exactly which disk, in which slot, was going into the vdev. Example command below. zpool create tank raidz2 -o ashift=12 ata-... ata-... ata-... ata-... ata-... ata-... Show Sample Output

    ls -l /dev/disk/by-id |grep -v "wwn-" |egrep "[a-zA-Z]{3}$" |sed 's/\.\.\/\.\.\///' |sed -E 's/.*[0-9]{2}:[0-9]{2}\s//' |sed -E 's/->\ //' |sort -k2 |awk '{print $2,$1}' |sed 's/\s/\t/'
    lig0n · 2015-01-25 19:29:40 0
  • This is great if you decide to change the name of a variable in multiple .java scripts and want to see a list of where it is used. Once in a text file you can easily sort the info by using find and replace to add /n to every ./ it finds to read your results more clearly. Note: grep will search the current directory. -r stands for recursive which will tell grep to search all directories and subdirectories.

    grep -r "word" >> "~/Desktop/filename.txt"
    hobopowers · 2015-05-06 12:49:53 0
  • Search a string in YouTube using GET command, parse the response with a suitable replace pattern and pipe its result to VLC Show Sample Output

    GET | sed -ne '/<a href="\/watch.*title="/s/.*<a href="\(\/watch[^"]*\)".* title="\([^"]*\)".*/https:\/\/\1/p' | vlc -
    plokij · 2016-10-28 23:55:59 0
  • lists the files found by find, waits for user input then uses xdg-open to open the selected file with the appropriate program. usage: findopen path expression [command] With the third optional input you can specify a command to use other than xdg-open, for example you could echo the filename to stdout then pipe it to another command. To get it to work for files with spaces it gets a bit messier... findopen() { files=( $(find "$1" -iname "$2" | tr ' ' '@') ); select file in "${files[@]//@/ }"; do ${3:-xdg-open} "$file"; break; done } You can replace the @ with any character that probably wont be in a file name.

    findopen() { local PS3="select file: "; select file in $(find "$1" -iname "$2"); do ${3:-xdg-open} $file; break; done }
    quigybo · 2010-02-28 02:28:59 0

  • -1
    find -name ".php" -exec perl -pi -e 's/search/replace/g/' {} \;
    unixmonkey8504 · 2010-03-02 14:18:44 0
  • a brief list of very common special characters in Dutch. Usefull for formatting Word source to html.

    %s/?/\&iuml;/ge | %s/?/\&#0233;/ge | %s/?/"/ge | %s/?/"/ge | %s/?/'/ge | %s/?/'/ge | %s/?/\&ecirc;/ge | %s/?/\&#0133;/ge | %s/?/\&#232;/ge | %s/?/\&#243;/ge | %s/?/\&ouml;/ge | %s/?/\&#0233;/ge | %s/?/\&ndash;/ge | %s/?/\&mdash;/ge
    miccaman · 2010-07-15 07:40:45 0
  • I used this to mass install a lot of perl stuff. Threw it together because I was feeling *especially* lazy. The 'perl' and the 'module' can be replaced with whatever you like.

    apt-cache search perl | grep module | awk '{print $1;}' | xargs sudo apt-get install -y
    captwheeto · 2010-11-26 14:00:39 0
  • If you add the bookmarklet to your browser's bookmarks with like say, the keyword 'cfu', you can for example type 'cfu hello' in the location bar and the %s gets replaced with 'hello'. The bookmarklet will convert the search text to base64 for use with the commandlinefu website and will take you there. Tested with Firefox. Show Sample Output

    echo "javascript:location.href=''+encodeURIComponent('%s')+'/'+btoa('%s')+'/sort-by-votes'"
    darkfader · 2011-03-07 22:01:46 0
  • Basic search and replaceEdit The :substitute command searches for a text pattern, and replaces it with a text string. There are many options, but these are what you probably want: :%s/foo/bar/g Find each occurrence of 'foo', and replace it with 'bar'. :%s/foo/bar/gc Change each 'foo' to 'bar', but ask for confirmation first. :%s/\/bar/gc Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation. :%s/foo/bar/gci Change each 'foo' (case insensitive) to 'bar'; ask for confirmation. :%s/foo/bar/gcI Change each 'foo' (case sensitive) to 'bar'; ask for confirmation. The g flag means global ? each occurrence in the line is changed, rather than just the first. DetailsEdit Search range: :s/foo/bar/g Change each 'foo' to 'bar' in the current line. :%s/foo/bar/g Change each 'foo' to 'bar' in all lines. :5,12s/foo/bar/g Change each 'foo' to 'bar' for all lines from line 5 to line 12 inclusive. :'a,'bs/foo/bar/g Change each 'foo' to 'bar' for all lines from mark a to mark b inclusive. :.,$s/foo/bar/g Change each 'foo' to 'bar' for all lines from the current line (.) to the last line ($) inclusive. :.,+2s/foo/bar/g Change each 'foo' to 'bar' for the current line (.) and the two next lines (+2). :%s/foo/bar/g Equivalent to :1,$s/foo/bar/g (change all lines). :g/^baz/s/foo/bar/g Change each 'foo' to 'bar' in each line starting with 'baz'. When searching: ., *, \, [, ], ^, and $ are metacharacters. +, ?, |, {, }, (, and ) must be escaped to use their special function. \/ is / (use backslash + forward slash to search for forward slash) \t is tab, \s is whitespace \n is newline, \r is CR (carriage return = Ctrl-M = ^M) \{#\} is used for repetition. /foo.\{2\} will match foo and the two following characters. The \ is not required on the closing } so /foo.\{2} will do the same thing. \(foo\) makes a backreference to foo. Parenthesis without escapes are literally matched. Here the \ is required for the closing \). When replacing: \r is newline, \n is a null byte (0x00). \& is ampersand (& is the text that matches the search pattern). \1 inserts the text of the first backreference. \2 inserts the second backreference, and so on. You can use other delimiters with substitute: :s# Save typing by using \zs and \ze to set the start and end of a pattern. For example, instead of: :s/Copyright 2007 All Rights Reserved/Copyright 2008 All Rights Reserved/ Use: :s/Copyright \zs2007\ze All Rights Reserved/2008/ Using the current word or registersEdit :%s//bar/g Replace each match of the last search pattern with 'bar'. For example, you might first place the cursor on the word foo then press * to search for that word. The above substitute would then change all words exactly matching 'foo' to 'bar'. :%s/foo//g Replace each occurrence of 'foo' with the word under the cursor. means that you press Ctrl-R then Ctrl-W. The word under the cursor will be inserted as though you typed it. :%s/foo//g Replace each occurrence of 'foo' with the WORD under the cursor (delimited by whitespace). means that you press Ctrl-R then Ctrl-A. The WORD under the cursor will be inserted as though you typed it. :%s/foo/a/g Replace each occurrence of 'foo' with the contents of register 'a'. a means that you press Ctrl-R then a. The contents of register 'a' will be inserted as though you typed it. :%s/foo/\=@a/g Replace each occurrence of 'foo' with the contents of register 'a'. \=@a is a reference to register 'a'. The contents of register 'a' is not shown in the command. This is useful if the register contains many lines of text. :%s////g Replace each match of the last search pattern with the / register (the last search pattern). After pressing Ctrl-R then / to insert the last search pattern (and before pressing Enter to perform the command), you could edit the text to make any required change. :%s/*/bar/g Replace all occurrences of the text in the system clipboard (in the * register) with 'bar' (see next example if multiline). On some systems, selecting text (in Vim or another application) is all that is required to place that text in the * register. :%s/a/bar/g Replace all occurrences of the text in register 'a' with 'bar'. a means that you press Ctrl-R then a. The contents of register 'a' will be inserted as though you typed it. Any newlines in register 'a' are inserted as ^M and are not found. The search works if each ^M is manually replaced with '\n' (two characters: backslash, 'n'). This replacement can be performed while you type the command: :%s/=substitute(@a,"\n",'\\n','g')/bar/g The "\n" (double quotes) represents the single character newline; the '\\n' (single quotes) represents two backslashes followed by 'n'. The substitute() function is evaluated by the = (Ctrl-R =) expression register; it replaces each newline with a single backslash followed by 'n'. The indicates that you press Enter to finish the = expression. See Paste registers in search or colon commands instead of using the clipboard. Additional examplesEdit :%s/foo/bar/ On each line, replace the first occurrence of "foo" with "bar". :%s/.*\zsfoo/bar/ On each line, replace the last occurrence of "foo" with "bar". :%s/\.*// On each line, delete the whole word "foo" and all following text (to end of line). :%s/\.\{5}// On each line, delete the whole word "foo" and the following five characters. :%s/\\zs.*// On each line, delete all text following the whole word "foo" (to end of line). :%s/.*\// On each line, delete the whole word "foo" and all preceding text (from beginning of line). :%s/.*\ze\// On each line, delete all the text preceding the whole word "foo" (from beginning of line). :%s/.*\(\\).*/\1/ On each line, delete all the text preceding and following the whole word "foo". Special casesEdit For substituting patterns with a corresponding case-sensitive text, Michael Geddes's keepcase plugin can be used, e.g.: :%SubstituteCase/\cHello/goodBye/g Substitute 'Hello hello helLo HELLO' by 'Goodbye goodbye goodBye GOODBYE' For changing the offsets in a patch file (line number of a block), this little snippet can be used: s/^@@ -\(\d\+\),\(\d\+\) +\(\d\+\),\(\d\+\) @@$/\="@@ -".eval(submatch(1)+offsetdiff).",".submatch(2)." +".eval(submatch(3)+offsetdiff).",".submatch(4)." @@"/g Useful when we want to strip some blocks from a patch, without patch having to complain about offset differences. Note Should try to make the expression more compact, but don't know how without having the possibility of modifying unwanted lines.

    anhstar · 2011-08-19 14:50:28 1
  • Yeah, there are many ways to do that. Doing with sed by using a for loop is my favourite, because these are two basic things in all *nix environments. Sed by default does not allow to save the output in the same files so we'll use mv to do that in batch along with the sed. Show Sample Output

    for files in $(ls -A directory_name); do sed 's/search/replaced/g' $files > $ && mv $ $files; done;
    bassu · 2009-05-07 20:13:07 6
  • This command will grep the entire directory looking for any files containing the list of files. This is useful for cleaning out your project of old static files that are no longer in use. Also ignores .svn directories for accurate counts. Replace 'static/images/' with the directory containing the files you want to search for. Show Sample Output

    ls -1 static/images/ | while read line; do echo -n $line' '[; grep -rc $line *|grep -v ".svn"|cut -d":" -f2|grep -vc 0| tr "\n" -d; echo -n ]; echo ; done
    psytek · 2009-03-20 20:33:36 1
  • replace apt-get with your distro's package manager. Where 'something' is the package name, and 'specific' is what you're specifically looking for. This helps if your query is 2+ words long. Show Sample Output

    apt-get search something | grep specific
    ProfOak · 2010-09-27 07:10:39 1
  •  < 1 2 3

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Check These Out

list block devices
Shows all block devices in a tree with descruptions of what they are.

Compress blank lines in VIM
This command will squeeze all consequent blank lines (including those with only space and tab charactes) to one. It will also empty the resulting line (remove the "|s/.*//" part if you don't need that).

Print diagram of user/groups
Parses /etc/group to "dot" format and pases it to "display" (imagemagick) to show a usefull diagram of users and groups (don't show empty groups).

list file descriptors opened by a process
Useful for examining hostile processes (backdoors,proxies)

Clean up after a poorly-formed tar file
These days, most software distributed in tar files will just contain a directory at the top level, but some tar files don't have this and can leave you with a mess of files in the current folder if you blindly execute $ tar zxvf something.tar.gz This command can help you clean up after such a mistake. However, note that this has the potential to do bad things if someone has been *really* nasty with filenames.

Search and replace text in all php files with ruby
Search for the string "search" and replace it with the string "replace", on all files with the extension php in the curret folder. Do also a backup of each file with the extension "bkp".

Insert the last argument of the previous command

Check if a command is available in your system
Usefull to detect if a commad that your script relies upon is properly installed in your box, you can use it as a function function is_program_installed() { type "$1" >/dev/null } Invoke it and check the execution code is_program_installed "dialog" if [ ! $? -eq 0 ]; then echo "dialog is not installed" exit 1 fi

Check motherboard manufacturer, product name, version and serial number
Very slick way to check your motherboard manufacturer, product name, version and serial number.

Recursive find and replace file extension / suffix (mass rename files)
Find recursively all files in ~/Notes with the extension '.md' and pipe that via xargs to rename command, which will replace every '.md' to '.txt' in this example (existing files will not be overwritten).

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: