Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Aniline

« Newer Snippets
Older Snippets »
Showing 1-10 of 18 total  RSS 

SED by itself

//SED examples

   1  
   2  
   3  grep widget mlb | awk '{print $3}' | sed 's/-->/ /g'
   4  
   5  sed sub --> with blank space g= global
   6  
   7   # substitute "foo" with "bar" ONLY for lines which contain "baz"
   8   sed '/baz/s/foo/bar/g'
   9  
  10   # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
  11   sed '/baz/!s/foo/bar/g'
  12  
  13   # change "scarlet" or "ruby" or "puce" to "red"
  14   sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'   # most seds
  15   gsed 's/scarlet\|ruby\|puce/red/g'      
  16  
  17  Substitute newlines for &
  18  sed -e 's/&/\n/g' test.txt 

AWK more of it.

Badly formatted post broke our parser. Please edit!

RSYNC

// RSYNC usage

   1  
   2  rsync -e ssh atayebali@qafeed1:/usr/local/aolserver/servers/fs/pages/livexml/MLB/MLB_TRANS.XML rysnc_test.xml
   3  
   4  rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
   5    or   rsync [OPTION]... [USER@]HOST:SRC DEST
   6    or   rsync [OPTION]... SRC [SRC]... DEST
   7    or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
   8    or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
   9    or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
  10    or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  11  SRC on single-colon remote HOST will be expanded by remote shell
  12  SRC on server remote HOST may contain shell wildcards or multiple
  13    sources separated by space as long as they have same top-level
  14  
  15  copying folders
  16  
  17  rsync -e ssh -r atayebali@dev.foxsports.com:/path folder/

All kinds of code

Case Switch
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	echo "enter something>>" 
   5  4	read var 
   6  5	 
   7  6	case $var in 
   8  7	1 ) echo "1" ;; 
   9  8	2 ) echo "2" ;; 
  10  9	an* ) echo "not" ;; 
  11  10	*tom ) echo "wow" ;; 
  12  11	*) echo "end" 
  13  12	esac 
  14  	view plain | print | ?
  15  #!/bin/bash echo "enter something>>" read var case $var in 1 ) echo "1" ;; 2 ) echo "2" ;; an* ) echo "not" ;; *tom ) echo "wow" ;; *) echo "end" esac

Find Files
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	find . -name "*" -size 0k > file1.txt 
   5  4	sed -e 's/\.\///g' file1.txt > file2.txt 
   6  5	for line in `cat 0sizenuggets1.txt`; do ls -l $line; done > finalResult.txt 
   7  6	uuencode finalResult.txt finalresult.txt | mail -s "size 0 nuggets" atayebali@foxsports.com 
   8  7	rm file1.txt file2.txt 
   9  	view plain | print | ?
  10  #!/bin/bash find . -name "*" -size 0k > file1.txt sed -e 's/\.\///g' file1.txt > file2.txt for line in `cat 0sizenuggets1.txt`; do ls -l $line; done > finalResult.txt uuencode finalResult.txt finalresult.txt | mail -s "size 0 nuggets" atayebali@foxsports.com rm file1.txt file2.txt

Get Time
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	wget -O tf http://nist.time.gov/timezone.cgi?Pacific/d/-8 
   5  4	grep '>[0-9][0-9]:[0-9][0-9]'  tf | cut -c63-70 
   6  5	echo "PST" 
   7  6	rm tf 
   8  	view plain | print | ?
   9  #!/bin/bash wget -O tf http://nist.time.gov/timezone.cgi?Pacific/d/-8 grep '>[0-9][0-9]:[0-9][0-9]' tf | cut -c63-70 echo "PST" rm tf

If Statement Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	echo "$1" 
   5  4	 
   6  5	if [ "$1" == hello ]; then 
   7  6	        echo "hello there" 
   8  7	else 
   9  8	        echo "By nice" 
  10  9	fi 
  11  10	 
  12  11	checking for directories 
  13  12	 
  14  13	if [ -d "DB/" ] 
  15  14	 then 
  16  15	        rm -rf DB 
  17  16	        echo "killing DB folder" 
  18  17	 fi 
  19  18	 
  20  19	checking for files 
  21  20	if [ -f "DB/" ] 
  22  21	 then 
  23  22	        rm -rf DB 
  24  23	        echo "killing DB folder" 
  25  24	 fi 
  26  	view plain | print | ?
  27  #!/bin/bash echo "$1" if [ "$1" == hello ]; then echo "hello there" else echo "By nice" fi checking for directories if [ -d "DB/" ] then rm -rf DB echo "killing DB folder" fi checking for files if [ -f "DB/" ] then rm -rf DB echo "killing DB folder" fi

Sed Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	for line in `ls` 
   5  4	do 
   6  5	        sed -e 's/Spring/Regular Season/g' line > tmp 
   7  6	        mv tmp line 
   8  7	        echo -n $line 
   9  8	        grep Regular $line 
  10  9	done 
  11  	view plain | print | ?
  12  #!/bin/bash for line in `ls` do sed -e 's/Spring/Regular Season/g' line > tmp mv tmp line echo -n $line grep Regular $line done

While Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	echo "enter number: " 
   5  4	read var 
   6  5	 
   7  6	while [ $var -le 10 ]; do 
   8  7	echo "var is $var" 
   9  8	var=$((var + 1)) 
  10  9	done 
  11  	view plain | print | ?
  12  #!/bin/bash echo "enter number: " read var while [ $var -le 10 ]; do echo "var is $var" var=$((var + 1)) done

Email Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	function email 
   5  4	{ 
   6  5	echo "this is a test" | mail -s "test" "$1" 
   7  6	} 
   8  7	 
   9  8	if [ "$1" = "" ]; then 
  10  9	echo "nothing in param" 
  11  10	else 
  12  11	echo "sending mail" 
  13  12	 
  14  13	#emailing attachment 
  15  14	#uuencode statsNbaAllStarSim statssim | mail -s "test attachement" atayebali@foxsports.com 
  16  15	#uuencode <filename that will be send> <name for attached file that is sent> | 
  17  16	 
  18  17	#passing the param to function 
  19  18	email $1 
  20  19	fi 
  21  	view plain | print | ?
  22  #!/bin/bash function email { echo "this is a test" | mail -s "test" "$1" } if [ "$1" = "" ]; then echo "nothing in param" else echo "sending mail" #emailing attachment #uuencode statsNbaAllStarSim statssim | mail -s "test attachement" atayebali@foxsports.com #uuencode <filename that will be send> <name for attached file that is sent> | #passing the param to function email $1 fi

Function Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	 
   5  4	function show_uptime 
   6  5	{ 
   7  6	    # Temporary function stub 
   8  7	        uptime 
   9  8	} 
  10  9	 
  11  10	      echo $(show_uptime) 
  12  	view plain | print | ?
  13  #!/bin/bash function show_uptime { # Temporary function stub uptime } echo $(show_uptime)

Command Line Args
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	 
   5  4	echo "Positional Parameters" 
   6  5	echo '$0 = ' $0 
   7  6	echo '$1 = ' $1 
   8  7	echo '$2 = ' $2 
   9  8	echo '$3 = ' $3 
  10  9	 
  11  10	if [ $1==hello ] ; then 
  12  11	echo "YO MAN" 
  13  12	fi 
  14  	view plain | print | ?
  15  #!/bin/bash echo "Positional Parameters" echo '$0 = ' $0 echo '$1 = ' $1 echo '$2 = ' $2 echo '$3 = ' $3 if [ $1==hello ] ; then echo "YO MAN" fi
  16  <code>
  17  Read User Input
  18  
  19  1	#!/bin/bash 
  20  2	 
  21  3	echo "Hello guy what is your name?" 
  22  4	read text 
  23  5	echo "Hello $text" 
  24  6	 
  25  7	echo $1 
  26  8	 
  27  9	echo "How old are you?" 
  28  10	if  read -t 5 response ; then 
  29  11	        echo "sweet" 
  30  12	else 
  31  13	        echo "sour" 
  32  14	 
  33  15	fi 
  34  16	 
  35  17	echo -n "type in secret" 
  36  18	if  read -t 5 response ; then 
  37  19	        echo "sweet" 
  38  20	fi 
  39  	view plain | print | ?
  40  #!/bin/bash echo "Hello guy what is your name?" read text echo "Hello $text" echo $1 echo "How old are you?" if read -t 5 response ; then echo "sweet" else echo "sour" fi echo -n "type in secret" if read -t 5 response ; then echo "sweet" fi

Create Cookie Script
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	function set_value 
   5  4	{ 
   6  5	       domain=( fox hotmail gmail ) 
   7  6	        echo "${domain[0]}" 
   8  7	 
   9  8	} 
  10  9	 
  11  10	 
  12  11	 
  13  12	for line in `cat description.dat` 
  14  13	        do 
  15  14	 
  16  15	        echo $line 
  17  16	        String=$String$line=$(set_value)\& 
  18  17	 
  19  18	done 
  20  19	 
  21  20	echo -e  "\n\n" 
  22  21	 
  23  22	echo $String 
  24  	view plain | print | ?
  25  #!/bin/bash function set_value { domain=( fox hotmail gmail ) echo "${domain[0]}" } for line in `cat description.dat` do echo $line String=$String$line=$(set_value)\& done echo -e "\n\n" echo $String

Introduce a new line
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	#Introduce a new line after every '&' in the test.txt file 
   5  4	 
   6  5	sed -e 's/&/\n/g' test.txt 
   7  	view plain | print | ?
   8  #!/bin/bash #Introduce a new line after every '&' in the test.txt file sed -e 's/&/\n/g' test.txt 

Permissions

// Triplets explained
chmod 755

Triplet for u: rwx => 4 + 2 + 1 = 7
Triplet for g: r-x => 4 + 0 + 1 = 5
Tripler for o: r-x => 4 + 0 + 1 = 5
Which makes : 755

AWK & SED Constructs

// Various awk and sed combinations.

   1  
   2  awk & Sed construct
   3  
   4  View lines  500 to 505
   5  awk 'NR >= 500 && NR <= 505'
   6  
   7  remove blank lines
   8  sed -e /^$/d data > filename
   9  
  10  remove tabs :
  11  
  12   sed 's/^[ \t]*//' data_raw > data_raw1
  13  
  14  search and replace words with escape:
  15  
  16  sed -e '/\/id\//d' urls.dat > urlsnostory.dat
  17  
  18  awk '{print $3,$2}' emp_names
  19  
  20  awk '/AL/ {print $3,$2}' emp_names
  21  
  22  what I used
  23  awk '{print $1}' parsed_hrefs >> finalcut
  24  
  25   awk '/href\=\"(.*) / {print $2}' hrefs pinr
  26  
  27  awk '{print $NF}' wnba -- Print last line of the code.
  28  
  29  -----------------------
  30  Extracting from live xml
  31  awk '/gameId=/{print "/nugget/100_5_" substr($5,9,11)}' ts
  32   awk '/gamecode/{print "/nugget/100_73_" substr($2,7,10)}' nba
  33  awk '/gamecode/{print "/nugget/100_49_" substr($2,7,9)}' mlb
  34  
  35  ----------------------------
  36  
  37  Extracting gameIds from scores page
  38  awk '/gamecode code=*/ {print substr($2,28,10)}' nbascores
  39  
  40  CBK:
  41  awk '/gamecode code=*/ {print substr($2,7,12)}' cbk
  42  
  43  Extracting headers
  44  awk '{print $1 " " $2 " " $3 " <img src=\"http://jqa.foxsports.com/fe/img/Writers/header/" $3 ".jpg\"> " "<br>"}' authwids 
  45  
  46  Field Seperator
  47  awk -F\\ '{print $6}' logos  [ The \\ escape is added ]

FIND & AWK combination

// File name extraction

   1  
   2  substr(field, position, number_of_characters)
   3  
   4   find |  awk '/MLB_LIVE/ {print substr($0, 21,6)}' | sort
   5  
   6  in a script (parse and save to tmp file:
   7  
   8  for string in $( find |  awk '/MLB_LIVE/ {print substr($0, 21,6)}'  ); do echo $string; done > tmp
   9  
  10  
  11  Extract from ping
  12  ping qafeed1 | awk ' {print substr($8, 6, 9)}'

FIND

// find by name
   1  
   2  find . -name '*3079716*'



// find file extensions
   1  
   2  find | cut -d "." -f3 | sort | uniq -c

Watch

// See file updates in real time

   1  
   2  watch -d <file or directory>
   3  
   4  watch -n 2 ls
   5  
   6  watch -d -n 5 cat live.xml

Find Server Type Wget

//Get server type.

   1  
   2  wget -d <url>
« Newer Snippets
Older Snippets »
Showing 1-10 of 18 total  RSS