2014-02-09 37 views

回答

0

嘗試像

#!/bin/bash 
while read -r line; do 
    [[ $line =~ '['(.*)']' ]] && echo "${BASH_REMATCH[1]} ${line##*' '}" 
done < file 

例如

> cat file 
34600 - - [30/Apr/1998:21:30:17 +0000] "GET /images/hm_bg.jpg HTTP/1.0" 200 24736 

> while read -r line; do [[ $line =~ '['(.*)']' ]] && echo "DATE=${BASH_REMATCH[1]} SIZE=${line##*' '}"; done < file 
DATE=30/Apr/1998:21:30:17 +0000 SIZE=24736 
0

您可以使用awk

awk '{sub(/[][]/,""); printf "DATE: %s SIZE: %s \n", $4,$NF}' file 
Date: 30/Apr/1998:21:30:17 Size: 24736
0

使用SED

sed -nr 's/.*\[([^]]*)\].* ([0-9]+)/\1 \2/p' file 

30/Apr/1998:21:30:17 +0000 24736 
相關問題