A problem with "grep" on a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A problem with "grep" on a script
# 1  
Old 08-28-2012
A problem with "grep" on a script

Hello every one,

I have two files : file1 and file2

- file1 contains one column and multiple lines.
- file2 contains multiple lines and multiple columns.

I want to do a script that compare the first column of file2 with the only column of file1 then took the lines that are the same and search for each line the whole line on file2 then write the result on a file that we can call RESULT.

Here is my script :

Code:
#!/bin/bash
more file2|awk '{print $1}'|while read line
do
     grep $line file1
done|while read ligne
do
     grep '^$ligne' file2
done

But I still do not have the result that I want.

Regards,


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-28-2012 at 06:12 AM.. Reason: code tags
# 2  
Old 08-28-2012
Please post additionally an example of the input files and the expected output, thanks. Use code tags when doing so, thanks.
# 3  
Old 08-28-2012
The line:
Code:
grep '^$ligne' file2

search the pattern $ligne and I don't think it's what you want. So it's should be
Code:
grep "^$ligne" file2

You can do
Code:
sort file1 > sorted_file1
sort file2 > sorted_file2
join sorted_file2 sorted_file1

If first column of each file are already sorted it's not necessary to sort files.

Last edited by delugeag; 08-28-2012 at 06:29 AM..
# 4  
Old 08-28-2012
Please come up with sample data for the input and required output
# 5  
Old 08-28-2012
Sorry I will use Code Tags on my futur posts.
Example of file1: - unix
- Solaris
- HPUX
- AIX
Example of file2: - Threads java j2ee
- Solaris sparc OS
- AIX OS IBM

the result should be: - Solaris sparc OS
- AIX OS IBM

Thank you delugeag. When I use the "..." the script works.

Code:
#!/bin/bash
more file2|awk '{print $1}'|while read line
do
grep "$line" file1
done|while read ligne
do
grep "^$ligne" file2
done>RESULTAT

Can you tell me what the difference between "..." and '...' ?

How can I do to send the file RESULT to a specific mail ? for example resultat@resultat.com ?

Regards,
# 6  
Old 08-28-2012
Quote:
Originally Posted by adilyos
Can you tell me what the difference between "..." and '...' ?
man bash
Code:
       Enclosing  characters  in  single quotes preserves the literal value of
       each character within the quotes.  A single quote may not occur between
       single quotes, even when preceded by a backslash.

       Enclosing  characters  in  double quotes preserves the literal value of
       all characters within the quotes, with the exception of $, `,  \,  and,
       when  history  expansion  is enabled, !.  The characters $ and ` retain
       their special meaning within double quotes.

Quote:
How can I do to send the file RESULT to a specific mail ? for example resultat@resultat.com ?
Code:
mailx -s "Hello world" you@youremailid.com < RESULTAT

# 7  
Old 08-29-2012
Code:
awk 'NR==FNR{a[$1]++;next}(a[$1]){print}' file1 file2 > result.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

4. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

5. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

6. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

7. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

8. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies
Login or Register to Ask a Question