Linux

在 shell 腳本中列印具有多列的文件的所有行

  • April 10, 2016

我有一個包含 4 列的文件,格式如下:

name;user_id;dob;date_of_joining

我想通過 shell 腳本在下面的輸出中列印所有變數

員工 ” $ name" is having " $ user_id”,其出生日期為“ $ dob" and joined organisation on " $ 加入的日期”

我怎樣才能做到這一點?

使用awk

awk -F\; '{print("The employee", $1, "is having", $2, "whose date of birth is", $3, "and joined organisation on", $4)}' filename

文件名在哪裡filename

read使用 shell,您可以使用操作IFS變數的命令從一行中獲取多個欄位:

while IFS=';' read -r name user_id dob joined; do
   echo "The employee $name is having $user_id whose date of birth is $dob and joined organisation on $joined"
done < filename

引用自:https://unix.stackexchange.com/questions/234765