2013-10-03 58 views
1

我有一個是給我輸出的命令:用awk修改輸出

/home/konnor/md5sums:ea66574ff0daad6d0406f67e4571ee08 counted-file.xml.20131003-083611 

我需要的輸出爲:

ea66574ff0daad6d0406f67e4571ee08 counted-file.xml 

我得到的最接近是:

$ echo /home/konnor/md5sums:ea66574ff0daad6d0406f67e4571ee08 counted-file.xml.20131003-083611 | awk '{ printf "%s", $1 }; END { printf "\n" }' 

/home/konnor/md5sums:ea66574ff0daad6d0406f67e4571ee08 

我對awk並不熟悉,但我相信這是我想使用的命令,任何人都有任何想法?

回答

2

或者只是一個sed oneliner:

echo /home/konnor/md5sums:ea66574ff0daad6d0406f67e4571ee08 counted-file.xml.20131003-083611 \ 
    | sed -E 's/.*:(.*\.xml).*/\1/' 
+1

它仍然需要放棄尾隨的.20 131003-083611'來自文件名。簡單的變化...''/ .*:(。* \。xml)。* $/\ 1 /'' – lurker

+0

Oopsie ...忘了那個:) – Wolph

+0

兄弟...這是我最美的東西見過。謝謝。 – cakes88

2
$ echo "/home/konnor/md5sums:ea66574ff0daad6d0406f67e4571ee08 counted-file.xml.20131003-083611" | 
    cut -d: -f2 | 
    cut -d. -f1-2 
ea66574ff0daad6d0406f67e4571ee08 counted-file.xml 

注意,這依賴於點.存在爲counted-file.xml

2

不知道這是對你合適:

sed 's/^.*:\(.*\)\.[^.]*$/\1/' 

你的榜樣:

kent$ echo "/home/konnor/md5sums:ea66574ff0daad6d0406f67e4571ee08 counted-file.xml.20131003-083611"|sed 's/^.*:\(.*\)\.[^.]*$/\1/'           
ea66574ff0daad6d0406f67e4571ee08 counted-file.xml 

grep的線工程太:

grep -Po ':\K.*(?=\..*?$)' 
2
$ awk -F[:.] -v OFS="." '{print $2,$3}' <<< "/home/konnor/md5sums:ea66574ff0daad6d0406f67e4571ee08 counted-file.xml.20131003-083611" 
ea66574ff0daad6d0406f67e4571ee08 counted-file.xml