gunio
1
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.
It will be good if you show a sample input and desired output???
gunio
3
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
Try...
awk -F"|" 'NR==FNR{arr[FNR]=$0;next}{for(i in arr)if(i==$1)print arr}' fileB fileA
panyam
5
A sed version :
for i in `cat file1.txt`
do
sed -n "$i p" file2.txt >> op.txt
done
gunio
6
thx, awk version works fine for me
jp2542a
7
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...
awk 'NR==FNR{arr[$1]=0;next} (FNR in arr){print $0}' fileA fileB
gunio
8
What if I wanted to remove those lines from the original file at the same time?