The SED command gets the nth tab-separated value between lines x and y… here is a solution to the problem.
The SED command gets the nth tab-separated value between lines x and y
I’ve been able to extract certain lines from one large tab-delimited text file and write them to another :
sed -n 100,200p file.tsv >> output.txt
However, I
actually tried to get the 8th tab-delimited value from each line and write them to a comma-separated file, but I couldn’t find the correct syntax for pattern matching, despite reading dozens of online articles.
For each time, I’m basically trying to match
$2
at /([^\t]*\t){7}([0-9]*).*/
No luck.
The line in the text file file.tsv resembles :
01 name1 title1 summary1 desc1 image1 url1 120019 time1
02 name2 title2 summary2 desc2 image2 url2 576689 time2
Can anyone help me with this?
Solution
Perl one-line code:
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