开发者

SED command to get nth tab separated value between lines x and y

I have been able to extract certain lines from a large tab-separated text file and write them to another file:

sed -n 100,200p file.tsv >> output.txt

However, I am actually trying to grab the 8th tab-separated value from each line and wr开发者_开发技巧ite them to a file comma separated, but I cannot find the right syntax to use for the pattern matching, despite reading dozens of online articles.

For each time I have basically been trying to match

$2 in /([^\t]*\t){7}([0-9]*).*/

with no luck.

The lines within the text file file.tsv resemble:

01  name1   title1  summary1    desc1   image1  url1    120019  time1
02  name2   title2  summary2    desc2   image2  url2    576689  time2

Please can anyone help me with this query?


A Perl one-liner:

perl -F'\t' -ane 'push @csv, $F[7] if $. > 100 && $. < 200; END { print join ",", @csv if @csv }' /path/to/input/file > /path/to/output/file


Here it is using GNU sed and extended expressions:

sed -nre '100,200{s/^(\S+\s+){7}(\S+).*$/\2/;p}' file.tsv

Here it is using POSIX only:

sed -n '100,200{s/^\([^[:space:]]\+[[:space:]]\+\)\{7\}\([^[:space:]]\+\).*$/\2/;p}' file.tsv

I do agree with Alf that awk would be a better fit for this.

Here is the awk solution with line limits:

awk 'NR==100,NR==200{print $8}' file.tsv


I think I would rather use awk that way:

$ awk '{ print col 8 : $8 }' file

The forward work will be easier I guess.


This will work if there are empty fields.

sed -nre '100,200{s/^(([^\t]*)\t){7}([^\t]*)(\t.*|$)/\3/;p}' file.tsv
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜