some sytem scripts

This commit is contained in:
Marina Wahl 2014-07-05 18:22:01 -04:00
parent 4b0a965e4a
commit 5852171b21
25 changed files with 639 additions and 1 deletions

View file

@ -0,0 +1,53 @@
Data Manipulation
-----------------
Print elements in the column 3 that has the word "good" in the other columns
$ awk '/good/ { print $3 }' inventory
Syntax:
$ awk -f {PROGRAM FILE} FILENAME
awk program is:
PATTERN{
ACTION 1
ACTION 2
ACTION 3
}
. (Dot) Match any character
* Match zero or more character
^ Match beginning of line
$ Match end of line
\ Escape character following
[ ] List
{ } Match range of instance
+ Match one more preceding
? Match zero or one preceding
| Separate choices to match
IF CONDITION
------------
if (condition)
{
Statement 1
Statement 2
Statement N
if condition is TRUE
}
else
{
Statement 1
Statement 2
Statement N
if condition is FALSE
}
FOR LOOP
--------

View file

@ -0,0 +1,41 @@
For files with many columns:
===========================
$ cut -f2 NAMEFILE --> display the second column
$ cut -f1 NAMEFILE --> display first column
$ paste FILE1 FILE2 --> join the columns together of two files
$ join FILE1 FILES2 --> join separate files but only if there is common field in both files with identical values
Changing the contend of files:
==============================
CHARACTERS:
----------
$ tr "h2" "3x" < FILENAME --> changes all h to 3 and all 2 to x
WORDS:
------
$ sed expression file
$ sed '/tea/s//milk/g' FILENAME
/tea/ --> find tea word
s//milk/ --> replace (substitute) the word milk for tea
g --> make the changes globally
DUPLICATES:
-----------
Print LINES that are unique.
$ uniq FILENAME
Duplicate lines must be next to next to each other, so it's better to sort first:
$ sort FILENAME | uniq
Ex
--
$ ex FILENAME --> start the editor

View file

@ -0,0 +1,21 @@
# awk -f FILEAWK FILELIST
BEGIN{
}
#
# main logic is here
#
{
sfile = $1
dfile = $2
cpcmd = "cp " $1 " " $2
printf "Coping %s to %s\n",sfile,dfile
system(cpcmd)
}
#
# End action, if any, e.g. clean ups
#
END{
}

View file

@ -0,0 +1,12 @@
# awk -f for_loop.awk
BEGIN {
printf "To test for loop\n"
printf "Press CTRL + C to stop\n"
}
{
for(i=0;i<NF;i++)
{
printf "Welcome %s, %d times.\n" ,ENVIRON["USER"], i
}
}

View file

@ -0,0 +1,8 @@
# awk -f math1
{
no1 = $1
no2 = $2
ans = $1 + $2
print no1 " + " no2 " = " ans
}

View file

@ -0,0 +1,30 @@
# awk -f math2
BEGIN {
myprompt = "(To Stop press CTRL+D) > "
printf "Welcome to MyAddtion calculation awk program v0.1\n"
printf "%s" ,myprompt
}
{
no1 = $1
op = $2
no2 = $3
ans = 0
if ( op == "+" )
{
ans = $1 + $3
printf "%d %c %d = %d\n" ,no1,op,no2,ans
printf "%s" ,myprompt
}
else
{
printf "Opps!Error I only know how to add.\nSyntax: number1 + number2\n"
printf "%s" ,myprompt
}
}
END {
printf "\nGoodbye, %s\n !" , ENVIRON["USER"]
}