SED command to display the nth tab between lines x and y

I managed to extract certain lines from a large text file separated by delimiters and write them to another file:

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

However, I'm actually trying to grab the 8th value with tab delimiters from each line and write them in comma-separated files, but I cannot find the correct syntax for pattern matching, despite reading dozens of online articles.

For every time I tried to match

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

no luck.

The lines in 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 help me with this question?

+3
source share
4 answers

GNU sed :

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

POSIX:

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

Alf, awk .

awk :

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

Perl:

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
+2

I think I prefer to use awk this way:

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

Advanced work will be easier, I think.

+1
source

This will work if there are empty fields.

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

Source: https://habr.com/ru/post/1777203/


All Articles