[英]How to print fields using multiple field separators
我正在尋找使用空格和點來分隔數據的第二列和最后一列的值。
我的輸入是
abcds 874598 thd/vb.sbds/jf 48459 com.dat.first.NAME
所需的輸出是
874598 NAME
使用awk
:
$ awk -F'[. ]' '{print $2, $NF}' file
874598 NAME
-F
選項將字段分開設置,我們提供包含空格或句點的字符類 。 Awk使用字段分隔符值將文件中的每一行溢出到字段中,並將它們存儲在遞增引用中, $1
是第一個字段, $2
是第二個字段。 NF
是對當前記錄中字段數的引用, $NF
是最后一個字段的值。
您可以將命令讀取為:
awk # the command; awk takes a script and executes it on every input line
-F'[. ]' # the field separator; break each line up using spaces or periods
'{print $2, $NF}' # the script to execute; print the second and last field on every line
file # the input file to run the script on
使用read
和純BASH:
s='abcds 874598 thd/vb.sbds/jf 48459 com.dat.first.NAME'
IFS=$' \t.' read -ra arr <<< "$s"
echo "${arr[1]} ${arr[-1]}"
874598 NAME
分解:
IFS=' .' # sets input field separator as space or dot
read -ra # reads all fields into an array
${arr[1]} # represents 2nd element in array
${arr[-1]} # represents last element in array
從您的評論聽起來像您想要的:
$ awk -F '[[:space:]]+|[.]' '{print $2, $NF}' file
874598 NAME
要么:
$ awk '{sub(/.*\./,"",$NF); print $2, $NF}' file
874598 NAME
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.