我想打印整行与特定的列开始计数变量的文本。
file.log format
1 101010 101010 4.0001 my home
2 101010 101010 5.0001 my home
3 101010 101010 6.0001 my home
4 101010 101010 7.0001 my home
my script
count=4
awk -v cnt="$count" '$4 ~ /^[cnt]\./' file.log
我要这个结果。
1 101010 101010 4.0001 my home
,,但我的结果没什么.
请告诉我我
我花了几个小时学习Awk教程,但我无法绕过这个问题:我想使用一个变量作为awk查询的正则表达式。下面是我想要实现的一个示例:
#!/bin/bash
#My test array
testarray=(teststring[1078] teststringthatshouldnotmatch teststring[5845])
#myregex as a variable
regex="teststring\[.*"
#the awk
for value in ${testarray[*]}
do
echo ${value} | awk '{if ($1 ~ reg
我试图通过html源代码进行解析。在我的例子中,我只是在呼应它。但是,实际上我正在从一个文件中读取html。
这里有一些代码,在语法上是可以工作的:
echo "<td>Here</td> some dynamic text to ignore <garbage> is a string</table>more junk" |
awk -v FS="(<td>|</td>|<garbage>|</table>)" '{print $2, $4}'
在F
我使用过的典型tsv文件片段
10 Interstellar Main Theme Extended UDVtMYqUAyw
11 Journey XvG78AmBLc4
12 Jurassic Park Music & Ambience Amazing Soundscapes and Music PPl__iyIg6w
13 Lord of the Rings Sound of The Shire chLZQtCold8
14 Lord of the Rings The Shire: Sunset at Bag End uBmbI8dzc-M
在
我需要一个与any character(s) followed by foo. or bar. followed by anything followed by is.a.server followed by anything匹配的正则表达式。
例如:
"foo.lnx station is.a.server" # match this
"my bar.unx node is.a.server.and.client" # match this
"baz station is.a.server" # do NOT not match this
我想用我从文件中读取的代码替换awk中的常量javascript行。在bash的命令行中以交互方式运行此操作:
CUSTOM_CODE=`cat custom_code.txt`
awk -v r=$CUSTOM_CODE '{gsub(/export default function\(\) \{/,r)}1' main.js > main-patched.js
问题是,如果我将这2个命令放在bash文件中,那么它将不再适用于以下awk错误:awk: cannot open myobj (No such file or directory)。
样本文件:
main
我有一个listA,它看起来像这样:
gene1
gene2
gene11
gene22
gene23
我也没有几个标签分隔的文本文件(*hist.txt),其第4列与列表中的基因匹配。我想从标签分隔的文本文件中提取listA中每个基因的值。
这就是我写的
for i in `cat listA.txt`
do
for a in *hist.txt
do
fn=${a%%_*}
cat $a | awk -v OFS="\t" -v fn="$fn" -v pattern="$i" '$4
在日志文件中,我想将以模式“印象DE表”开头的所有行截断为40个字符。我怎么能这么做?
例如,如果file.log包含行:
IMPRESSION DE LA TABLE A.B.C 12345
^ 40th character
那么我想保持:
IMPRESSION DE LA TABLE A.B.C
我绝对需要保留表的名称(在我的示例中是A.B.C)。
使用sed,我可以删除遵循模式的内容,但不能保留其他字符:
sed 's/ IMPRESSION DE LA
我的第二列值从0开始到2000年,变量$a设置为13。它打印了所有的价值。代码:
#!/bin/sh
IFS=- read a b <<< "$1"
echo $a # value is displayed as 13
echo $b # value is displayed as 20... for now it does not matter
sort -r -k 2,2 $2 | awk '{if ($2 > $a) print $2}' # if statement should check which of a second
你能帮我解决这个难题吗?我正在尝试打印文件中字符串(即行#)的位置,首先打印到std输出,然后在一个变量中捕获该值,供以后使用。该字符串为“myFile”,文件名为“myFile”,定义如下:
this is first line
this is second line
this is my string on the third line
this is fourth line
the end
现在,当我在命令提示符下直接使用这个命令时:
% awk ‘s=index($0, “my string”) { print “line=” NR, “position= ” s}’ myFile
我