6

I have a control file - cntl.txt

2
3
5

Data file - data.txt

red
blue
yellow
green
violet
orange

I need to reading the lines matching from control file, here the output expected is:

blue
yellow 
violet
don_crissti
  • 82,805
Parthi
  • 85

5 Answers5

5

Example of a very inefficient solution:

for i in $(<control.txt); do awk -v c=$i 'NR~c{ print $0 }' data.txt; done;

I report also a good solution I learned tonight:

awk 'FNR==NR{ z[$0]++;next }; FNR in z' control.txt data.txt
andreatsh
  • 2,025
4

Using only POSIX specified features of Sed:

sed -n -e "$(sed '/./s/$/p/' cntl.txt)" data.txt

Of course if your cntl.txt file has lines besides numbers, you may get an error. But if it has empty lines these will be handled correctly (i.e. they will not affect the output).

Wildcard
  • 36,499
3

Try this:

join <(nl data.txt|sort -k1b,1) <(cat cntl.txt|sort -k1b,1) | sort -nk1,1 | cut -d' ' -f2-

nl - will enumerate lines for you

 1  red
 2  blue
 3  yellow
 4  green
 5  violet
 6  orange

| sort -k1b,1 - will sort them by the line number (first field), lexicographically

cat cntl.txt| sort -k1b,1 - will sort the control file in the same order

2
3
5

join <() <() - will join the sorted (and numbered) "data" with the sorted "control", on the first field (i.e. line number)

2 blue
3 yellow
5 violet

|sort -nk1,1 - will re-sort the results numerically (to put the lines back in order)

| cut -d' ' -f2- - will drop the line number field

blue
yellow
violet
zeppelin
  • 3,822
  • 10
  • 21
  • 1

    That seems silly

    That is not silly, as join operates on the lexicographically and not numerically sorted data.

    – zeppelin Nov 02 '16 at 21:58
  • Also, <(...) is a Bash-ism, not specified by POSIX

    Yep, this is probably not canonicaly POSIX, but it is not a bash-only extension either, at least ksh and zsh do support this too.

    – zeppelin Nov 02 '16 at 22:03
2

With sed only:

sed -n "$(sed -e 's/$/p;/' < cntl.txt)" data.txt
Ipor Sircer
  • 14,546
  • 1
  • 27
  • 39
  • 1
    Good, but no reason to use tr. Newlines are a perfectly acceptable command separator for Sed. (Also, $(...) is the preferred syntax for command substitution.) – Wildcard Nov 02 '16 at 22:10
-1

Another possible solution:

IFS=$'\n' read -d '' -r -a colors < 'data.txt'; unset IFS;

for i in $(<cntl.txt); do
        echo ${colors[i-1]} 
done

IFS line sets up internal file separator as newline and inserts each line from data.txt into array. After that you loop through lines in cntl.txt and print array elements with given index from it (minus 1 because you start your data.txt from 1, not from 0, otherwise it would be unnecessary).