Home > Mobile >  How to extract lines containing blank in a specific column with Linux command
How to extract lines containing blank in a specific column with Linux command

Time:04-05

Example:

aa\tab\tac\tad\tae
ba\tbb\tbc
ca\tcb\tcc
da\tdb\tdc\tdd

Expected output:

ba\tbb\tbc
ca\tcb\tcc

I want to extract lines containing blank in a 4th column with linux command. If you know the command, could you let me know it?

CodePudding user response:

Using awk

$ awk -F"\\" '{if($4=="") print}' input_file
ba\tbb\tbc
ca\tcb\tcc

CodePudding user response:

You can use cut and use "" as delimiter and check the field 4 if it is not empty. Then use the grep -v to check if a trailing "" exists or not. in an If statement you can get the result.

text='aa\tab\tac\'    

if [[ -z $(echo $text | cut -d \\ -f 4 | grep -v \\\\\$) ]]; then
    if[[ -z $(echo $text | grep -v \\\\\$) ]]; then
        echo $text
    else
        echo 'Text has a slash at the end'
    fi
else 
    echo 'Not valid input'
fi
  • Related