2015-06-29 39 views

回答

2

使用awk

$ awk -F'[. ]' '{print $2, $NF}' file 
874598 NAME 

-F選項設置字段分離,我們提供一種含有character class的空間或一段時間。 Awk使用字段分隔符值將文件中的每一行分散到字段中,並將它們存儲在增量引用中,第一個字段爲$1,第二個爲$2NF是對當前記錄中字段數的引用,$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 
0

使用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 
+0

謝謝,我想第一個(AWK),輸出是不錯,但對於長的名單就像低於其沒有給予正確的結果,ABCDEF 28295 1 2 01:48? 00:01:26 /apps/local/share/java/jdk1.7.0_67/bin/java -classpath ./thfd-1.0-SNAPSHOT-with-dependencies.jar:/drags/theme/nod/ojdf/config -javaagent :./ spring-instrument-3.2.5.RELEASE.jar -Djavax.net.ssl.trustStore =/drags/theme/thisone/tdfh_thdi.jks -Djavax.net.ssl.key =/drags/theme/thisone/PML_thdi .jks -Djavax.net.ssl.key =客戶端com.twishe.dgfcc.ilem.MainCLass –

+0

用你的長線,我得到'28295 MainCLass'作爲輸出。你還期望什麼? – anubhava

+0

謝謝,再一次,我明白了,實際上在我的要求中,它是由TAB分隔的空間位置,所以我們如何才能得到它 –

0

從您的評論聽起來像你想:

$ awk -F '[[:space:]]+|[.]' '{print $2, $NF}' file 
874598 NAME 

或:

$ awk '{sub(/.*\./,"",$NF); print $2, $NF}' file 
874598 NAME 
+0

謝謝,Ed,這對我有用:) –

相關問題