0

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?

LoganLee
  • 145
  • 1
  • 10

2 Answers2

1

Using awk

$ awk -F"\\" '{if($4=="") print}' input_file
ba\tbb\tbc
ca\tcb\tcc
HatLess
  • 10,622
  • 5
  • 14
  • 32
  • awk -F"\\t" '{if($4=="") print}' input_file – LoganLee Apr 05 '22 at 04:26
  • 1
    Same thing a bit shorter ;) `awk -F'\' '$4==""' input_file` .. no need for an `if`, you can put the condition outside the `{}` ... and since `print` is awk's default action you can give the statement and the curlys a miss. – tink Apr 05 '22 at 06:05
0

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
Mehrwarz
  • 413
  • 1
  • 10