Linux Sysadmin Test Prep
File Manipulation
File Manipulation
sed
sed 's/and/\&/g;s/^I/You/g' example.txt
(multiple search and replace global)
sed -n '/^Oct 3/ p' /var/log/syslog | sed -n 1,5p
(print only lines 1-5 starting with "Oct 3")
sed '/junk/d' file.txt
(delete "junk" lines)
sed 'G' file.txt
(add new line after each line)
uniq, sort, cut, tr
cat /var/log/syslog | uniq -c -w 6
(count, 6 wide [date space])
du -sch /var/* | sort -h
( file usage - summarize, total, human readable | sort human numerical )
cat file.txt | tr [:lower:] [:upper:]
last | tr -s ' ' | cut -d' ' -f1,3 | sort -k2 | uniq
Complete each exercise in a single line of code.
There are files we won't want mixed in with files we do want. Search all the folders and delete all the files with "junk" in their filename (case insensitive).
Then, delete everything in
/danielsTestDrills/answers/
except for files containing "answer" (case insensitive).
Copy lines 1, 2, 3, and 9 from
/danielsTestDrills/testFile-1
into a new file/danielsTestDrills/testFile-2
.Copy all lines from
/danielsTestDrills/testFile-1
with with any data between "daniel" and "pass the LFCS" (case insensitive) and append into/danielsTestDrills/testFile-2
.Copy any lines with the word "stuff" that does not come at the beginning or end (ignoring trailing whitespace) of a line from
/danielsTestDrills/testFile-1
and append into/danielsTestDrills/testFile-2
.
Copy any lines with ! from
/danielsTestDrills/testFile-1
into a new file named/danielsTestDrills/testFile-3
.Copy all lines with ordinal numbers (1st - 10th) from
/danielsTestDrills/testFile-1
into the file/danielsTestDrills/testFile-3
.Copy any lines starting with daniel (case insensitive)from
/danielsTestDrills/testFile-1
into the file named/danielsTestDrills/testFile-3
.Move the first line of
/danielsTestDrills/testFile-3
to the end of the file.
Delete any lines from
/danielsTestDrills/testFile-1
with the "junk" or "Junk" in them.Delete line 4 from
/danielsTestDrills/testFile-1
.Delete any lines starting or ending with "stuff" from
/danielsTestDrills/testFile-1
.
Compare the three files in
/danielsTestDrills
and put the file name of the different file into/danielsTestDrills/answers/answerFile
. Do not include leading path information.Find the single unique line and append it to
/danielsTestDrills/answers/answerFile
Make a tar archive of the three testFile files and save it as/danielsTestDrills/answers/testFiles.tar
and then remove the three files.