Find string in one file and manipulate other


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find string in one file and manipulate other
# 1  
Old 07-16-2012
Find string in one file and manipulate other

hi,

I have 2 files delimited by "|"

File 1:
HTML Code:
1|28|ABC|11|9620034||XXX555|
29|22|ABC|11|9620258||XXX555|
51|26|ABC|11|9620314||XXX555|
77|20|ABC|11|9630506||XXX555|
97|36|ABC|11|9630562||XXX555|
File 2:
HTML Code:
9620028|I
9620034|I
9620314|S
9620332|I
9620258|I
9630506|S
9630562|S
9631795|I
9640217|S
9640232|I
Now, For each 1st field in File 2
if found in field 5 of file 1,
place the 2nd field of File 2 into last field of File 1 to look like:

Result:
HTML Code:
1|28|ABC|11|9620034||XXX555|I|
29|22|ABC|11|9620258||XXX555|I|
51|26|ABC|11|9620314||XXX555|I|
77|20|ABC|11|9630506||XXX555|S|
97|36|ABC|11|9630562||XXX555|S|
How can I do this ? Thanks in advance.
# 2  
Old 07-16-2012
Code:
awk -F'|' 'FNR==NR {a[i]=$1; b[i]=$2; i++} {for(x in a){if($5==a[x]){print $0 b[x] "|"}}}' file2 file1

# 3  
Old 07-16-2012
If, like the sample data, each file is sorted on the joining field:
Code:
join -t \| -1 5 -2 1 -o 1.1,1.2,1.3,1.4,1.5,1.6,1.7,2.2,2.3 file1 file2

Regards,
Alister
# 4  
Old 07-16-2012
Thanks very much balajesuri and Alister. Both the codes work perfectly fine. SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to manipulate string in line?

Hello, I looked up on google but do not know from which point to start... I am under ubuntu 18 bionic and Mainfile consists of 25K lines MainFile: Test,AAEE9FED3, GGBBDD DD AA X2d Moscow 112233445566aaBBccPPdddEE Test,AAEE9FED3, GG33DD s00022 Leningrad 11298932566aaBBccPPdddEE... (8 Replies)
Discussion started by: baris35
8 Replies

2. UNIX for Advanced & Expert Users

Manipulate files with find and fuser not working as expected on SunOs

Greetings, For housekeeping, I use the following command: find /some/path -type f -name "*log*" ! -exec fuser -s "{}" 2>/dev/null \; -exec ls -lh {} \; It finds all log files not currently in use by a process and manipulates them. This command always works on linux and redhat machines,... (2 Replies)
Discussion started by: dampio
2 Replies

3. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

4. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

5. Shell Programming and Scripting

Manipulate XML File Continous STRING by each Order Line using SHELL

heres sample File: <?xml version="1.0"?> <!DOCTYPE cXML SYSTEM "www"><cXML.............................................. <OrderRequest>USE UNIX.com</Extrinsic><Extrinsic name="UniqueName">Peter@UNIX.com</Extrinsic><Extrinsic name="ContractingEntity">UNIX... (3 Replies)
Discussion started by: Pete.kriya
3 Replies

6. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

7. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

8. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

9. Shell Programming and Scripting

Manipulate string in shell script

I am writing a shell script for some purpose. I have a variable of the form -- var1 = "policy=set policy" Now I need to manipulate the variable var to get the string after index =. that is i should have "set polcy". Also I need to to this for many other variables where the value of "=" is not... (3 Replies)
Discussion started by: Dev_Sharma987
3 Replies

10. Shell Programming and Scripting

Sed command to find, manipulate and replace a number

Hi, Im very new to the world of sed so I'm really not even sure if this is possible. What i need to do is read from a flat file and every time i see this line: VAL=123,456 I need to change 456 to 457 for every occurence of this line in the file. The numbers 123 and 456 are different for... (6 Replies)
Discussion started by: LT_2008
6 Replies
Login or Register to Ask a Question