Search Pattern and Print lines in Single Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search Pattern and Print lines in Single Column
# 1  
Old 12-26-2014
Search Pattern and Print lines in Single Column

Hi Experts

I have small query where I request the into a single file
Suppose:

File1: {Unique entries}
Code:
AA
BB
CC
DD

FileB:
Code:
AA, 123
AA, 234
AA, 2345
CC, 123
CC, 5678
DD,123
BB, 7890

Output Required:
Code:
AA, 123,234,2345
CC,123,5678
DD,123
BB,7890

Regards
Navkanwal

Last edited by Don Cragun; 12-26-2014 at 08:31 PM.. Reason: Add CODE tags.
# 2  
Old 12-26-2014
This seems to be similar to a request you submitted a year and a half ago.

Can you modify the solution you were given for that problem to solve this problem too? (You never did say what script solved that issue.) What have you tried?
# 3  
Old 12-26-2014
Hi Don

Thanks for the information.
The correction worked fine, last time around and I replied as well

Originally Posted by navkanwal View Post
The command is working fine and output was generated in less than 5 minutes.

However, the request is different this time as, I have multiple lines starting with the same alphabets, so it has become tricky as the previous command is not providing the desired output.

Regards
Navkanwal
# 4  
Old 12-27-2014
Hello Navkanwal,

Kindly use code tags for commands/codes you are using in your posts as per forum rules. Following solution may help you in same.
Code:
awk 'FNR==NR{X[$1]=X[$1]?X[$1] OFS $2:$2;next} ($1 in X){Z=$1 OFS X[$1];gsub(/, /,",",Z);print Z}' FS=, OFS=, file2 file1

Output will be as follows.
Code:
AA,123,234,2345
BB,7890
CC,123,5678
DD,123

Hope this helps.

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-27-2014 at 05:56 AM.. Reason: Edited output according to requirement
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 12-27-2014
You didn't quite answer Don Cragun's questions about your attempts to modify/adapt the solution given in that former thread to solve your actual problem.
And, you don't seem to care about how the spaces in your input file(s) are being handled in the output file.
However, try
Code:
awk 'NR==FNR {T[$1]; next} !($1 in T) {next} $1 != A {printf "%s%s", NL, $1; A=$1; NL="\n"} {printf ",%s", $2} END {printf NL}' FS="," file1 file2
AA, 123, 234, 2345
CC, 123, 5678
DD,123
BB, 7890

This User Gave Thanks to RudiC For This Post:
# 6  
Old 12-30-2014
Thanks Experts !!

The command is working fine and as desired.

Regards
Navkanwal
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

7. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

8. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

9. UNIX for Dummies Questions & Answers

Print lines between the search pattern

hi, I have a file say x.txt containing xxx 123 bla bla ... you xxx dfk dbf ... me xxx ... ... keeps on.. i need to search for pattern in the line starting xxx in the file. If pattern matched, I need to fetch all the lines till i find next xxx. (17 Replies)
Discussion started by: prsshini
17 Replies
Login or Register to Ask a Question