Practice Exercise: File Manipulation Utilities
Objective
Explore and practice file manipulation utilities in Linux, including cut, paste, join, and sort.
Task 1: Using cut for Text Extraction
- Open a terminal window.
- Create a sample CSV file named data.csvwith multiple columns of data (e.g., name, age, city).
- Use the cutcommand to extract specific columns fromdata.csv. For example, extract only the "name" and "city" columns and save the result in a new file.# Create the data.csv file with sample data [intern@intern-a1t-inf-lnx1 ~]$ cat data.csv name,age,city Alice,25,New York Bob,30,Los Angeles Charlie,28,Chicago David,22,Houston Eve,35,San Francisco Frank,40,Boston Grace,29,Seattle Hank,32,Austin Ivy,27,Denver Jack,24,Phoenix # Use the cut command to extract specific columns (e.g., "name" and "city") [intern@intern-a1t-inf-lnx1 ~]$ cut -d ',' -f 1,3 data.csv > extracted_data.csv # Display the contents of the extracted_data.csv file [intern@intern-a1t-inf-lnx1 ~]$ cat extracted_data.csv name,city Alice,New York Bob,Los Angeles Charlie,Chicago David,Houston Eve,San Francisco Frank,Boston Grace,Seattle Hank,Austin Ivy,Denver Jack,Phoenix
Task 2: Merging Lines with paste
- Create two sample text files named file1.txtandfile2.txtwith multiple lines of text.
- Use the pastecommand to merge the lines fromfile1.txtandfile2.txtside by side and save the result in a new file.# Create two sample text files, file1.txt and file2.txt [intern@intern-a1t-inf-lnx1 ~]$ cat file1.txt Line 1 from file1 Line 2 from file1 Line 3 from file1 [intern@intern-a1t-inf-lnx1 ~]$ cat file2.txt Line 1 from file2 Line 2 from file2 Line 3 from file2 # Use the paste command to merge the lines side by side and save the result in a new file, merged.txt [intern@intern-a1t-inf-lnx1 ~]$ paste file1.txt file2.txt > merged.txt # Display the contents of the merged.txt file [intern@intern-a1t-inf-lnx1 ~]$ cat merged.txt Line 1 from file1 Line 1 from file2 Line 2 from file1 Line 2 from file2 Line 3 from file1 Line 3 from file2
Task 3: Combining Files with join
- Create two sample CSV files named employees.csvandsalaries.csvwith a common key column (e.g., employee ID).
- Use the joincommand to combine the two files based on the common key column and save the result in a new file.# Create two sample CSV files, employees.csv and salaries.csv, with a common key column (employee ID) cat > employees.csv <<EOF Employee ID,Name,Department 101,John,HR 102,Emily,Finance 103,Michael,IT 104,Susan,HR EOF cat > salaries.csv <<EOF Employee ID,Salary 101,60000 102,75000 103,80000 104,55000 EOF # Use the join command to combine the two files based on the common key column (Employee ID) # The `-t` option specifies the delimiter (,) used in the CSV files, and `-1` and `-2` specify the join fields. # The `-o` option specifies the output format. join -t ',' -1 1 -2 1 employees.csv salaries.csv > combined.csv # Display the contents of the combined.csv file cat combined.csv Employee ID,Name,Department,Salary 101,John,HR,60000 102,Emily,Finance,75000 103,Michael,IT,80000 104,Susan,HR,55000
Task 4: Sorting Data with sort
- Create a sample text file named unsorted.txtwith unsorted lines of text.
- Use the sortcommand to sort the lines inunsorted.txtalphabetically and save the sorted result in a new file.[intern@intern-a1t-inf-lnx1 ~]$ cat > unsorted.txt <<EOF Banana Cherry Apple Date Grape EOF [intern@intern-a1t-inf-lnx1 ~]$ sort unsorted.txt > sorted.txt [intern@intern-a1t-inf-lnx1 ~]$ cat sorted.txt Apple Banana Cherry Date Grape
Conclusion
In this lab exercise, you've practiced using file manipulation utilities such as cut, paste, join, and sort in a Linux environment. These commands are essential for various text and data processing tasks. You've learned how to extract specific columns, merge lines from multiple files, combine files based on common keys, and sort text data. These skills are valuable for tasks like data cleaning, data merging, and data analysis in a Linux environment.