0

there is my script:

table_nm=$1
hive_db=$(echo $table_nm | cut -d'.' -f1)
hive_tb=$(echo $table_nm | cut -d'.' -f2)

At first, I got the right result:

$echo "dev.dmf_bird_cost_detail" | cut -d'.' -f1
dev   #correct
$echo "dev.dmf_bird_cost_detail" | cut -d'.' -f2
dmf_bird_cost_detail   #correct

but,i got something is wrong,if there is no specified character in $table_nm, I get this result:

$echo "dmf_bird_cost_detail" | cut -d'.' -f1
dmf_bird_cost_detail   
$echo "dmf_bird_cost_detail" | cut -d'.' -f2
dmf_bird_cost_detail  
$echo "dmf_bird_cost_detail" | cut -d'.' -f3
dmf_bird_cost_detail   

The result that is not I expected, i hope it's empty, so i conducted some tests and found that if there is no specified character in the string, the command "cut" will return the original value, is that true?

At last,i know "awk" will solves my problem, but I would like to know why "cut" has the above result? Thank you guys so much!

Lvbey
  • 3
  • 1
  • 1
    By default with the field option, everything is printed if no delimiter exists. There used to be a `-s` (`--only-delimited`) option, which prevented this but I can't get it to work on my box. You may have better luck! – Rolf of Saxony Jul 30 '21 at 06:41

2 Answers2

0

From POSIX cut specification:

-f list

[...] Lines with no field delimiters shall be passed through intact, unless -s is specified. [...]

why "cut" has the above result?

My guess would be that the first implementations of cut had such behavior (my guess would be it was a bug), and it was preserved and POSIX standardized existing behavior and added -s option. You can browse https://minnie.tuhs.org/cgi-bin/utree.pl for some old verison of cut.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

The proper solution is probably to use a parameter expansion anyway.

hive_db=${table_nm%.*}
hive_tb=${table_nm#"$hive_db".}

If you expect more than one dot, you need some additional processing to extract the second field.

Because this uses shell built-ins, it is a lot more efficient than spawning two processes for each field you want to extract (and even then you should use proper quoting).

tripleee
  • 175,061
  • 34
  • 275
  • 318