Lect11 / 12 - Basic Analysis Flashcards

1
Q

Explain the following ls commands:

  • # ls -l
  • # ls -R
  • # ls -lt
  • # ls -Rlth
A
  • # ls -l => long list (size and dates)
  • # ls -R => recursive list
  • # ls -lt => sort by modification date
  • # ls -Rlth => show human readable size
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does this command looks like:

“find, starting in /mnt/analysis, by name, file with .jpg extension”

A

find /mnt/analysis -name *jpg

Other Parameters:

  • -type f : files
  • -type d : directories
  • -name : by name
  • -iname : by name (case insensitive)
  • -many others : read the man page
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you execute a command based on the find result (e.g. md5hash)?

A

# find /mnt/analysis -type f -exec md5sum {} \;

or

# find /mnt/analysis -name *.jpg -exec md5sum {} \;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you determine the filetype by showing the first line in hex?

A

# xxd cat_Warmer.jpg | head -n 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the following grep options:

  1. # grep -i
  2. # grep -v
  3. # grep ^string
  4. # grep string$
  5. # grep [char]
  6. # grep ^$
A
  1. :case insensitive
  2. :reverse grep (exclude)
  3. :string at beginning of a line
  4. :string at the end of a line
  5. :group of characters
  6. :blank line
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you use a keyword list with grep?

A

# grep -abif analysis/searchlist.txt fat_fs.raw > analysis/hits.txt

Parameters are:

  • -f <filename> : Use filename as keyword list</filename>
  • -i : Case insensitive
  • -a : Search binary as text
  • -b : Return bit offset of keyword hits
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you replace control characters with new line characters using grep?

A

# tr ’[:cntrl:]’ ’\n’ < fat_fs.raw | grep -abif analysis/searchlist.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Viewing of different file types. How can you:

  1. stream the contents of a file to STDOUT
  2. paging viewer for documents (and command output)
  3. view MS Office docs from the command line (install first)
  4. view MS Office xml format files (.docx, etc.)
  5. for PDF files
  6. to view graphics from the command line
A
  1. cat
  2. less
  3. catdoc
  4. catdocx
  5. xpdf (or evince)
  6. xv or display
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to seek into the file to a specified number of bytes?

A

# xxd -s 75441 fat_fs.raw | head

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Name commands to parse structured data:

  1. display contents of a file to STDOUT
  2. same, but reverse the contents
  3. search for patterns and strings in an object
  4. sort the contents [reverse, unique, etc]
  5. counts the number of words, lines and bytes in output
  6. “an output processing tool”.
  7. Replace or “translate” characters and sets
  8. “Stream editor” - processes text in a stream
A
  1. cat
  2. tac
  3. grep
  4. sort
  5. wc
  6. awk
  7. tr
  8. sed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you sort in alphabetical order and removes duplicates?

A

# cat names.txt | sort -u

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you change the field delimiter in awk?

A

# cat file.txt | awk -F “,” ‘{print $1 $2 $3”\t”$NF}’

or

# awk -F “,” ‘{print $1 $2 $3”\t”$NF}’ <filename></filename>

$NF is normally the last field of a line as it stands for “number of fields”.​

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

awk examples:

How to add tabulator in the output?

A

cat text.txt | awk ‘{print $1 “\t” $2}’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

awk examples?

How to omit the header record?

A

# awk ‘NR!=1{print $1}’ file1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

awk examples:

Print entire file content?

A

# awk ‘{print $0}’ file1

or

# awk ‘1’ file1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

awk examples:

Use special field separator

A

# awk ‘{print $1,$3}’ FS=”,” file1

17
Q

awk examples:

Use comma to separate output

A

awk -F “,” ‘NR!=1{print $1,$3}’ OFS=”,” file1

18
Q

What is sed?

A

sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed ), sed works by making only one pass over the input(s), and is consequently more efficient.

19
Q

sed examples:

Add “fruit” to the beginning of every line

A

$ sed ‘s/^/Fruit: /’ sample1.txt

20
Q

sed examples:

Add something to the end of the line

A

$ sed ‘s/$/ Fruit/’ sample1.txt

21
Q

sed examples:

To replace a particular char (a => A)

A

$ sed ‘s/a/A/’ sample1.txt

22
Q

sed examples:

Replace all occurrences (a => A)

A

$ sed ‘s/a/A/g’ sample1.txt

23
Q

sed examples:

Replace the 2nd occurrence (a => A)

A

$ sed ‘s/a/A/2’ sample1.txt

24
Q

sed examples:

Replace all occurrences from 2nd occurrence onwards

A

$ sed ‘s/a/A/2g’ sample1.txt

25
Q

sed examples:

Replace ‘a’ only in a specific line say 3rd line

A

$ sed ‘3s/a/A/g’ sample1.txt

26
Q

sed examples:

Replace or substitute ‘a’ on a range of lines

A

$ sed ‘1,3s/a/A/g’ sample1.txt

27
Q

sed examples:

To replace the entire line with something. For example, to replace ‘apple’ with ‘apple is a Fruit’.

A

$ sed ‘s/.*/& is a Fruit/’ sample1.txt

28
Q

sed examples:

Using sed, we can also do multiple substitutions. For example, say to replace all ‘a’ to ‘A’, and ‘p’ to ‘P’:

A

$ sed ‘s/a/A/g; s/p/P/g’ sample1.txt

or

$ sed -e ‘s/a/A/g’ -e ‘s/p/P/g’ sample1.txt

29
Q

sed examples:

Add header into first line

A

$ sed -i ‘1i Employee, EmpId’ empFile

30
Q

sed examples:

Add a line ‘——-‘ after the header line or the 1st line

A

$ sed -i ‘1a —————’ empFile

31
Q

sed examples:

Add a trailer line to this file

A

$ sed -i ‘$a —————’ empFile

32
Q

sed examples:

Add a record after a particular record

A

$ sed -i ‘/Hilesh/a Bharti, 1002’ empFile

33
Q

Logfile analysis:

Show only one filed of the log file

A

$ cat cisco.log | awk ‘{print $5}’

Output:

%DHCPD-4-PING_CONFLICT:

%DHCPD-4-PING_CONFLICT:

%SYS-5-CONFIG_I:

%SYS-5-CONFIG_I:

34
Q

Logfile analysis:

Group them together, count them, then sort them from the greatest to least number of occurrences

A

cat cisco.log | awk ‘{print $5}’| sort | uniq -c | sort -rn

Output:

8 %LINEPROTO-5-UPDOWN:

3 %SYS-5-CONFIG_I:

2 %DHCPD-4-PING_CONFLICT:

1 by

1 Software

1 Cisco

1 %SYS-5-RESTART:

35
Q

Logfile analysis:

Exclude garbage from output

A

cat cisco.log | grep %[a-zA-Z]*-[0-9]-[a-zA-Z]* | awk ‘{print $5}’ | sort | uniq -c | sort -rn

Output:

8 %LINEPROTO-5-UPDOWN:

3 %SYS-5-CONFIG_I:

2 %DHCPD-4-PING_CONFLICT:

1 %SYS-5-RESTART:

1 %SSH-5-ENABLED: