Search code examples
bashsedcut

Cut string of text after a character from a column each line of a csv, keeping the other columns, and printing to a new file


I have a CSV file with a first column that reads:

/Users/swilki/Desktop/Africa_OSSD/OSSD_Output/BER5_OSSD_F008071.csv.0.01.out.csv

Followed by additional columns listing counts pulled from other CSV files.

What I want is to remove "/Users/swilki/Desktop/Africa_OSSD/OSSD_Output/" from each line without affecting any other part of the file.

I've tried using sed, grep, and cut, but that only seems to print the output in the terminal or a new file only containing that part of the line, and not the rest of the columns. Can I remove the "/Users/swilki/Desktop/Africa_OSSD/OSSD_Output/" and keep everything else the same?


Solution

  • You can use awk to get this job done.
    Please see below code which will replace the contents/Users/swilki/Desktop/Africa_OSSD/OSSD_Output/ with "" empty and will update the operation in the same file with the option inplace
    yourfile.csv is the input file.

    awk -i inplace '{sub(/\/Users\/swilki\/Desktop\/Africa_OSSD\/OSSD_Output\//,"")}1' yourfile.csv
    
    

    The above will remove the "/Users/swilki/Desktop/Africa_OSSD/OSSD_Output/" and keep everything else same.
    Output of yourfile.csv:
    BER5_OSSD_F008071.csv.0.01.out.csv

    Option 2, If you want to print in a new file:
    Below code will be give the replaced contents in the new file your_newfile.csv

    awk '{sub(/\/Users\/swilki\/Desktop\/Africa_OSSD\/OSSD_Output\//,"")}1' yourfile.csv >your_newfile.csv