Selected matching lines

two files: one with the line number only, and the 2nd one with line number and content, as following:

line_file.txt
1
3
5
9
23
30
content_file.txt
1|we are the world|good|great
2|easily do this by highlighting you|easily do this by highlighting you|easily do this by highlighting you
3|itles when posting. For exam|itles when posting. For exam|itles when posting. For exam
7|itles when posting. For examitles when posting. For exam|itles when posting. For exam
9|itles when posting. For exam|itles when posting. For exam|itles when posting. For exam
...

the desired output is to get all content with the matching line numbers from line_file.txt

output.txt
1|we are the world|good|great
3|itles when posting. For exam|itles when posting. For exam|itles when posting. For exam
9|itles when posting. For exam|itles when posting. For exam|itles when posting. For exam
...

Thank you very much.

awk -F"|" 'FNR==NR {a[$1]++;next}{if ($1 in a) {print}}' line_file.txt content_file.txt
1 Like

Longhand using __builtins__ only, OSX 10.7.5, default bash terminal...

#!/bin/bash
# line_select.sh
echo '1
3
5
9
23
30' > /tmp/line_file.txt
echo '1|we are the world|good|great
2|easily do this by highlighting you|easily do this by highlighting you|easily do this by highlighting you
3|itles when posting. For exam|itles when posting. For exam|itles when posting. For exam
7|itles when posting. For examitles when posting. For exam|itles when posting. For exam
9|itles when posting. For exam|itles when posting. For exam|itles when posting. For exam' > /tmp/content_file.txt
> /tmp/combined_file.txt
ifs_str="$IFS"
while read line
do
	while read text
	do
		IFS='|'
		textarray=($text)
		if [ "$line" == "${textarray[0]}" ]
		then
			IFS=$'\n'
			echo "${text[0]}" >> /tmp/combined_file.txt
			break
		fi
	done < /tmp/content_file.txt
done < /tmp/line_file.txt
cat < /tmp/combined_file.txt
echo "Done..."
IFS="$ifs_str"
exit 0

Results:-

Last login: Thu Apr 24 21:01:44 on ttys000
AMIGA:barrywalker~> chmod 755 line_select.sh
AMIGA:barrywalker~> ./line_select.sh
1|we are the world|good|great
3|itles when posting. For exam|itles when posting. For exam|itles when posting. For exam
9|itles when posting. For exam|itles when posting. For exam|itles when posting. For exam
Done...
AMIGA:barrywalker~> _