我有一条日志,每分钟都会收到以下数据:
ID: Unit3443 IP: 192.168.1.1 MAC: 00:20:c0:04:e8:ab
ID: Unit2222 IP: 192.168.2.2 MAC: 00:40:c0:05:e8:bc
我抓住了尾巴为-1 / File的最后一行
是否可以一口气将这两个条目分配给变量?目前我有
UnitID=`tail -1 $File|awk '{print $1}'`
UnitIP=`tail -1 $File|awk '{print $2}'`
这可行,但是它要求文件被读取两次,而我担心的是在第二次读取时,tail -1的输出将不再相同。
是否可以在该行的一次读取中同时分配这两个变量?
答案 0 :(得分:2)
arr=( $(tail -1 "$file" | awk '{print $1, $2}') )
UnitID=${arr[0]}
UnitIP=${arr[1]}
或:
id=0
ip=1
unit=( $(tail -1 "$file" | awk '{print $1, $2}') )
echo "${unit[id]}"
echo "${unit[ip]}"
或:
line=$(tail -1 "$file" | awk '{print $1, $2}')
UnitID="${line% *}"
UnitIP="${line#* }"
答案 1 :(得分:1)
另一种替代方法,而不是使用@Pesa建议的xdf <- read_sl2("~/Downloads/Chart 09_07_2018 [2].sl2")
## Format: sl2
## Block size: downscan
## .............
xdf
## # A tibble: 1,308 x 22
## channel upperLimit lowerLimit frequency waterDepth keelDepth speedGps temperature lng_enc lat_enc
## <chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <int> <int>
## 1 Secondary 0 13.3 200 KHz 2.62 0.328 0.5 15.8 4433307 7003054
## 2 DSI (Downsc… 0 13.4 200 KHz 2.62 0.328 0.5 15.8 4433307 7003054
## 3 Primary 0 13.3 200 KHz 2.62 0.328 0.5 15.9 4433307 7003054
## 4 Secondary 0 13.3 200 KHz 2.62 0.328 0.5 15.9 4433307 7003054
## 5 DSI (Downsc… 0 13.4 200 KHz 2.59 0.328 0 15.8 4433307 7003054
## 6 Secondary 0 13.3 200 KHz 2.59 0.328 0 15.8 4433307 7003054
## 7 Secondary 0 13.3 200 KHz 2.52 0.328 0 15.9 4433307 7003054
## 8 DSI (Downsc… 0 13.4 200 KHz 2.52 0.328 0 15.9 4433307 7003054
## 9 Primary 0 13.3 200 KHz 2.52 0.328 0 15.8 4433307 7003054
## 10 DSI (Downsc… 0 13.4 200 KHz 2.52 0.328 0 15.8 4433307 7003054
## # ... with 1,298 more rows, and 12 more variables: speedWater <dbl>, track <dbl>, altitude <dbl>,
## # heading <dbl>, timeOffset <int>, headingValid <lgl>, altitudeValid <lgl>, gpsSpeedValid <lgl>,
## # waterTempValid <lgl>, positionValid <lgl>, waterSpeedValid <lgl>, trackValid <lgl>
,这将是一次glimpse(xdf)
## Observations: 1,308
## Variables: 22
## $ channel <chr> "Secondary", "DSI (Downscan)", "Primary", "Secondary", "DSI (Downscan)", "Sec...
## $ upperLimit <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ lowerLimit <dbl> 13.3, 13.4, 13.3, 13.3, 13.4, 13.3, 13.3, 13.4, 13.3, 13.4, 13.3, 13.4, 13.3,...
## $ frequency <chr> "200 KHz", "200 KHz", "200 KHz", "200 KHz", "200 KHz", "200 KHz", "200 KHz", ...
## $ waterDepth <dbl> 2.620, 2.620, 2.620, 2.620, 2.586, 2.586, 2.516, 2.516, 2.516, 2.516, 2.516, ...
## $ keelDepth <dbl> 0.328084, 0.328084, 0.328084, 0.328084, 0.328084, 0.328084, 0.328084, 0.32808...
## $ speedGps <dbl> 0.5, 0.5, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0....
## $ temperature <dbl> 15.84112, 15.84112, 15.86293, 15.86293, 15.79128, 15.79128, 15.86293, 15.8629...
## $ lng_enc <int> 4433307, 4433307, 4433307, 4433307, 4433307, 4433307, 4433307, 4433307, 44333...
## $ lat_enc <int> 7003054, 7003054, 7003054, 7003054, 7003054, 7003054, 7003054, 7003054, 70030...
## $ speedWater <dbl> 0.5, 0.5, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0....
## $ track <dbl> 4.974188, 4.974188, 4.974188, 4.974188, 4.974188, 4.974188, 4.974188, 4.97418...
## $ altitude <dbl> 324.7375, 324.7375, 324.7375, 324.7375, 324.8687, 324.8687, 324.8687, 324.868...
## $ heading <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ timeOffset <int> 1317703, 1317706, 1318036, 1318905, 1318946, 1318982, 1319130, 1319140, 13192...
## $ headingValid <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, ...
## $ altitudeValid <lgl> TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FAL...
## $ gpsSpeedValid <lgl> TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FAL...
## $ waterTempValid <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, ...
## $ positionValid <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, ...
## $ waterSpeedValid <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, ...
## $ trackValid <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, ...
并使用两次结果,以避免在read
之间更改文件时出现问题
tail
注意:我也将示例更改为使用tail
而不是反引号-的工作方式相同,但更容易理解。
答案 2 :(得分:1)
您可以这样做:
dput
测试结果
df2 <- tibble(
id = rep(1:9, each = 5, times = 2),
date = rep(seq(as.POSIXct("2018-01-01 00:00:00"), by = "month", length.out = 5), times = 18),
band = rep(c("b1", "b2"), each = 45),
value = c(rnorm(45, 0), rnorm(45, 1))
)
正如@EdMorton在评论中所指出的那样,并非所有read UnitID UnitIP < <(awk 'END{print $2, $4}' file)
变体都将$ 0 .. $ NF变量保留在echo $UnitID
Unit2222
echo $UnitIP
192.168.2.2
部分中,因此希望以下内容可以更广泛地应用:
awk