Copying lines from fileA if they start by a word from fileB


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying lines from fileA if they start by a word from fileB
# 1  
Old 05-22-2009
Copying lines from fileA if they start by a word from fileB

Hi I'm in the need of a script that basically takes two files and generates a 3rd. It reads from fileA and fileB and copies lines from fileA if they start by a word of fileB.

for example

fileA
The dog is beautful
Where is your cat
Why are you sad?
Help me!

fileB
The
Where
tree
dog

fileC would be like this
The dog is beautful
Where is your cat
# 2  
Old 05-22-2009
Code:
awk 'FILENAME=="FileB" {arr[$0]++}
       FILENAME=="FileA" {if($1 in arr) {print $0}    } '  FileB FileA > FileC

# 3  
Old 05-22-2009
Woah thanks a bunch for the almost instantaneous response, just tested and it works !
# 4  
Old 05-22-2009
I have a similar thing I need:

I have a fileA with the following format
word(s) ||| word(s) ||| number
word(s) ||| word(s) ||| number
word(s) ||| word(s) ||| number

and a fileB which is just plain text. I want to copy into fileC the lines from fileA where the first word or sequence of words (everything before the first ||| delimiter) exists anywhere in the fileB.

For example if I have

abc def ||| foobar ||| 123

I want to copy the line only if fileB contains "abc def", not just abc.
# 5  
Old 05-22-2009
No homework. Thread closed. Infraction given.

Quote:
OrgName: Universite du Quebec en Outaouais
OrgID: UQEO-1
Address: 283, boulevard Alexandre-Tache
Address: Case postale 1250, succursale Hull
City: Gatineau
StateProv: QC
PostalCode: J8X-3X7
Country: CA

NetRange: 132.213.0.0 - 132.213.255.255
CIDR: 132.213.0.0/16
NetName: UQO-132-213-0-0
NetHandle: NET-132-213-0-0-1
Parent: NET-132-0-0-0-0
NetType: Direct Assignment
NameServer: JUPITER.UQO.CA
NameServer: AMNESIX.UQSS.UQUEBEC.CA
Comment:
RegDate: 1989-03-16
Updated: 2007-04-17

OrgTechHandle: TELEC8-ARIN
OrgTechName: Telecom
OrgTechPhone: +1-819-595-3900
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Grep from FileA, search in FileB, edit FileC > Output

Hello, Similar question to my previous posts. I am sorry for the trouble... Just checked my old threads but I can not implement any solution into this case.. My aim is to grab each line in fileA, check it in fileB and merge with fileC (tab separated) in corresponding line as given below: FileA:... (2 Replies)
Discussion started by: baris35
2 Replies

3. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Perl search csv fileA where two strings exist on another csv fileB

Hi I have two csv files, with the following formats: FileA.log: Application, This occured blah Application, That occured blah Application, Also this AnotherLog, Bob did this AnotherLog, Dave did that FileB.log: Uk, London, Application, datetime, LaterDateTime, Today it had'nt... (8 Replies)
Discussion started by: PerlNewbRP
8 Replies

5. Shell Programming and Scripting

Start copying large file while its still being restored from tape

Hello, I need to copy a 700GB tape-image file over a network. I want to start the copy process before the tape-image has finished being restored from the tape. The tape restore speed is about 78 Mbps and the file transfer speed over the network is about 45 Mbps I don't want to use a pipe, since... (7 Replies)
Discussion started by: swamik
7 Replies

6. Shell Programming and Scripting

regex - start with a word but ignore that word

Hi Guys. I guess I have a very basic query but stuck with it :( I have a file in which I want to extract particular content. The content is between standard format like : Verify stats A=0 B=12 C=34 TEST Failed Now I want to extract data between "Verify stats" & "TEST Failed" but do... (6 Replies)
Discussion started by: ratneshnagori
6 Replies

7. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

8. Shell Programming and Scripting

start to print when a specified word is matched

Hello everybody, How can I start to print an output when a word hits the pattern space. for example: file.txt : first line second line third line fourth line ... cat file.txt > function "second" should print second line third line fourth line ... thanks for the replies now... (3 Replies)
Discussion started by: Oddant
3 Replies

9. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

10. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies
Login or Register to Ask a Question