Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Search Pattern and add column Post 302399034 by rekha_sri on Friday 26th of February 2010 05:37:35 AM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
inet_pton(3)						     Library Functions Manual						      inet_pton(3)

NAME
inet_pton - Converts a text string to a numeric address LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <arpa/inet.h> int inet_pton ( int af, const char *src, void *dst) ; PARAMETERS
Specifies the address family. Valid values are AF_INET for an Internet Protocol Version 4 (IPv4) address and AF_INET6 for an IPv6 address. Points to the address text string to be converted. Points to a buffer that is to contain the numeric address. DESCRIPTION
The use of this routine is deprecated. Use the getaddrinfo(3) routine instead; it is also protocol-independent. The inet_pton() function converts a text string to a numeric value in Internet network-byte order. If the af parameter is AF_INET, the function accepts a string in the standard IPv4 dotted-decimal form: ddd.ddd.ddd.ddd In this format, ddd is a one to three digit decimal number between 0 and 255. If the af parameter is AF_INET6, the function accepts a string in the following form: x:x:x:x:x:x:x:x In this format, x is hexadecimal value of a 16-bit piece of the address. IPv6 addresses can contain long strings of zero (0) bits. To make it easier to write these addresses, you can use double colon characters (::) one time in an address to represent 1 or more 16-bit groups of zeros. For mixed IPv4 and IPv6 environments, the following form is also accepted: x:x:x:x:x:x:ddd.ddd.ddd.ddd In this form, x is hexadecimal value of a 16-bit piece of the address and ddd is a one to three digit decimal value between 0 and 255 that represents the IPv4 address. See RFC 2373 for more information on IPv6 addressing formats. The calling application is responsible for ensuring that the buffer referred to by the dst parameter is large enough to hold the numeric address. AF_INET addresses require 4 bytes and AF_INET6 addresses require 16 bytes. RETURN VALUES
Upon successful completion, the inet_pton() function returns 1. If the input string is not a valid IPv4 dotted-decimal string or a valid IPv6 address string, the function returns 0. If any other error occurs, the function returns -1. ERRORS
If the inet_pton() routine call fails, errno is set to one of the following values: The address family specified in the af parameter is unknown. RELATED INFORMATION
Functions: getaddrinfo(3), inet_ntop(3). RFC 2373, IP Version 6 Addressing Architecture delim off inet_pton(3)
All times are GMT -4. The time now is 09:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy