Grep command to find string in Variable in file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep command to find string in Variable in file2
# 1  
Old 09-23-2014
Scissors Grep command to find string in Variable in file2

Hi ,
I am executing 2 queries and output is saved in file1.txt and file2.txt
example of file1.txt
Code:
Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,,
Testing Excel _.xlsx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,,
example of file2.txt
Testing Excel _.xlsx,,,,,b,20653
Testing word Doc.docx,,,,,a,20653

Expected output:file3.txt
Code:
Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,,,,,,,a,20653
Testing Excel _.xlsx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,,,,,,,b,20653


Now I want to merge file1.txt and file2.txt into file3.txt using following code with the help of name highlighted above which is common in both files and appears in the begining of line.

Code:
 
while read line
do
file_met_name=`echo $line | cut -d ',' -f1`
met_line_2 =` grep "$file_met_name,",file2.txt | cut -d ',' -f2-`
echo "$line","$met_line_2" >> file3.txt
done < file1.txt

Problem faced: If $file_met_name has string with multiple spaces as in "Testing word Doc.docx" the above code is not able to provide required output.

Last edited by Corona688; 09-23-2014 at 04:13 PM..
# 2  
Old 09-23-2014
If order doesn't matter and you can sort file1.txt and file2.txt by their first field....

Code:
sort -t, -k 1,1 -o file1.txt file1.txt
sort -t, -k 1,1 -o file2.txt file2.txt

So.. if the files are sorted then you can use join to do the "merge"

Code:
join -t, -j 1 file1.txt file2.txt >file3.txt

Not sure if this is exactly what you wanted, but might be...
# 3  
Old 09-23-2014
Instead of giving us a program that doesn't give the output you want, tell us the output you do want.
# 4  
Old 09-24-2014
With few adjustments your code seems to work
Code:
while read line
do
file_met_name=`echo "$line" | cut -d ',' -f1`
met_line_2=`grep "^$file_met_name," file2.txt | cut -d ',' -f2-`
echo "$line","$met_line_2"
done < file1.txt > file3.txt

The following splits on IFS for reading so you save one cut:
Code:
while IFS="," read file_met_name other
do
met_line_2=`grep "^$file_met_name," file2.txt | cut -d ',' -f2-`
echo "$file_met_name,$other,$met_line_2"
done < file1.txt > file3.txt

With awk it becomes much faster but needs more memory:
Code:
awk -F, 'NR==FNR {s[$1]=substr($0,index($0,FS)); next} {print $0 s[$1]}' file2.txt file1.txt > file3.txt


Last edited by MadeInGermany; 09-24-2014 at 04:54 PM.. Reason: awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep a sub-string from a string stored in a variable.

For example: I am grepping "Hello" from a file and there are 10 matches. So all ten lines with match will get stored into a variable($match). Now I want to ignore those lines which have "Hi" present in that. Currently I tried this: match = grep "Hello" file | grep -v "Hi" file But that's not... (2 Replies)
Discussion started by: pavan
2 Replies

2. Shell Programming and Scripting

String variable as part of expression in find command

Hi, I am new in scripting, and I am currently working on a script that will look for other files in a certain directory and exclude some file type. this works fine:Find_File2Exclude=`find ${paths} -maxdepth 1 -type f \( ! -iname '*.out' ! -iname '*.auc' ! -iname '*.cps' ! -iname '*.log' ! -iname... (4 Replies)
Discussion started by: kedd05
4 Replies

3. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

4. Shell Programming and Scripting

bash: using a string variable in grep

Hi, I've been stuck for several days on this. Using grep on a command line, I can use quotes, eg... grep 'pattern of several words' filename I want to do this in my bash script. In my script I have captured the several command line arguments (eg arg1 arg2) into a variable: variable=$@ I... (2 Replies)
Discussion started by: adrian777uk
2 Replies

5. Shell Programming and Scripting

how to find string from file1 in file2

hi; i am looking for simple search script that find string from file1 in file 2 file 1 contain a loot of string like: 204080111111111 204080222222222 204080333333333 in each row and i would like to take the first row for example 204080111111111 from file1 and find it in file2 when it... (1 Reply)
Discussion started by: kpinto
1 Replies

6. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

7. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

8. UNIX for Dummies Questions & Answers

grep string and then find another string

I have a file that looks like this: Name=TOM abcded asdfas fkoiaerj inadhah Name=Chris 23nafds vasdkfna afadns afdadsfa aaaaaa bbbbbb cccccc I would to search for the string 'bbbbbb', then I would like to search above that string for the 1st occurrence of "Name" and not the other... (7 Replies)
Discussion started by: doza22
7 Replies

9. UNIX for Dummies Questions & Answers

Using GREP to extract variable following a string

Hello, I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like: 1 F=... (10 Replies)
Discussion started by: modey3
10 Replies

10. AIX

Help Using Grep command to Find the string in the files in the root directory

I want to serch for a string in all the files in the root directory. i want the search to be limited to only the files in the directory i.e the search to be done only in the files not in the sub directory. the approaches tried are 1)grep "pattern string" this command was serching the... (3 Replies)
Discussion started by: Subbu_Angeline
3 Replies
Login or Register to Ask a Question