Skip to content

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.csv with multiple columns of data (e.g., name, age, city).
  • Use the cut command to extract specific columns from data.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.txt and file2.txt with multiple lines of text.
  • Use the paste command to merge the lines from file1.txt and file2.txt side 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.csv and salaries.csv with a common key column (e.g., employee ID).
  • Use the join command 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.txt with unsorted lines of text.
  • Use the sort command to sort the lines in unsorted.txt alphabetically 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.