This is essentially the command I want, All of it works except that I want to print something special in my third column that would use shell commands(or just more awk commands I guess but I don't know how I would fit this into the original awk statement). All I need help with is the pseudo command substitution between , and ar[,
This is essentially the command I want, All of it works except that I want to print something special in my third column that would use shell commands(or just more awk commands I guess but I don't know how I would fit this into the original awk statement). All I need help with is the pseudo command substitution between $2, and ar[$4,$1] in the print statement but left the rest in for the sake of specificity.
这本质上是我想要的命令,所有这些都可以工作,除了我想在我的第三列中打印一些特殊的东西,它会使用shell命令(或者我想的更多awk命令但是我不知道我怎么会把它放到原始的awk声明)。我需要帮助的是打印语句中$ 2和ar [$ 4,$ 1]之间的伪命令替换,但为了特殊性,剩下的就是其余部分。
awk 'NR==FNR{ar[$3,$2]=$1+ar[$3,$2]; }
NR>FNR && ar[$4,$1] {print "hs"$1,$2,`awk '$1 == #$1 from outer awk command# file2 | tail -n 1 | awk '{print $3}'`, ar[$4,$1]}' file1 file2
file1 will look like
file1看起来像
5 8 t11
15 7 t12
3 7 t14
file2 will look like
file2看起来像
8 4520 5560 t11
8 5560 6610 t12
8 6610 7400 t13
7 9350 10610 t11
7 10610 11770 t12
7 11770 14627 t13
7 14627 16789 t14
And output should look like
输出应该看起来像
8 4520 7400 5
7 10610 16789 15
7 14647 16789 3
Thank-You!
谢谢!
1 个解决方案
#1
2
Non-awk, inefficient shell tools code:
非awk,低效的shell工具代码:
while read a b c ; do \
echo -n "$b " ; \
egrep "^$b " file2 | \
grep -A 9999999 " $c" | \
cut -d' ' -f2,3 | \
sed '1{s/ .*//;t}
${s/.* //;t};d' | \
xargs echo -n ; \
echo " $a" ; \
done < file1 | \
column -t
Output:
输出:
8 4520 7400 5
7 10610 16789 15
The main loop inputs file1 which controls what in file2 needs to be printed. file1 has 3 fields, so read needs 3 variables: $a, $b, and $c. The output uses $b and $a, so those two variables come "for free" -- the first and last lines of the main loop, (both echos), prefix $b and suffix $a to the two numbers in the middle of each line.
主循环输入file1,它控制file2中需要打印的内容。 file1有3个字段,因此read需要3个变量:$ a,$ b和$ c。输出使用$ b和$ a,所以这两个变量“免费” - 主循环的第一行和最后一行(两个回声),前缀$ b和后缀$ a到中间的两个数字每一行。
The egrep prints every line in file2 that begins with $b, but of those lines we only want the one that ends in $c plus the lines after that, which is what grep -A ... prints. Only the middle two columns are needed, so cut prints just those columns. Now we have a two column block of numbers, and we only want the upper left corner, or the lower right corner, which the sed code prints...
egrep打印file2中以$ b开头的每一行,但是在那些行中我们只想要以$ c结尾的行加上之后的行,这就是grep -A ...打印的行。只需要中间两列,因此剪切仅打印那些列。现在我们有一个两列数字块,我们只想要sed代码打印的左上角或右下角...
Any sed code automatically counts lines as it runs. When sed hits the first line, it runs what's in the first set of curly brackets, ('1{<code>}'). If that fails sed checks if it's the last line, ($ means last line), if it is, sed runs what's in the second set of curly brackets, ('${<code>}'). If it's not the first or last line sed deletes it.
任何sed代码在运行时自动计算行数。当sed命中第一行时,它会运行第一组花括号中的内容('1 {}')。如果失败则sed检查它是否是最后一行($表示最后一行),如果是,则sed运行第二组花括号中的内容('$ {}')。如果它不是第一行或最后一行,则删除它。
Inside those curly brackets: s/ .*// works just like cut -f 1 would. The closing t means 'GOTO label', but when there's no 'label' sed just starts a new cycle, reading another line -- without t, the code would run the d, and print nothing. With two fields, s/.* // works like cut -f 2, etc.
在那些花括号内:s /。* //就像cut -f 1那样工作。结束t表示'GOTO标签',但是当没有'标签'时,sed只是开始一个新的循环,读取另一行 - 没有t,代码将运行d,并且什么都不打印。有两个字段,s /.* //就像cut -f 2一样工作,等等。
Each pass of the main while loop sed prints two numbers, but each is on it's own line. Piping that to xargs echo -n puts both numbers on the same line as the $b was printed on.
主while while循环sed的每次传递打印两个数字,但每个都在它自己的行上。管道到xargs echo -n将两个数字放在与打印$ b相同的行上。
] in the print statement but left the rest in for the sake of specificity.This is essentially the command I want, All of