Tags: bash

Sort by: Date / Title /

  1. 4 years ago by spirit
    1. mysqldump –add-drop-table –extended-insert –force –log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost "mysql -uUSER -pPASS NEW_DB_NAME"
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2167"></script>
  2. 4 years ago by spirit
    1. export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER "$(history 1)"|logger -p user.alert -t bash_history'
    2. readonly PROMPT_COMMAND
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2153"></script>
  3. 4 years ago by spirit
    1. FILE SPACING:
    2.  
    3.  # double space a file
    4.  sed G
    5.  
    6.  # double space a file which already has blank lines in it. Output file
    7.  # should contain no more than one blank line between lines of text.
    8.  sed '/^$/d;G'
    9.  
    10.  # triple space a file
    11.  sed 'G;G'
    12.  
    13.  # undo double-spacing (assumes even-numbered lines are always blank)
    14.  sed 'n;d'
    15.  
    16.  # insert a blank line above every line which matches "regex"
    17.  sed '/regex/{x;p;x;}'
    18.  
    19.  # insert a blank line below every line which matches "regex"
    20.  sed '/regex/G'
    21.  
    22.  # insert a blank line above and below every line which matches "regex"
    23.  sed '/regex/{x;p;x;G;}'
    24.  
    25. NUMBERING:
    26.  
    27.  # number each line of a file (simple left alignment). Using a tab (see
    28.  # note on 't' at end of file) instead of space will preserve margins.
    29.  sed = filename | sed 'N;s/n/t/'
    30.  
    31.  # number each line of a file (number on left, right-aligned)
    32.  sed = filename | sed 'N; s/^/     /; s/ *(.{6,})n/1  /'
    33.  
    34.  # number each line of file, but only print numbers if line is not blank
    35.  sed '/./=' filename | sed '/./N; s/n/ /'
    36.  
    37.  # count lines (emulates "wc -l")
    38.  sed -n '$='
    39.  
    40. TEXT CONVERSION AND SUBSTITUTION:
    41.  
    42.  # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
    43.  sed 's/.$//'               # assumes that all lines end with CR/LF
    44.  sed 's/^M$//'              # in bash/tcsh, press Ctrl-V then Ctrl-M
    45.  sed 's/x0D$//'            # works on ssed, gsed 3.02.80 or higher
    46.  
    47.  # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
    48.  sed "s/$/`echo -e r`/"            # command line under ksh
    49.  sed 's/$'"/`echo r`/"             # command line under bash
    50.  sed "s/$/`echo r`/"               # command line under zsh
    51.  sed 's/$/r/'                        # gsed 3.02.80 or higher
    52.  
    53.  # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
    54.  sed "s/$//"                          # method 1
    55.  sed -n p                             # method 2
    56.  
    57.  # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
    58.  # Can only be done with UnxUtils sed, version 4.0.7 or higher. The
    59.  # UnxUtils version can be identified by the custom "--text" switch
    60.  # which appears when you use the "--help" switch. Otherwise, changing
    61.  # DOS newlines to Unix newlines cannot be done with sed in a DOS
    62.  # environment. Use "tr" instead.
    63.  sed "s/r//" infile >outfile         # UnxUtils sed v4.0.7 or higher
    64.  tr -d r <infile >outfile            # GNU tr version 1.22 or higher
    65.  
    66.  # delete leading whitespace (spaces, tabs) from front of each line
    67.  # aligns all text flush left
    68.  sed 's/^[ t]*//'                    # see note on 't' at end of file
    69.  
    70.  # delete trailing whitespace (spaces, tabs) from end of each line
    71.  sed 's/[ t]*$//'                    # see note on 't' at end of file
    72.  
    73.  # delete BOTH leading and trailing whitespace from each line
    74.  sed 's/^[ t]*//;s/[ t]*$//'
    75.  
    76.  # insert 5 blank spaces at beginning of each line (make page offset)
    77.  sed 's/^/     /'
    78.  
    79.  # align all text flush right on a 79-column width
    80.  sed -e :a -e 's/^.{1,78}$/ &/;ta'  # set at 78 plus 1 space
    81.  
    82.  # center all text in the middle of 79-column width. In method 1,
    83.  # spaces at the beginning of the line are significant, and trailing
    84.  # spaces are appended at the end of the line. In method 2, spaces at
    85.  # the beginning of the line are discarded in centering the line, and
    86.  # no trailing spaces appear at the end of lines.
    87.  sed  -e :a -e 's/^.{1,77}$/ & /;ta'                     # method 1
    88.  sed  -e :a -e 's/^.{1,77}$/ &/;ta' -e 's/( *)1/1/'  # method 2
    89.  
    90.  # substitute (find and replace) "foo" with "bar" on each line
    91.  sed 's/foo/bar/'             # replaces only 1st instance in a line
    92.  sed 's/foo/bar/4'            # replaces only 4th instance in a line
    93.  sed 's/foo/bar/g'            # replaces ALL instances in a line
    94.  sed 's/(.*)foo(.*foo)/1bar2/' # replace the next-to-last case
    95.  sed 's/(.*)foo/1bar/'            # replace only the last case
    96.  
    97.  # substitute "foo" with "bar" ONLY for lines which contain "baz"
    98.  sed '/baz/s/foo/bar/g'
    99.  
    100.  # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
    101.  sed '/baz/!s/foo/bar/g'
    102.  
    103.  # change "scarlet" or "ruby" or "puce" to "red"
    104.  sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'   # most seds
    105.  gsed 's/scarlet|ruby|puce/red/g'                # GNU sed only
    106.  
    107.  # reverse order of lines (emulates "tac")
    108.  # bug/feature in HHsed v1.5 causes blank lines to be deleted
    109.  sed '1!G;h;$!d'               # method 1
    110.  sed -n '1!G;h;$p'             # method 2
    111.  
    112.  # reverse each character on the line (emulates "rev")
    113.  sed '/n/!G;s/(.)(.*n)/&21/;//D;s/.//'
    114.  
    115.  # join pairs of lines side-by-side (like "paste")
    116.  sed '$!N;s/n/ /'
    117.  
    118.  # if a line ends with a backslash, append the next line to it
    119.  sed -e :a -e '/$/N; s/n//; ta'
    120.  
    121.  # if a line begins with an equal sign, append it to the previous line
    122.  # and replace the "=" with a single space
    123.  sed -e :a -e '$!N;s/n=/ /;ta' -e 'P;D'
    124.  
    125.  # add commas to numeric strings, changing "1234567" to "1,234,567"
    126.  gsed ':a;s/B[0-9]{3}>/,&/;ta'                     # GNU sed
    127.  sed -e :a -e 's/(.*[0-9])([0-9]{3})/1,2/;ta'  # other seds
    128.  
    129.  # add commas to numbers with decimal points and minus signs (GNU sed)
    130.  gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/12,3/g;ta'
    131.  
    132.  # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
    133.  gsed '0~5G'                  # GNU sed only
    134.  sed 'n;n;n;n;G;'             # other seds
    135.  
    136. SELECTIVE PRINTING OF CERTAIN LINES:
    137.  
    138.  # print first 10 lines of file (emulates behavior of "head")
    139.  sed 10q
    140.  
    141.  # print first line of file (emulates "head -1")
    142.  sed q
    143.  
    144.  # print the last 10 lines of a file (emulates "tail")
    145.  sed -e :a -e '$q;N;11,$D;ba'
    146.  
    147.  # print the last 2 lines of a file (emulates "tail -2")
    148.  sed '$!N;$!D'
    149.  
    150.  # print the last line of a file (emulates "tail -1")
    151.  sed '$!d'                    # method 1
    152.  sed -n '$p'                  # method 2
    153.  
    154.  # print the next-to-the-last line of a file
    155.  sed -e '$!{h;d;}' -e x              # for 1-line files, print blank line
    156.  sed -e '1{$q;}' -e '$!{h;d;}' -e x  # for 1-line files, print the line
    157.  sed -e '1{$d;}' -e '$!{h;d;}' -e x  # for 1-line files, print nothing
    158.  
    159.  # print only lines which match regular expression (emulates "grep")
    160.  sed -n '/regexp/p'           # method 1
    161.  sed '/regexp/!d'             # method 2
    162.  
    163.  # print only lines which do NOT match regexp (emulates "grep -v")
    164.  sed -n '/regexp/!p'          # method 1, corresponds to above
    165.  sed '/regexp/d'              # method 2, simpler syntax
    166.  
    167.  # print the line immediately before a regexp, but not the line
    168.  # containing the regexp
    169.  sed -n '/regexp/{g;1!p;};h'
    170.  
    171.  # print the line immediately after a regexp, but not the line
    172.  # containing the regexp
    173.  sed -n '/regexp/{n;p;}'
    174.  
    175.  # print 1 line of context before and after regexp, with line number
    176.  # indicating where the regexp occurred (similar to "grep -A1 -B1")
    177.  sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
    178.  
    179.  # grep for AAA and BBB and CCC (in any order)
    180.  sed '/AAA/!d; /BBB/!d; /CCC/!d'
    181.  
    182.  # grep for AAA and BBB and CCC (in that order)
    183.  sed '/AAA.*BBB.*CCC/!d'
    184.  
    185.  # grep for AAA or BBB or CCC (emulates "egrep")
    186.  sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d    # most seds
    187.  gsed '/AAA|BBB|CCC/!d'                        # GNU sed only
    188.  
    189.  # print paragraph if it contains AAA (blank lines separate paragraphs)
    190.  # HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
    191.  sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
    192.  
    193.  # print paragraph if it contains AAA and BBB and CCC (in any order)
    194.  sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'
    195.  
    196.  # print paragraph if it contains AAA or BBB or CCC
    197.  sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
    198.  gsed '/./{H;$!d;};x;/AAA|BBB|CCC/b;d'         # GNU sed only
    199.  
    200.  # print only lines of 65 characters or longer
    201.  sed -n '/^.{65}/p'
    202.  
    203.  # print only lines of less than 65 characters
    204.  sed -n '/^.{65}/!p'        # method 1, corresponds to above
    205.  sed '/^.{65}/d'            # method 2, simpler syntax
    206.  
    207.  # print section of file from regular expression to end of file
    208.  sed -n '/regexp/,$p'
    209.  
    210.  # print section of file based on line numbers (lines 8-12, inclusive)
    211.  sed -n '8,12p'               # method 1
    212.  sed '8,12!d'                 # method 2
    213.  
    214.  # print line number 52
    215.  sed -n '52p'                 # method 1
    216.  sed '52!d'                   # method 2
    217.  sed '52q;d'                  # method 3, efficient on large files
    218.  
    219.  # beginning at line 3, print every 7th line
    220.  gsed -n '3~7p'               # GNU sed only
    221.  sed -n '3,${p;n;n;n;n;n;n;}' # other seds
    222.  
    223.  # print section of file between two regular expressions (inclusive)
    224.  sed -n '/Iowa/,/Montana/p'             # case sensitive
    225.  
    226. SELECTIVE DELETION OF CERTAIN LINES:
    227.  
    228.  # print all of file EXCEPT section between 2 regular expressions
    229.  sed '/Iowa/,/Montana/d'
    230.  
    231.  # delete duplicate, consecutive lines from a file (emulates "uniq").
    232.  # First line in a set of duplicate lines is kept, rest are deleted.
    233.  sed '$!N; /^(.*)n1$/!P; D'
    234.  
    235.  # delete duplicate, nonconsecutive lines from a file. Beware not to
    236.  # overflow the buffer size of the hold space, or else use GNU sed.
    237.  sed -n 'G; s/n/&&/; /^([ -~]*n).*n1/d; s/n//; h; P'
    238.  
    239.  # delete all lines except duplicate lines (emulates "uniq -d").
    240.  sed '$!N; s/^(.*)n1$/1/; t; D'
    241.  
    242.  # delete the first 10 lines of a file
    243.  sed '1,10d'
    244.  
    245.  # delete the last line of a file
    246.  sed '$d'
    247.  
    248.  # delete the last 2 lines of a file
    249.  sed 'N;$!P;$!D;$d'
    250.  
    251.  # delete the last 10 lines of a file
    252.  sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
    253.  sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
    254.  
    255.  # delete every 8th line
    256.  gsed '0~8d'                           # GNU sed only
    257.  sed 'n;n;n;n;n;n;n;d;'                # other seds
    258.  
    259.  # delete lines matching pattern
    260.  sed '/pattern/d'
    261.  
    262.  # delete ALL blank lines from a file (same as "grep '.' ")
    263.  sed '/^$/d'                           # method 1
    264.  sed '/./!d'                           # method 2
    265.  
    266.  # delete all CONSECUTIVE blank lines from file except the first; also
    267.  # deletes all blank lines from top and end of file (emulates "cat -s")
    268.  sed '/./,/^$/!d'          # method 1, allows 0 blanks at top, 1 at EOF
    269.  sed '/^$/N;/n$/D'        # method 2, allows 1 blank at top, 0 at EOF
    270.  
    271.  # delete all CONSECUTIVE blank lines from file except the first 2:
    272.  sed '/^$/N;/n$/N;//D'
    273.  
    274.  # delete all leading blank lines at top of file
    275.  sed '/./,$!d'
    276.  
    277.  # delete all trailing blank lines at end of file
    278.  sed -e :a -e '/^n*$/{$d;N;ba' -e '}'  # works on all seds
    279.  sed -e :a -e '/^n*$/N;/n$/ba'        # ditto, except for gsed 3.02.*
    280.  
    281.  # delete the last line of each paragraph
    282.  sed -n '/^$/{p;h;};/./{x;/./p;}'
    283.  
    284. SPECIAL APPLICATIONS:
    285.  
    286.  # remove nroff overstrikes (char, backspace) from man pages. The 'echo'
    287.  # command may need an -e switch if you use Unix System V or bash shell.
    288.  sed "s/.`echo b`//g"    # double quotes required for Unix environment
    289.  sed 's/.^H//g'             # in bash/tcsh, press Ctrl-V and then Ctrl-H
    290.  sed 's/.x08//g'           # hex expression for sed 1.5, GNU sed, ssed
    291.  
    292.  # get Usenet/e-mail message header
    293.  sed '/^$/q'                # deletes everything after first blank line
    294.  
    295.  # get Usenet/e-mail message body
    296.  sed '1,/^$/d'              # deletes everything up to first blank line
    297.  
    298.  # get Subject header, but remove initial "Subject: " portion
    299.  sed '/^Subject: */!d; s///;q'
    300.  
    301.  # get return address header
    302.  sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
    303.  
    304.  # parse out the address proper. Pulls out the e-mail address by itself
    305.  # from the 1-line return address header (see preceding script)
    306.  sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'
    307.  
    308.  # add a leading angle bracket and space to each line (quote a message)
    309.  sed 's/^/> /'
    310.  
    311.  # delete leading angle bracket & space from each line (unquote a message)
    312.  sed 's/^> //'
    313.  
    314.  # remove most HTML tags (accommodates multiple-line tags)
    315.  sed -e :a -e 's/<[^>]*>//g;/</N;//ba'
    316.  
    317.  # extract multi-part uuencoded binaries, removing extraneous header
    318.  # info, so that only the uuencoded portion remains. Files passed to
    319.  # sed must be passed in the proper order. Version 1 can be entered
    320.  # from the command line; version 2 can be made into an executable
    321.  # Unix shell script. (Modified from a script by Rahul Dhesi.)
    322.  sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode   # vers. 1
    323.  sed '/^end/,/^begin/d' "$@" | uudecode                    # vers. 2
    324.  
    325.  # sort paragraphs of file alphabetically. Paragraphs are separated by blank
    326.  # lines. GNU sed uses v for vertical tab, or any unique char will do.
    327.  sed '/./{H;d;};x;s/n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/n/g'
    328.  gsed '/./{H;d};x;y/n/v/' file | sort | sed '1s/v//;y/v/n/'
    329.  
    330.  # zip up each .TXT file individually, deleting the source file and
    331.  # setting the name of each .ZIP file to the basename of the .TXT file
    332.  # (under DOS: the "dir /b" switch returns bare filenames in all caps).
    333.  echo @echo off >zipup.bat
    334.  dir /b *.txt | sed "s/^(.*).TXT/pkzip -mo 1 1.TXT/" >>zipup.bat
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2151"></script>
  4. sponsorised links
  5. 5 years ago by spirit
    1. echo $((RANDOM%256)).$((RANDOM%256)).$((RANDOM%256)).$((RANDOM%256))
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2145"></script>
  6. 5 years ago by spirit
    1. date +%s
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2144"></script>
  7. 6 years ago by spirit
    1. #Linux
    2. for ip in $(seq 1 254); do ping -c 1 192.168.1.$ip>/dev/null; [ $? -eq 0 ] && echo "192.168.1.$ip UP" || : ; done
    3.  
    4. #Windows:
    5. for /L %I in (1,1,254) DO ping -w 30 -n 1 192.168.1.%I | find "Reply"
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2130"></script>
  8. 6 years ago by cyo
    1. curl -I http://bit.ly/aBcDeF | grep '^Location: ' | awk '{ print $2; }'
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2116"></script>
  9. 6 years ago by spirit
    find / -mtime 2 -o -ctime 2
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2113"></script>
  10. 6 years ago by spirit
    1. find / -perm -0002 -type d -print
    2. find / -perm -0002 -type f -print
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2112"></script>
  11. 6 years ago by spirit
    Great for finding effects of make install
    1. find / -cmin -5
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2111"></script>

First / Previous / Next / Last / Page 1 of 10 (91 posteets)