CSSE2310 - Final Exam - Bash commands Flashcards
How do you handle a question like this ….
“Get the first line of file.c which contains the word “car”. “
- start with getting the first line
“head -1 file.c”
- Pipe this into grep to search for “car”
“head -1 file.c | grep “car”
Answer: “head -1 file.c | grep “car”
OR
cat file.c | head -1 | grep “car”
How do you handle a question like this …
“Get the last 4 lines in file.c and print them alphabetically”
- start with getting the last four lines
“tail -4 file.c”
- Pipe this into sort to make them alphabetical
“tail -4 file.c | sort
How do you handle a question like this …
“Get the lines in file.c that contain both cat and dog”
- start with searching the file for cat
grep -n “cat” file.c
- Pipe this into a grep to get the lines that also contain dog
grep -n “cat” file.c | grep “dog”
OR
cat file.c | grep “cat” | grep “dog”
How do you handle a question like this …
Get the lines in file.c that contain cat not case sensitive
- start with searching the file for lines that contain cat
grep -n “cat” file.c
- Use dash i to include not be case-sensitive
grep -i -n “cat” file.c
How do you handle a question like this …
Get all the lines in the current directory that contain the word cat
- start with searching the current directory that contain cat
grep -r cat .
How do you handle a question like this …
Count the number of lines in the file “file.c” that contain the word “dog”
- Use -c with grep
grep -c “dog” file.c
OR
grep “dog” file.c | wc -l
OR
cat file.c | grep “dog” | wc -l
How do you handle a question like this …
Get the files that contain “dog” in the current directory?
- Use -l with grep
grep -r -l “dog” .
OR
grep -l -r “dog” .
How do you handle a question like this …
Get the 2nd and 4th column of the file.c, delimited by tabs.
- Use cut to get the columns
cut -f 2,4 file.c
How do you handle a question like this …
Get the 2nd to 5th column of the file.c, delimited by tabs.
- Use cut to get the columns
cut -f 2-5 file.c
How do you handle a question like this …
Get the 5, 7 th column of the file.c delimited by “,”
- Use cut to get the columns and -d for the delimiter
cut -f 5,7 -d “,” file.c
How do you handle a question like this …
Get all the lines of the file.c where “cat dog” appears EXACTLY.
- Use grep -c to count the number of occurrences
grep -w “cat dog” file.c
How do you handle a question like this …
Get the first 4 lines of the file file.c
- Use head command and -n
head -4 file.c
How do you handle a question like this …
Get the lines in file.c that contain cat OR dog
- use grep command and \ |
grep “cat|dog” file.c
OR
grep -n “cat|dog” file.c
How do you handle a question like this …
Get the lines in file.c that contain cat AND dog
- use grep command and .*
grep “cat.*dog” file.c
How do you handle a question like this …
Get the fifth line of the file file.c
- Step 1. use the cat command to show the contents on screen.
cat file.c
- pipe into the head command to say what line you want to read
cat file.c | head -5
- pipe into the tail to say how many lines you want to read from the head command
cat file.c | head -5 | tail -1
OR
head -5 file.c | tail -1