Explain the following ls commands:
How does this command looks like:
“find, starting in /mnt/analysis, by name, file with .jpg extension”
find /mnt/analysis -name *jpg
Other Parameters:
How can you execute a command based on the find result (e.g. md5hash)?
# find /mnt/analysis -type f -exec md5sum {} \;
or
# find /mnt/analysis -name *.jpg -exec md5sum {} \;
How can you determine the filetype by showing the first line in hex?
# xxd cat_Warmer.jpg | head -n 1
Explain the following grep options:
How can you use a keyword list with grep?
# grep -abif analysis/searchlist.txt fat_fs.raw > analysis/hits.txt
Parameters are:
How can you replace control characters with new line characters using grep?
# tr ’[:cntrl:]’ ’\n’ < fat_fs.raw | grep -abif analysis/searchlist.txt
Viewing of different file types. How can you:
How to seek into the file to a specified number of bytes?
# xxd -s 75441 fat_fs.raw | head
Name commands to parse structured data:
How can you sort in alphabetical order and removes duplicates?
# cat names.txt | sort -u
How can you change the field delimiter in awk?
# 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”.
awk examples:
How to add tabulator in the output?
cat text.txt | awk ‘{print $1 “\t” $2}’
awk examples?
How to omit the header record?
# awk ‘NR!=1{print $1}’ file1
awk examples:
Print entire file content?
# awk ‘{print $0}’ file1
or
# awk ‘1’ file1
awk examples:
Use special field separator
# awk ‘{print $1,$3}’ FS=”,” file1
awk examples:
Use comma to separate output
awk -F “,” ‘NR!=1{print $1,$3}’ OFS=”,” file1
What is sed?
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.
sed examples:
Add “fruit” to the beginning of every line
$ sed ‘s/^/Fruit: /’ sample1.txt
sed examples:
Add something to the end of the line
$ sed ‘s/$/ Fruit/’ sample1.txt
sed examples:
To replace a particular char (a => A)
$ sed ‘s/a/A/’ sample1.txt
sed examples:
Replace all occurrences (a => A)
$ sed ‘s/a/A/g’ sample1.txt
sed examples:
Replace the 2nd occurrence (a => A)
$ sed ‘s/a/A/2’ sample1.txt
sed examples:
Replace all occurrences from 2nd occurrence onwards
$ sed ‘s/a/A/2g’ sample1.txt