Selected matching lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selected matching lines
# 1  
Old 04-24-2014
Selected matching lines

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

Code:
line_file.txt
1
3
5
9
23
30

Code:
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
Code:
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.
# 2  
Old 04-24-2014
Code:
awk -F"|" 'FNR==NR {a[$1]++;next}{if ($1 in a) {print}}' line_file.txt content_file.txt

This User Gave Thanks to pilnet101 For This Post:
# 3  
Old 04-24-2014
Longhand using __builtins__ only, OSX 10.7.5, default bash terminal...
Code:
#!/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:-
Code:
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~> _


Last edited by wisecracker; 04-24-2014 at 05:38 PM.. Reason: Add the break command, copy and paste error by me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use same file selected in first bash process that has matching digits in it fot the second

In the below portion of a bash script the user selects a file from a directory. select file in $(cd /home/cmccabe/Desktop/NGS/API/5-14-2016/bedtools;ls);do break;done files in directory 123_base_counts.txt 456_base_counts.txt 789_base_counts.txt second portion of bash currently (user... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Deleting selected lines in a file

Hi Guys , I have two files say a1 and a2 having following contents a1 dag wfd a2 dag wfd chire hcm I want to delete only the lines in a2 which are in a1 and final output of a2 should be a2 chire hcm (6 Replies)
Discussion started by: Pradeep_1990
6 Replies

3. Shell Programming and Scripting

Delete selected lines

hi Gurus, I have a source file with more than 10 columns ( not fixed ) I want to delete all the lines on the following condition 1) where i have first column as "UPDATE PLAN ADD RATE SCHEDULE" and fourth column as null awk '($1=="UPDATE PLAN ADD RATE SCHEDULE" && $4=="") {print $0}'... (5 Replies)
Discussion started by: r_t_1601
5 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

6. Shell Programming and Scripting

finding least out of selected lines

Hello, I have a file, which looks like: I want to print the row containg "PRO" in second column after comparing and finding the minimum value of fifth column present in all "PRO". and likewise for every other string present in second column. I am using : filename=list... (2 Replies)
Discussion started by: CAch
2 Replies

7. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

8. Shell Programming and Scripting

Process selected lines

I have an if statement where I state that if there are more than one records (lines) found containing a string in a file, then it enters into a while loop to use each line for as many lines as there are and then stop. Trouble is, I can't figure out how to move to the next instance of each line. ... (2 Replies)
Discussion started by: derekphl
2 Replies

9. UNIX for Dummies Questions & Answers

Copy selected lines in vim

Hi, I am looking to copy selected lines from a file using the vim editor. I have looked up a few resources and they have suggested to use this- Type mk Type: "ay'k (double quotes, <register name from a-z>, <y-yank single quote, k You can paste those lines wherever you want with "ap I tried... (7 Replies)
Discussion started by: coolavi
7 Replies

10. Shell Programming and Scripting

print selected lines

Hi everybody: I try to print in new file selected lines from another file wich depends on the first column. I have done a script like this: lines=( "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "21" "31" "41" "51" "55" "57" "58" ) ${lines} for lines in ${lines} do awk -v ... (6 Replies)
Discussion started by: tonet
6 Replies
Login or Register to Ask a Question