today#123
2934
9236
monday
today#12341
4246
58234
monday
today#456
7768
32347
monday
但是在我的文件中大约有200k +行,但它是以"今天"开头的部分制作的。并以"星期一"
结束我可以轻松地将一个或所有部分与:
分开awk '/today/ {show=1} show; /monday/ {show=0}' file.txt
但是我无法找到如何找到带有特殊字符串的部分(在本例中为7768) 。谁能帮我 ?
1。)每个部分都有随机数行
2。)文件不断变化(每天一次或两次)
结果应该是这样的:
today#456
7768
32347
monday
谢谢。
答案 0 :(得分:2)
使用awk:
awk 'show && c{
if(show=$1==7768)print c;
c=""
}
show;
/monday/{
show=0
}
/today/{
show=1;
c=$0
}
' infile
输出:
$ awk 'show && c{if(show=$1==7768)print c;c=""}show;/monday/{show=0}/today/{show=1;c=$0}' infile
today#456
7768
32347
monday
输入:
$ cat infile
today#123
2934
9236
monday
today#12341
4246
58234
monday
today#456
7768
32347
monday
答案 1 :(得分:2)
关注# imports
import numpy as np
from skimage import exposure
from joblib import Parallel, delayed
# number of processes
nprocs = 10
# batched image array
img_arr = np.random.randint(0, 255, (1000, 32, 32, 3))
# function to be applied on all images
def process_image(img):
img_eq = exposure.equalize_hist(img)
return img_eq
result = []
# run `process_image()` in parallel
result.extend(Parallel(n_jobs=nprocs)(delayed(process_image)(img_arr[idx]) for idx in range(img_arr.shape[0])))
也可能对您有所帮助。我正在设置一个名为awk
的变量,您可以在其中提供您想要查找的任何值,除了变量命名值的值之外,不需要更改代码中的任何内容。
value
输出如下。
awk -v value="7768" '
/monday/ && flag{
print;
flag=val=""
}
/today/{
val=$0;
next
}
$0 ~ value{
flag=1;
print val RS $0;
next
}
flag && val
' Input_file
说明: 现在也为上述代码添加说明。
today#456
7768
32347
Monday
答案 2 :(得分:1)
您可以为此编写一个bash脚本recordfinder.sh
。它可能如下所示:
# cat recordfinder.sh
#!/bin/bash
exitfn(){
echo "Usage : recordfinder.sh <filename> <searchstring>"
[ "$1" -eq 1 ] && echo "Couldn't open file" && exit 1
[ "$1" -eq 2 ] && echo "No search string provided" && exit 2
}
[ -f "$1" ] || exitfn 1
[ -z "$2" ] && exitfn 2
awk -v str="$2" -v RS="" '$0 ~ str' "$1"
# ./recordfinder.sh filename 7768
today#456
7768
32347
monday
希望它能给你一些灵活性: - )
答案 3 :(得分:1)
sed -n '/today/{:a;/monday/{/\n4246\n/p;b};N;ba}'
^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | |
dont print all lines-----+ | | | | | | | |
| | | | | | | |
if found start of block (today)+ | | | | | | |
then start loop with label (a)-----+ | | | | | |
if found end of block (monday)----------+ | | | | |
then check if patterm (4246) found---------------+ | | | |
if found, then print this buffer--------------------+ | | |
break the loop------------------------------------------+ | |
load another line into buffer--------------------------------+ |
and loop (goto label (a))---------------------------------------+
测试:
$ sed -n '/today/{:a;/monday/{/\n4246\n/p;b};N;ba}' <sample.txt
today#12341
4246
58234
monday