in black and white
Main menu
Home About us Share a book
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Linux for dummies - Klimas M.

Klimas M. Linux for dummies - Wiley publishing , 2002. - 169 p.
Download (direct link): linuxfordummies2002.pdf
Previous << 1 .. 101 102 103 .. 104105 106 < 107 > 108 109 110 111 112 113

gawk is particularly suitable for processing text-based tables. A table consists of records (each line is normally one record). The records contain fields separated by a delimiter. Often used delimiters are whitespace (gawk default), comma, or colon. All gawk expressions have a form: gawk 'pattern {action}' my_file. You can ommit the patern or action: the default pattern is "match everything"
Part 7: Learning with Linux
Linux Newbie Guide by Stan, Peter and Marie Klimas 01/08/2003
and the default action is "print the line". gawk can also be used as a filter (to process the output from another command, as used in our examples).
Example. To print lines containing the string "1024", I may use: cat filename | gawk '/1024/ {print}'
Like in sed, the patterns to match are enclosed in a pair of "/ /".
What makes gawk more powerful than sed is the operations on fields. $1 means "the first field", $2 means "the second field", etc. $0 means "the entire line". The next example extracts fields 3 and 2 from lines containing "1024" and prints them with added labels "Name" and "ID". The printing goes to a file called "newfile":
cat filename | gawk '/1024/ {print "Name: " $3 "ID: " $2}' > newfile
The third example finds and prints lines with the third field equal to "peter" or containing the string "marie":
cat filename | gawk '$3 == "peter" || $3 ~ /marie/ '
To understand the last command, here is the list of logical tests in gawk: == equal, ! = not equal, < less than, > greater than, <= less than or equal to, >= greater than or equal to, ~ matching a regular expression, !~ not matching a regular expression, || logical OR,
&& logical AND, ! logical NOT.
Concurrent versions system. Try: info cvs for more information. Useful to keep the "source code repository" when several programmers are working on the same computer program.
(in X-terminal). A GUI front-end to the cvs versioning system. file -z filename
Determine the type of the file filename. The option -z makes file look also inside compressed files to determine what the compressed file is (instead ofjust telling you that this is a compressed file).
To determine the type of content, file looks inside the file to find particular patterns in contents ("magic numbers")—it does not just look at the filename extension like MS Windows does. The "magic numbers" are stored in the text file /usr/share/magic—really impressive database of filetypes.
touch filename
Change the date/time stamp of the file filename to the current time. Create an empty file if the file does not exist. You can change the stamp to any date using touch -t 200201311759.30 (year 2002 January day 31 time 17:59:30).
There are three date/time values associated with every file on an ext2 filesystem:
- the time of last access to the file (atime)
- the time of last modification to the file (mtime)
- the time of last change to the file's inode (ctime).
Touch will change the first two to the value specified, and the last one always to the current system time. They can all be read using the stat command (see the next entry).
stat filename
Print general info about a file (the contents of the so-called inode). strings filename | more
Display the strings contained in the binary file called filename, "strings" could, for example, be a useful first step to a close examination of an unknown executable.
(=octal dump). Display contents as octal numbers. This can be useful when the output contains non-printable characters. For example, a filename may contain non-printable characters and be a real pain. This can also be handy to view binary files.
dir | od -c | more
(I would probably rather do: ls -b to see any non-printable characters in filenames).
cat my_file | od -c |more od my_file |more
Comparison of different outputs:
Show 16 first characters from a binary (/bin/sh) as ASCII characters or backslash escapes (octal):
Part 7: Learning with Linux
Linux Newbie Guide by Stan, Peter and Marie Klimas
od -N 16 -c /bin/sh output:
0000000 177 E L F 001 001 001 \0 \0 \0 \0 \0 \0 \0 \0 \0
Show the same binary as named ASCII characters: od -N 16 -a /bin/sh output:
0000000 del E L F soh soh soh nul nul nul nul nul nul nul nul nul
Show the same binary as short hexcadecimals: od -N 16 -t x1 /bin/sh output:
0000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Show the same binary as octal numbers: od -N 16 /bin/sh output:
0000000 042577 043114 000401 000001 000000 000000 000000 000000
(=word count) Print the number of lines, words, and bytes in the file.
Examples: dir | wc
cat my_file | wc wc myfile
cksum filename
Compute the CRC (="cyclic redundancy check") for file filename to verify its integrity. md5sum filename
Compute a md5 checksum (128-bit) for file filename to verify its integrity. mkpasswd -l 10
Make a hard-to-guess, random password of the length of 10 characters. sort -f filename
Arrange the lines in filename according to the ascii order. The option -f tells sort to ignore the upper and lower character case. The ascii character set is (see man ascii):
Previous << 1 .. 101 102 103 .. 104105 106 < 107 > 108 109 110 111 112 113