我想从名为datax.txt
第二行的文件中提取:
0/0/0/0/0/0 | 0/0/0/0/0/0 | 0/0/0/0/0/0
然后我想将 3 个序列存储在 3 个变量中0/0/0/0/0/0
。
我该怎么办?
关键是将问题一分为二:
cut
是其中之一对于未来的问题,一定要包含一个更完整的数据集,现在是一个。我稍微更改了第二行,以便我们可以验证我们得到了正确的列:
文件.txt
4/4/4/4/4/4 | 4/4/4/4/4/4 | 4/4/4/4/4/4
0/0/0/0/a/0 | 0/0/0/0/b/0 | 0/0/0/0/c/0
8/8/8/8/8/8 | 8/8/8/8/8/8 | 8/8/8/8/8/8
8/8/8/8/8/8 | 8/8/8/8/8/8 | 8/8/8/8/8/8
然后是基于上述两个关键操作的适当脚本:
提取.bash
file=$1
target_line=2
# get the n-th line
# https://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file
line=$(cat $file | head -n $target_line | tail -1)
# get the n-th field on a line, using delimiter '|'
var1=$(echo $line | cut --delimiter='|' --fields=1)
echo $var1
var2=$(echo $line | cut --delimiter='|' --fields=2)
echo $var2
var3=$(echo $line | cut --delimiter='|' --fields=3)
echo $var3
aaand:
$ ./extract.bash f.txt
0/0/0/0/a/0
0/0/0/0/b/0
0/0/0/0/c/0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句