Visit Our UNIX and Linux User Community


extract particular lines from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract particular lines from text file
# 1  
Old 08-31-2009
extract particular lines from text file

I have two files

file A which have a number in every row and file B which contains few hundred thousand rows with about 300 characters in each row (csv)

What I need is to extract whole rows from B file (only these which numbers are indicated in A file)

I also need to use cygwin.

Any help will be appreciated.
# 2  
Old 08-31-2009
It will be good if you show a sample input and desired output???
# 3  
Old 08-31-2009
Code:
A file input:

3
4
6
8
10


B file input 
aaa|sdftqwtegaerta|Adfgadfhgsa
bbb|asdgfasdgart|fghsdfhsdfhs
ccc|dag5dsgarf|fhsdfh7
ddd|da4dsgarf|fhsdfhsdhf
eee|dagsdgadsgarf|fhsdfhsdhf
fff|dags3sgarf|fhsdfhsdy
ggg|dagsdgadsgarf|fhsdfhsdhf
hhh|dags345gadsgarf|fhsdrf
jjj|dagsdgadsgarf|fhsdfh34
kkk|dagsdgadsgarf|fhs354
lll|dagsdgadsgarf|fhsd54

desired output
ccc|dag5dsgarf|fhsdfh7
ddd|da4dsgarf|fhsdfhsdhf
fff|dags3sgarf|fhsdfhsdy
hhh|dags345gadsgarf|fhsdrf
kkk|dagsdgadsgarf|fhs354

# 4  
Old 08-31-2009
Try...

Code:
 
awk -F"|" 'NR==FNR{arr[FNR]=$0;next}{for(i in arr)if(i==$1)print arr[i]}' fileB fileA

# 5  
Old 08-31-2009
A sed version :

Code:
for i in `cat file1.txt`
do
sed -n "$i p" file2.txt >> op.txt
done

# 6  
Old 09-13-2009
Quote:
Originally Posted by malcomex999
Try...

Code:
 
awk -F"|" 'NR==FNR{arr[FNR]=$0;next}{for(i in arr)if(i==$1)print arr[i]}' fileB fileA

thx, awk version works fine for me
# 7  
Old 09-13-2009
Alternative

This is an alternative if you have more file than memory can hold. This only holds the indexes in memory instead of the data file...

Code:
awk 'NR==FNR{arr[$1]=0;next} (FNR in arr){print $0}' fileA fileB


Previous Thread | Next Thread
Test Your Knowledge in Computers #815
Difficulty: Medium
In CSS, E[foo] matches an E element with a 'foo' class name.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Extract lines from text files

I have some files containing the following data # RESIDUE AA STRUCTURE BP1 BP2 ACC N-H-->O O-->H-N N-H-->O O-->H-N TCO KAPPA ALPHA PHI PSI X-CA Y-CA Z-CA 1 196 A M 0 0 230 0, 0.0 2,-0.2 0, 0.0 0, 0.0 0.000 360.0 360.0 360.0 76.4 21.7 -6.8 11.3 2 197 A D + 0 0 175 1,-0.1 2,-0.1 0, 0.0 0, 0.0... (10 Replies)
Discussion started by: edweena
10 Replies

3. Shell Programming and Scripting

extract lines from text after keyword

I have a text and I want to extract the 4 lines following a keyword! For example if I have this text and the keyword is AAA hello helloo AAA one two three four helloooo hellooo I want the output to be one two three four (7 Replies)
Discussion started by: stekanius
7 Replies

4. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

5. UNIX for Dummies Questions & Answers

Help please, extract multiple lines from a text file

Hi all, I need to extract lines between the lines 'RD' and 'QA' from a text file (following). there are more that one of such pattern in the file and I need to extract all of them. however, the number of lines between them is varied in the file. Therefore, I can not just use 'grep -A' command.... (6 Replies)
Discussion started by: johnshembb
6 Replies

6. Shell Programming and Scripting

Extract two lines before and after the 'search text'

Hi Guys, I have a situation wherein I need to extract two lines from below the search string. Eg. Current: $ grep "$(date +'%a %b %e')" alert.log Mon Apr 12 03:58:10 2010 Mon Apr 12 12:51:48 2010 $ Here I would like the display to be something like Mon Apr 12... (6 Replies)
Discussion started by: geetap
6 Replies

7. Shell Programming and Scripting

AWK: How to extract text lines between two strings

Hi. I have a text test1.txt file like:Receipt Line1 Line2 Line3 End Receipt Line4 Line5 Line6 Canceled Receipt Line7 Line8 Line9 End (9 Replies)
Discussion started by: TQ3
9 Replies

8. Shell Programming and Scripting

Extract lines of text based on a specific keyword

I regularly extract lines of text from files based on the presence of a particular keyword; I place the extracted lines into another text file. This takes about 2 hours to complete using the "sort" command then Kate's find & highlight facility. I've been reading the forum & googling and can find... (4 Replies)
Discussion started by: DionDeVille
4 Replies

9. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

10. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

Featured Tech Videos