Using grep but printing a line more than once


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using grep but printing a line more than once
# 1  
Old 01-22-2013
Using grep but printing a line more than once

Hi,

I am using the grep function to grep some lines from a text file. However, a line is grepped only once even if it matches the strings in the first file more than once. How can i change the code so that the line gets printed more than once?

Input file 1:
Code:
1 o p
1 a b
2 3 c
5 6 7

Input file 2:
Code:
o a c

Desired Output:
Code:
1 o p 
1 a b
2 3 c

Thanks!
# 2  
Old 01-22-2013
This way:

Code:
$ grep -E "$(sed 's/ /|/g;' file2)" file1
1 o p
1 a b
2 3 c

Guru.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Grep -w not printing exact matches

Dear All, Here is my input TAACGCACTTGCGGCCCCGGGATAAAAAAAAAAAAAAAAAAAAATGGATT NAGAGGGACGGCCGGGGGCATAAAAAAAAAAAAAAAAAAAAAGGGATTTC NGGGTTTTAAGCAGGAGGTGTCAAAAAAAAAAAAAAAAAAAAAGGGATTT NTGGAACCTGGCGCTAGACCAAAAAAAAAAAAAAAAAAAATGGATTTTTG ATACTTACCTGGCAGGGGAGATACCATGATCAATAAAAAAAAAAAAAAAA... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

4. Shell Programming and Scripting

Echo printing a line in 2 lines; expected to print in one line

Dear All, fileName: therm.txt nc3h7o2h 7/27/98 thermc 3h 8o 2 0g 300.000 5000.000 1390.000 41 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14 2 -2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05 3 ... (7 Replies)
Discussion started by: linuxUser_
7 Replies

5. UNIX for Dummies Questions & Answers

Printing the next line side by to the current line

Help, I have a text file which looks like disco 5674536 3456 jambo disco 453678 4578 jambo I would like to have an output which looks like below disco 3456 disco 4578 (4 Replies)
Discussion started by: Indra2011
4 Replies

6. Shell Programming and Scripting

Need Help with grep printing multiple lines.

I need help in printing multiple lines using a grep command. The situation is like this. I have a file that contains large number of lines Now I need to find the the lines in the file such that if the word "AllServiceType" is found then the next line also gets printed. Does anyone... (6 Replies)
Discussion started by: m_usmanayub
6 Replies

7. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

8. Programming

Line printing.

#include <iostream> int main() { int x=0; int y=1000; while(x<1000) { x++, y--; system("clear"); printf("\n%d\n%d", x,y); fflush(stdout); } return 0; } Without clearing the screen every time, how can i print 'x' and 'y' in the same spot? (2 Replies)
Discussion started by: cbreiny
2 Replies

9. Shell Programming and Scripting

Help required on grep command(Skip the first few lines from printing in the output)

Hi experts I want the proper argument to the grep command so that I need to skip the first few lines(say first 10 lines) and print all the remaining instances of the grep output. I tried to use grep -m 10 "search text" file*. But this gives the first 10 instances(lines) of the search string.... (7 Replies)
Discussion started by: ks_reddy
7 Replies

10. Shell Programming and Scripting

Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends, Only very recently i started learning sed command...an i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming. The below script... (4 Replies)
Discussion started by: frozensmilz
4 Replies
Login or Register to Ask a Question