How do you check if a text file had tab as its delimiter in bash?

14,325

Solution 1

Assuming a tab will only exist on the first line when the file is tab delimited then this

if awk '{exit !/\t/}' "$file"; then
    : # tab file
else
    : # space file
fi

should do what you want.

Also:

if [ -n "$(sed -n '/\t/p;q' "$file")" ]; then
    : # tab file
else
    : # space file
fi

Solution 2

The above solutions only check that there are tabs somewhere, not that the file is correctly formatted, i.e. that each line has 3 tab-separated columns.

I'd use something like the following, which checks that each line has the correct number of tabs:

no_cols=3
no_lines=$(cat "${file}" | wc -l)
no_tab_lines=$(cat "${file}" | cut -f${no_cols} | sed '/^$/d' | wc -l)
if [[ ${no_lines} -eq ${no_tab_lines} ]]; then
    echo "tabs"
else
    echo "not all tabs"
fi
Share:
14,325
Redson
Author by

Redson

Updated on August 21, 2022

Comments

  • Redson
    Redson over 1 year

    So I have a text file and it may have a tab as its field separator (delimiter) or it may have a space as a field separator. I would like to check if that text file is tabulated otherwise I will do something else with the file. I am using a bash script. So i'm open to anything with pure bash, sed, awk, grep, etc. (NOTE: that they are all GNU). So I am thinking of a structure like this:

    if [if delimiter is tab]; then
        #do soemthing
    elif [if delimiter is space]; then
        #do something else
    fi
    

    Any suggestions? Let me know if further explanation is required. Thanks!

    Here is an explanation update on what the text file looks like:

    If the text file has a tab as delimiter, then it delimited on every line. If the text file has a space as delimiter, then it is NOT delimited every line.

    Here are examples of possible text files that I might be facing:

    Delimiter is tab:

    col1   col2   col3
    -------
    1   2   3
    4   5   6
    

    Delimiter is space: (the space is between 12 and 3 && 4 and 56)

    col1col2col3
    -----------
    12 3
    4 56
    
  • Redson
    Redson over 9 years
    Thanks for the answer! +1. but I'm getting syntax error: unexpected end of file. Any ideas as to why?
  • Etan Reisner
    Etan Reisner over 9 years
    @Alias With what code exactly? You need some non-comment contents in each block of the if so what I wrote isn't legal as-is. Editing to fix that.
  • Redson
    Redson over 9 years
    I'm getting an error in the if statement for both code segments you posted. I had echo statements instead of comments as well inside the if statements
  • Etan Reisner
    Etan Reisner over 9 years
    Can you pastebin the code you have? Both of my snippets work when pasted into bash here. What shell are you using?
  • Redson
    Redson over 9 years
    not sure how to check the version of shell. so i used ps -p $$ and got: PID TTY TIME CMD 5270 pts/0 00:00:00 bash
  • Etan Reisner
    Etan Reisner over 9 years
    Does the file have DOS line endings? Does running do2sunix on it fix the problem?
  • Redson
    Redson over 9 years
    Thanks!!! there were special characters since the file was created using Windows.
  • Ed Morton
    Ed Morton over 9 years
    FWIW awk '{exit !/\t/}' file would do the job.
  • Etan Reisner
    Etan Reisner over 9 years
    @EdMorton I thought that might be possible but wasn't sure and, for some reason, didn't test it.