Commands matching search replace (67)

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


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


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


    0
    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 7
  • This sed command will search for 4.2.2.2 in all lines of test.txt and replace comment symbol ";" . You can use it for other purpose also.


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


    0
    sed -i.bak -e s/\|/\\t/g filepath.tsv
    johnmoxon · 2014-11-04 00:14:59 9
  • 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


    0
    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 8
  • 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.


    0
    grep -r "word" >> "~/Desktop/filename.txt"
    hobopowers · 2015-05-06 12:49:53 9
  • 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


    0
    GET https://www.youtube.com/results?search_query=shaun+the+sheep | sed -ne '/<a href="\/watch.*title="/s/.*<a href="\(\/watch[^"]*\)".* title="\([^"]*\)".*/https:\/\/www.youtube.com\1/p' | vlc -
    plokij · 2016-10-28 23:55:59 17
  • 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.


    -1
    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 3

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


    -1
    %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 4
  • 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.


    -2
    apt-cache search perl | grep module | awk '{print $1;}' | xargs sudo apt-get install -y
    captwheeto · 2010-11-26 14:00:39 3
  • 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


    -2
    echo "javascript:location.href='http://www.commandlinefu.com/commands/matching/'+encodeURIComponent('%s')+'/'+btoa('%s')+'/sort-by-votes'"
    darkfader · 2011-03-07 22:01:46 4
  • 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#http://www.example.com/index.html#http://example.com/# 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.


    -2
    :%s/foo/bar/g
    anhstar · 2011-08-19 14:50:28 7
  • 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


    -4
    for files in $(ls -A directory_name); do sed 's/search/replaced/g' $files > $files.new && mv $files.new $files; done;
    bassu · 2009-05-07 20:13:07 48
  • 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


    -5
    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 14
  • 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


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

What's this?

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

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Rename files in batch

Download an Entire website with wget

Set laptop display brightness
Run as root. Path may vary depending on laptop model and video card (this was tested on an Acer laptop with ATI HD3200 video). $ cat /proc/acpi/video/VGA/LCD/brightness to discover the possible values for your display.

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

a short counter
Maybe you know shorter ?

Log colorizer for OSX (ccze alternative)
Download colorizer by @raszi @ http://github.com/raszi/colorize

add all files not under version control to repository
This should handle whitespaces well and will not get confused if your filenames have "?" in them

back ssh from firewalled hosts
host B (you) redirects a modem port (62220) to his local ssh. host A is a remote machine (the ones that issues the ssh cmd). once connected port 5497 is in listening mode on host B. host B just do a ssh 127.0.0.1 -p 5497 -l user and reaches the remote host'ssh. This can be used also for vnc and so on.

Happy Days
AFAIR this is the wording ;)


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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: