Visit The New, Modern Unix Linux Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep from FileA, search in FileB, edit FileC > Output
# 1  
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: just nba teams,
FileB: nba teams with a couple of team players (team-player-team-player-team-player)
FileC: my current file
Output: merged output
FileA:
Code:
Golden State Warriors
Boston Celtics
New York Knicks
Toronto Raptors
Chicago Bulls

FileB:
Code:
Golden State Warriors
Andrew_Bogut
Golden State Warriors
Quinn_Cook

Boston Celtics
PJ_Dozier
Boston Celtics
Jonathan_Gibson

New York Knicks
Mario_Hezonja
New York Knicks
Isaiah_Hicks
New York Knicks
John_Jenkins

FileC:
Code:
Golden State Warriors	Jordan_Bell
Boston_Celtics	Jaylen_Brown
New York Knicks	Henry_Ellenson
Toronto_Raptors	OG_Anunoby
Chicago Bulls	Raw_Alkins

Output:
Code:
Golden State Warriors	Jordan_Bell	Andrew_Bogut	Quinn_Cook
Boston Celtics	Jaylen_Brown	PJ_Dozier	Jonathan_Gibson
New York Knicks	Henry_Ellenson		Mario_Hezonja	Isaiah_Hicks	John_Jenkins
Toronto_Raptors		OG_Anunoby
Chicago Bulls	Raw_Alkins

Multiple loops caused intensive headache here..
I'd appreciate your help..

Edit: I am near to solution with awk.. Working on it...

I think that Vgersh's old post will do that...
Code:
awk -f test.awk FileA FileB FileC

test.awk
Code:
FNR==NR { a[$1];next}
$1 in a {a[$1]=a[$1] OFS $2}
END {
  for (i in a)
    print i,a[i]
}

Solved: Even though file format is a bit different, it works with some extra manipulation.

Thank you
Boris

Last edited by baris35; 05-18-2019 at 02:29 PM..
# 2  
I don't think that will fly. You seem to use underscores and spaces in team names inconsistently, don't set the correct OFS variable, and your file2 doesn't have a meaningful $2 in the structure that you show and describe.
Try instead
Code:
awk -F"\t" '
FNR == 1        {++FC
                }
FC == 1         {a[$1]
                 next
                }
FC == 2 && NF   {getline T
                 a[$1] = a[$1] (a[$1]?OFS:"")  T
                 next
                }
NF              {print $0, a[$1]
                 delete a[$1]
                }
' OFS="\t" fileA fileB fileC
 Golden State Warriors	Jordan Bell	Andrew Bogut	Quinn Cook
Boston Celtics	Jaylen Brown	PJ Dozier	Jonathan Gibson
New York Knicks	Henry Ellenson	Mario Hezonja	Isaiah Hicks	John Jenkins
Toronto Raptors	OG Anunoby	
Chicago Bulls	Raw Alkins	

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Thank you Rudic,
I had to change the files while testing but I will run as you leaded.


Kind regards
Boris

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #479
Difficulty: Medium
A address usually refers to a single byte of memory (called bitwise-addressing).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

awk or grep to search one column and output the other

Hello, it would be great if someone can help me with the following: I want to search for the rows from fileA in column 1 of fileB and output column 2 of fileB if found in fileC. In the moment I search within the complete file. How can I change the code so only column 1 is searched? cat fileA... (7 Replies)
Discussion started by: Manyaka
7 Replies

3. UNIX for Dummies Questions & Answers

Find a value inside file1 and replace it with another value from another fileB

i have a file with some data and i need replace a perticular value with some other value from another another file req: file1.txt abc,idle.txt,1234 file2.txt 5678 now the requirement is need to replace the "idle.txt" is with "mike_5678" here 5678 from file2.txt... file2.txt contains only... (2 Replies)
Discussion started by: dssyadav
2 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. OS X (Apple)

Need Help with GREP REGEX scripts for common BB-EDIT text-editing

Hi Everybody.. I'm a "newbie" to using Command-line... A few half-remembered DOS commands from 30 years ago, and the very handy "Sudo rm -R pathname" REMOVE command... I do a lot of "cleaning" of plain-text OCR text files. with assorted common line-break, punctuation and capitalization... (1 Reply)
Discussion started by: TheMacGuy
1 Replies

6. Shell Programming and Scripting

search and edit in the same file using awk

Hi, I am having a user.txt contains the name of users and passwd.txt file contains as passwd.txt $cat usr.txt root bin daemon cap $cat passwd.txt root:x:0:0:root:/root:/usr/bin/ksh bin:x:1:1:bin:/bin:/sbin/csh daemon:x:2:2:daemon:/sbin:/usr/bin/ksh adm:x:3:4:adm:/var/adm:/sbin/nologin... (4 Replies)
Discussion started by: Manabhanjan
4 Replies

7. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: FrancoisCN
4 Replies

8. 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

9. Shell Programming and Scripting

How search,edit and save the file

Hi All, I want to edit a file using shell script..For ex...a file called /etc/passwd..here I am searching for "ftp" if it is there just change it to "tftp" without using any temporary file. (3 Replies)
Discussion started by: Vichu
3 Replies

10. Shell Programming and Scripting

script to search and edit scripts

Hi all, can you please help me in this one.. i have a many scripts in a directory & i get many requests to change the code of a particular script. for example file abc.txt contains #!/bin/bash mumbai 102403445 chennai 123980123 delhi 3456268468 kolkata 465376832 #kolkat 462945959 ... (3 Replies)
Discussion started by: geeko
3 Replies

Featured Tech Videos