How to concatenate grep results?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to concatenate grep results?
# 1  
Old 03-15-2015
How to concatenate grep results?

hi,

let's say we have input in files test1.txt, test2.txt, text3.txt ... ... ... ('...' means more files & lines not just 'dots')
test1.txt has:
Code:
A
B
C
D
...
...
...

test2.txt has
Code:
A
B
C
D
...
...
...
and so on.


i would like the output to be:
Code:
A B C D ... ... ...

i noticed that doing:
Code:
tst1=`grep 'A' *"test"* | grep 'B' *"test"* | grep 'C' *"test"* | grep 'D' *"test"* | ...more greps`
echo "$tst1"

returns the 1st grep but the rest are overwritten by the last grep.

your help will be really appreciated.

Last edited by Scrutinizer; 03-15-2015 at 11:16 AM.. Reason: code tags
# 2  
Old 03-15-2015
In general, when using grep in a pipeline, you need to specify the files only with the first grep, the other greps in the pipeline use stdin as input, not file(s), so:
Code:
tst1=$(grep 'A' *"test"* | grep 'B' | grep 'C' | grep 'D' | ...more greps)

You can try to print out on one line by leaving out the quotes:
Code:
echo $tst1

which depending on the input may be good enough.

---
But this will not produce what you want. In order to do that you need to specify what you want to accomplish? Do you want to print what these files have in common? It is not clear what the criteria are or what you are trying to accomplish..
---

Last edited by Scrutinizer; 03-15-2015 at 11:24 AM..
# 3  
Old 03-15-2015
thanks Scrutinizer. you are correct, it doesn't return anything.

what if i want to grep only 4 lines and put them all in one line? is it possible?
# 4  
Old 03-15-2015
You mean something like this?
Code:
echo $(grep -E 'A|B|C|D' *"test"*)


---
On Solaris use /usr/xpg4/bin/grep
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-15-2015
You may want to check man grep if it has
Quote:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cannot get results from grep command

Hi, i have a file hello.log which as several line that look like the below 2015-12-07 09:46:56 0:339 120.111.12.12 POST /helloWorld 2015-12-07 09:46:57 0:439 122.111.12.12 POST /helloWorld .... when i grep expecting to see results like the below. ... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

How to analyse results of grep

Hi all, I'm working with a peice of software that runs on Linux that allows planning trips in cars through maps. This software has different variations depending on the type of car, e.g. BMW, Audi, Hyundai, etc... Each variation has a dependency on common external components that are not... (1 Reply)
Discussion started by: emoshaya
1 Replies

3. UNIX for Dummies Questions & Answers

How to do ls -l on results of grep and find?

Hi, Am running the command below to search for files that contains a certain string. grep -il "shutdown" `find . -type f -mtime -1 -print` | grep "^./scripts/active" How do I get it to do a ls -l on the list of files? I tried doing ls -l `grep -il "shutdown" `find . -type f -mtime -1... (5 Replies)
Discussion started by: newbie_01
5 Replies

4. Shell Programming and Scripting

Grep no results

Hello guys, I have been looking around but can't find the answer to my problem: If the grep command displays no results, print "no results have been found" and increment x. But if the grep command find something, do nothing. if echo "no results have been found $x" x=`expr $x + 1 `... (3 Replies)
Discussion started by: Benou
3 Replies

5. UNIX for Dummies Questions & Answers

Acting on results from a grep command

Hi, I am currently reading a tar file and searching for a particular word using grep e.g. Plane. At the moment, if a sentence is found with the word "Plane" the sentence itself is piped to another file. Here is the code i am using; for jar in 'cat jar_file.tar'; do tar -tvf... (3 Replies)
Discussion started by: crunchie
3 Replies

6. Shell Programming and Scripting

Concatenate Loop Results

Hi, I have the following situation: Param1Values = AAAA,BBBB Param1=$(echo $Param1Values| tr "," "\n") for x in $Param1 do db2 select X from Y where Z IN ('$x') done Obviously the above will perform the select 'x' amount of times. Is there a way in which i can... (13 Replies)
Discussion started by: RichZR
13 Replies

7. UNIX for Dummies Questions & Answers

My ps -ef|grep command results are chopped off

On our one HP-UX 11i box, we have some very long paths defined. When I want to check on our user processes running, the resulting paths are chopped off. /xyz/abc/123/......./server/b is really a process running in the ..../server/bin directory. Is this a terminal problem or buffer length... (1 Reply)
Discussion started by: bsp18974
1 Replies

8. UNIX for Dummies Questions & Answers

How to display first 7 char of grep results?

My file contains the following: uat2000.aspclient.active=true uat2001.aspclient.active=true uat2002.aspclient.active=true uat2003.aspclient.active=true uat2004.aspclient.active=false uat2005.aspclient.active=false uat2006.aspclient.active=false uat2007.aspclient.active=false... (8 Replies)
Discussion started by: kthatch
8 Replies

9. UNIX for Dummies Questions & Answers

List grep results

Hi I need to search for matching strings in a database and I want to print out all files that matches in "detail", which means that I want the output to contain datum of last saving. I only get the grep function tp print the actual file names which is not enough since the database is to large... (14 Replies)
Discussion started by: slire
14 Replies

10. Shell Programming and Scripting

How to refine results of grep -p

I need help to further reduce the output shown below. I want to be able to only return the paragraph where the 'Database alias' is exactly equal to DBIHP. I do not want the other paragraphs being shown below. $ echo $dbalias DBIHP $ db2 list db directory|grep -p 'Database alias ... (2 Replies)
Discussion started by: priceb
2 Replies
Login or Register to Ask a Question