Showing posts with label command line. Show all posts
Showing posts with label command line. Show all posts

sed one-liners


sed 's/ Телефон: /\t/' filename             # basic search and replace
sed 's/ [-] .*//' filename                       # put tricky chars in []
sed 's/\&amp/\&/' filename                  # example of escaped char
sed 's/.\{1\}/& /g' filename                   # insert 1 space between each char
sed 's/.\{2\}/& /g' filename                   # insert 2 spaces between each char
sed 's/\(.*\)\/word$/word\1/' filename     # search and replace with a captured pattern () on LHS and \1 on RHS
sed 's/word/&\n/' filename                   # search and replace insert a newline
sed '/^\s*$/d' filename                         # delete blank line




Good resources for one-liners with examples 
http://sed.sourceforge.net/sed1line.txt

using "screen" command to handle multiple sessions

from http://www.cyberciti.biz/tips/linux-screen-command-howto.html


$ screen -S 1
CTRL+a, c            -- create another screen window
CTRL+a, n            -- switch to next screen window I've got open




  • To list all windows use the command CTRL+a followed by " key (first hit CTRL+a, releases both keys and press " ).
  • To switch to window by number use the command CTRL+a followed by ' (first hit CTRL+a, releases both keys and press ' it will prompt for window number).

Common screen commands

screen commandTask
Ctrl+a cCreate new window
Ctrl+a kKill the current window / session
Ctrl+a wList all windows
Ctrl+a 0-9Go to a window numbered 0 9, use Ctrl+a w to see number
Ctrl+a Ctrl+aToggle / switch between the current and previous window
Ctrl+a SSplit terminal horizontally into regions and press Ctrl+a c to create new window there
Ctrl+a :resizeResize region
Ctrl+a :fitFit screen size to new terminal size. You can also hit Ctrl+a F for the the same task
Ctrl+a :removeRemove / delete region. You can also hit Ctrl+a X for the same taks
Ctrl+a tabMove to next region
Ctrl+a D (Shift-d)Power detach and logout
Ctrl+a dDetach but keep shell window open
Ctrl-a Ctrl-\Quit screen
Ctrl-a ?Display help screen i.e. display a list of commands

Suggested readings:

See screen command man page for further details:
man screen


awk

specify tab-delimited columns
awk -F'\t' '{ print $1}' file

print column $13 and column $10 where $13 matches "MY"
awk -F"\t" '$13 ~ /MY/ {print $13"\t"$10}' | less

awk -F"\t" '$7 == "restaurant" ' japan.osm.poi  > restaurants


awk '{ print \$1} ' file,   print 1st column, escape $ when in perl system command



not really sure what this does.....

awk -F "\"*,\"*" '{print $3"\t"$5}' jigyosyo.csv

my .bashrc file


So you don't have to do source ~/.bashrc every time you open a terminal, put it in ~/.bash_profile.
~> cat .bash_profile
source ~/.bashrc


Things I like to have in my .bashrc file are:
.... in progress

file transfer

If you're transferring between Windows and linux, use winSCP.

If you're transferring linux to linux use scp like this:

>> scp ......

Perl one-liners


  • Read line, substitute regex and print line.
    • perl  -ne '{$l = $_; $l=~ s/dede/frfr/g; print $l;}' input1.txt
    • perl –ne ‘$l = $l =~ s/dede/frfr/g; print $l;}’ input1.txt input2.txt
    • -e means “execute” and –n makes it loop line by line
    • Multiple input files are fine.
  • Snazzier replacement per line

    • perl  -pe 's/a/b/g' < input > newfile
  • Read line and print the (1-indexed) line number and line.
    • perl –ne ‘print “$. – $_”’ input1.txt
    • output looks like:
      • 1 – My first line of input text
      • 2 – My second line of input text
  • With no input, print 0 to 999
    • perl -e ' for ($i=0;$i<999;$i++) { print "$i\n"}  '
THESE DON'T WORK WELL WITH sed
perl -pe "s/\&apos/'/g" < 2.2_cleaner > new
perl -pe 's/\&quot/"/g' < 2.2_cleaner > new

using tar to compress and extract files

 

Using tar to compress file(s)

  • tar –czvf destination.tar.gz source/*

Using tar to uncompress/extract file(s) 

  • tar –xzvf destination.tar.gz
where:
c = create a new tar file
v = verbose , display file to compress or uncompress
f = create the tar file with filename provided as the argument
z = use gzip to zip it
x = extract file