Search Pattern and add column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search Pattern and add column
# 1  
Old 01-07-2010
Search Pattern and add column

Hi,
I have two files. file1 contents:
Code:
aaa bbb ccc ddd
eee fff  ggg  ddd
www eee ggg dde
qqq zzz hhh ddd

file2 contents:
Code:
mmm
mmm
mmm
mmm

Now I want to add file2 contents to end of lines in file1 where a line contains pattern "ddd" and it should look like this:

file3 contents:
Code:
aaa bbb ccc ddd mmm
eee fff  ggg  ddd mmm
www eee ggg dde
qqq zzz hhh ddd mmm

Thanks,

Last edited by Yogesh Sawant; 02-26-2010 at 12:53 PM.. Reason: added code tags
# 2  
Old 01-07-2010
Try...
Code:
awk '{getline x < f} $NF=="ddd"{$0=$0 FS x} {print}' f=file2 file1 > file3

# 3  
Old 02-26-2010
Bug Search Pattern and add column

Dear Friend,

You can use the following code using perl.

File 1 content
aaa bbb ccc ddd
eee fff ggg ddd
www eee ggg dde
qqq zzz hhh ddd

File 2 content
mmm
mmm
mmmm
mmmm

Code:
use strict;
use warnings;

open(FH,"<file1") or die "Can't open";
open(FH1,"<file2") or die "Can't open";
open(FH2,">file3") or die "Can't open";
my($var,$var1);
while($var=<FH>)
{
    $var1=<FH1>;
    chomp($var);
    if($var=~/ddd$/)
    {
      $var=$var." $var1";
      print FH2 $var;
    }
    else
    {
    print FH2 $var."\n";
    }
}

file 3 output
aaa bbb ccc ddd mmmm
eee fff ggg ddd mmmm
www eee ggg dde
qqq zzz hhh ddd mmmm

Last edited by murugaperumal; 03-09-2010 at 12:48 AM..
# 4  
Old 02-26-2010
Bug

Hi,

Following bash script for matched the ddd in the file1 and get the file contents from the file2.After matching the ddd in the file ,it will append the mmm at the end of the line.

file1
------
aaa bbb ccc ddd
eee fff ggg ddd
www eee ggg dde
qqq zzz hhh ddd

file2
------
mmm
mmm
mmm
mmm

Code:
#!/bin/sh
counter=0;
lineno=1;
> file3
while read line; do
        if `echo ${line} | grep "ddd$" 1>/dev/null 2>&1`
        then
                value=`sed -n "${lineno}p" file2`
                oval=` echo $line | sed -r "s/(.*)/\1 ${value}/g" `
                echo $oval >> file3
                lineno=`expr $lineno + 1`
        else
                echo $line >> file3
        fi
done < file1

file3:
-------
aaa bbb ccc ddd mmm
eee fff ggg ddd mmm
www eee ggg dde
qqq zzz hhh ddd mmm

Last edited by rekha_sri; 02-26-2010 at 07:09 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. 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

3. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

4. Shell Programming and Scripting

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} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

5. Shell Programming and Scripting

awk search pattern in column

Want to search a pattern in column using the below command which not helpful awk -F"\|" '$1 == '"${VAR}"' {print $1,$2}' file how to search using "==" with variable other than the below case. awk -F"\|" '$1 ~ /'"${VAR}"'/ {print $1,$2}' file (14 Replies)
Discussion started by: Roozo
14 Replies

6. Shell Programming and Scripting

[awk] add column between range pattern

Hi all, I have table like this A1 1 2 1 3 2 4 A2 3 1 1 2 1 1 A3 1 1 2 0 3 2 ..... An And i want to add column to my table and the table will become like this : (3 Replies)
Discussion started by: psychop13
3 Replies

7. UNIX for Dummies Questions & Answers

Search and add the column in the file

Hi All, I have the Overview.csv file like below format Message ID Sendout Group Name Email Subject Name Type Rcpts Responses Response Rate Open Rate Click Rate 2000009723 01-22-2014 16:14 Test_GroupPQA2013 000123@yahoo.com INFO RISQUE D'INONDATION... (3 Replies)
Discussion started by: armsaran
3 Replies

8. Shell Programming and Scripting

Search a pattern and add new line below

Hi, I have 2 files like below. File A: apple mango File B: start abc def apple ghi end start cba fed (4 Replies)
Discussion started by: jayadanabalan
4 Replies

9. Shell Programming and Scripting

How to search pattern and add that pattern in next line

Hi All, I am new to shell scripting and need help in scripting using CSH. Here is what I am trying to so, 1. Search a specific string e.g. "task" from "task (input1, out1)". 2. Extract the arguements "input1" and "out1" 3. Add them in separate lines below. eg. "int input1" , " integer out1" ... (7 Replies)
Discussion started by: deshiashish
7 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question