Professional Documents
Culture Documents
Introduction To Computer Science: Lab Session3
Introduction To Computer Science: Lab Session3
Introduction To Computer Science: Lab Session3
Lab session3
Create a Text File
• cat command
– For Creating a text file
– useful if you want to immediately add some text to
your new file
cat > filename.txt
• press Ctrl+D to exit the file and return to the prompt.
– to view the contents of your file
cat sample.txt
Cont…
• Touch Command
– To create a text file
– To create multiple new files with a single command.
– for quickly creating files you intend to use later
touch filename.txt
Cont…
• The Standard Redirect Symbol (>)
– To create a text file
– To create one file at a time
> filename.txt
Combining Text Files
• Cat command
– to combine (or concatenate) two or more t extfiles
into one text file.
– Cat file1.txt file2.txt file3.txt
Comparing files
• cmp:
– used to compare two files character by character.
cmp file1 file2
• diff:
• used to compare two files line by line.
– diff file1.txt file2.txt
Cont…
• dircmp:
– used to compare the contents of directories.
dircmp dir1 dir2
• uniq:
– used to filter the repeated lines in a file which are
adjacent to each other
• uniq file1
Count Number of Lines, Words,
Characters
• wc (word count) command
– used to find out number of newline count, word
count, byte and characters count in a files.
– wc -l : Prints the number of lines in a file.
– wc -w : prints the number of words in a file.
– wc -c : Displays the count of bytes in a file.
– wc -m : prints the count of characters from a file.
– wc -L : prints only the length of the longest line in a
file.
Sorting a file
• Sort command
– Displays a File in Order
– sort file-list
view file content
• Cat command
• Less command
– uses to view file.
– allows for both forward and backward navigation.
Less filename
• more command: to view a text file one page at a
time, press spacebar to go to the next page
more filename
Cont…
• head : displays the first ten lines of a file,
unless otherwise stated.
– head filename:Would display the first ten lines
of your file.
– head -n filename – Would display the first fifteen
lines of myfile.txt. When n is line number
• tail : display the last part of the file
– tail filename
– tail -n filename : display the last n lines of the file
Searching a string in a file
grep command
for searching a string from a file.
grep string filename
Exercises
• Create a file name months_of _year
• Explore the file with name of the the first five
months
• Append the day and year to the file
• Display the file content using cat command
• Create a file name BirthDate and explore it with
your birth date like September Thursday,2000
• Compare the two files
• Search the month of your birth date from the first
file