Sponsored Content
Top Forums Shell Programming and Scripting How to combine two files with awk? Post 302722175 by xshang on Friday 26th of October 2012 12:01:55 PM
Old 10-26-2012
Quote:
Originally Posted by pamu
If you don't have any problem with spacings..Smilie

Code:
awk 'FNR==NR{if(!a[$1]){a[$1]=$0}else{a[$0]=$0};next}{if(a[$1]){print a[$1],$2,$3;delete a[$1]}else{print $1"\t"$2,$3}}END{for(i in a){if(a[i]){print a[i]}}}' file1 file2

Thank you!Smilie
The space is not a problem to me, but I try to figure out which file the data come from.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk : combine 3 variables into 1

Within one of my awk scripts, I have three variables extracted and calculated on. When done, I simply want to combine the three. The following works, but looks weird. My script reads a field that has text and numbers, knowing the last four comprise MMYY (month and year) # YY are last two... (2 Replies)
Discussion started by: joeyg
2 Replies

2. Shell Programming and Scripting

Combine awk statements

I have an awk statement that works but I am calling awk twice and I know there has to be a way to combine the two statements into one. The purpose is to pull out just the ip address from loopback1. cat config.txt | nawk 'BEGIN {FS="\n"}{RS="!"}{if ( $0 ~ "interface loopback1" ) print$4}' | nawk... (5 Replies)
Discussion started by: numele
5 Replies

3. Shell Programming and Scripting

combine awk and tr -d

Hi Everyone, awk 'BEGIN{print strftime("%c",1272814948)}' | tr -d '\n' how to change tr -d '\n' to be part of the awk? means awk this pchoh time, and awk also remove '\n', instead of using "|" to combine "tr" command. Thanks (2 Replies)
Discussion started by: jimmy_y
2 Replies

4. Shell Programming and Scripting

combine multiple files by column into one files already sorted!

I have multiple files; each file contains a certain data in a column view simply i want to combine all those files into one file in columns example file1: a b c d file 2: 1 2 3 4 file 3: G (4 Replies)
Discussion started by: ahmedamro
4 Replies

5. Shell Programming and Scripting

[awk] combine and convert time from log files

dear all, an awk newbie need your help.... i have log files with this format: mylog1a.log: "08/10/2012","5:05 PM" "Hostname","Device Address","Count" "","10.10.10.18","10234" mylog2a.log: "08/11/2012","5:05 PM" "Hostname","Device Address","Count" "","10.10.10.18","12543" ... (18 Replies)
Discussion started by: makan
18 Replies

6. Shell Programming and Scripting

Combine these two into one liner awk?

ignore the simplicity of the foo file, my actual file is much more hardcore but this should give you the jist of it. need to combine the two awks into one liner. essentially, need to return the value of one particular field in a file that has multiple comma separated fields. thanks guys cat foo... (1 Reply)
Discussion started by: jack.bauer
1 Replies

7. Shell Programming and Scripting

Combine awk commands into one

my code: gawk 'NR>'"${LASTLINENUM}"' && NR<='"${LINEENDNUM}"'' ${LOGFILE} | gawk '{l=$0;} /'"${STRING1}"'/ && /'"${STRING2}"'/ {for (i=NR-'"${BEFOREGLAF}"'; i<=NR+'"${AFTERGLAF}"'; i++) o=i; t++;} END { for(i=1; i<=NR; i++) if (o) print l; print t+=0;}' i would like to combine this into one... (5 Replies)
Discussion started by: SkySmart
5 Replies

8. Shell Programming and Scripting

Combine two awk commands

Hi, Can someone please guide me how to combine the following two awk calls in one? I noticed that it is very often situation for me, and I think that it can be replaced with one awk call. The question is more general, not the exact one. echo "A B C/D" | awk '{print $3}' | awk -F/ '{print... (4 Replies)
Discussion started by: mirusnet
4 Replies

9. UNIX for Beginners Questions & Answers

Combine awk scripts

Hi, Below command is working as expected, but would like to know how to club the two AWK scripts in the command into one echo -e "MMS000101S0203430A|20180412E|\nMMB0001INVESTMENT||107-86193-01-03|\nMMB0001FUND||107-86193-04-01|\nMMC9991 " | awk -F'|' -v OFS=, '/^MMC9991/{print r"|"s,t; next}... (3 Replies)
Discussion started by: JSKOBS
3 Replies

10. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies
getipnodebyname(3)					     Library Functions Manual						getipnodebyname(3)

NAME
getipnodebyname - Get a network host entry by name for a specific address family LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <netdb.h> struct hostent *getipnodebyname( const char *name, int addr_family, int flags, int *error_num); PARAMETERS
Specifies the official network node name, alias, or numeric node address (for example, a IPv4 dotted-decimal address or an IPv6 hexadecimal address). Specifies the address family. This can be AF_INET for IPv4 addresses or AF_INET6 for IPv6 addresses. Specifies the type of addresses for which to search and the types of addresses that are returned. See the "Nodename-to-Address Processing" section. Specifies an error return code value if the function is not successful. See the ERRORS section for a description of the values. DESCRIPTION
The use of this routine is deprecated. Use the getaddrinfo(3) routine instead; it is also protocol-independent. The getipnodebyname() routine is an evolution of the gethostbyname() routine that enables name lookups in address families other than AF_INET. The getipnodebyname() routine returns a pointer to a structure of type hostent. Its members specify data obtained from the local /etc/ipn- odes file, /etc/hosts file, or one of the files distributed by DNS/BIND or NIS. If multiple addresses are found, the h_addr_list field in the hostent structure contains the addresses. To determine which file or files to search, and in which order, the system uses the switches in the /etc/svc.conf file. The netdb.h header file defines the hostent structure. If using DNS/BIND, the information is obtained from a name server specified in the /etc/resolv.conf file. When the name server is not run- ning, the getipnodebyname() routine searches the local ipnodes name file for IPv6 and IPv4 addresses and the hosts name file for IPv4 addresses, if none are found in the ipnodes file. If the name parameter does not contain a dot "." and you are using DNS/BIND, the getipnodebyname() routine checks whether the environment variable HOSTALIASES is set. If set, it first searches the file named by HOSTALIASES for an alias matching the name parameter. If HOSTAL- IASES is not set, the routine uses the default domain. The alias file has the following format: name1 name2 The alias name. This name cannot include dots. The host name used by DNS/BIND to look up the node information. The hosts database must be distributed by DNS/BIND. The getipnodebyname() routine dynamically allocates the hostent structure. Use the freehostent() routine to free the allocated memory. Nodename-to-Address Processing A following is a list of the flags parameters and how the processing is affected by the value of the af parameters: If the af value is AF_INET, the routine searches for A records. If found, returns IPv4 addresses (h_length=4). If no A records are found, returns a NULL pointer. Provides backward compatibility for existing IPv4 applications. If the af value is AF_INET6, searches for AAAA records. If found, returns IPv6 records (h_length=16). If no AAAA records are found, returns a NULL pointer. If the af value is AF_INET, this is ignored. If the af value is AF_INET6, searches for AAAA records. If found, returns IPv6 records (h_length=16). If no AAAA records are found, searches for A records. If found, returns IPv4-mapped IPv6 addresses (h_length=16). If no records are found, returns a NULL pointer. If the af value is AF_INET, this is ignored. If the af value is AF_INET6, searches for AAAA records. If found, returns IPv6 addresses (h_length=16). Then, searches for A records. If found, returns IPv4-mapped IPv6 addresses (h_length=16). If no records are found, returns a NULL pointer. You can use the flags in any combination to achieve finer control of the translation process. The AI_ADDRCONFIG flag is typically used in combination with other flags to modify the search based on the source address or addresses configured on the system. The following list describes how the AI_ADDRCONFIG flags works by itself. If an IPv4 source address is configured, searches for A records. If an IPv6 source address is configured, searches for AAAA records. Most applications will want to use the combination of the AI_ADDRCONFIG and AI_V4MAPPED flags to control their search. To simplify this for the programmer, the AI_DEFAULT symbol, which is a logical OR of AI_ADDRCONFIG and AI_V4MAPPED, is defined. The following list describes how AI_DEFAULT directs the search. If the af value is AF_INET, searches for A records only if an IPv4 source address is configured on the sys- tem. If found, returns IPv4 addresses (h_length=4). If no A records are found, returns a NULL pointer. If the af value is AF_INET6, searches for AAA records only if an IPv6 source address is configured on the system. If found, returns IPv6 addresses (h_length=16). If no AAAA records are found and if an IPv4 address is configured on the system, searches for A records. If found, returns IPv4-mapped IPv6 addresses. If no records are found, returns a NULL pointer. EXAMPLES
For IPv4 applications (backward compatibility), use the following syntax: getipnodebyname(address, AF_INET, 0, &error_num); RETURN VALUES
Upon successful completion, the getipnodebyname() routine returns a pointer to a hostent structure. If it reaches the end of the network hostname database, it returns a null pointer. ERRORS
If the getipnodebyname() routine call fails, error_num is set to one of the following the values: The name you have used is not an official node name or alias; another type of name server request may be successful. The server recognized the request and the name, but no address is available for the name. Another type of name server request may be successful. An unexpected server failure occurred. This is a non- recoverable error. A transient error occurred, for example, the server did not respond. A retry at some later time may be successful. FILES
The Internet network node name database. Each record in the file occupies a single line and has three fields consisting of the node address (IPv6 or IPv4), official node name, and aliases. The Internet network hostname database. Each record in the file occupies a sin- gle line and has three fields consisting of the host IPv4 address, official hostname, and aliases. The resolver configuration file. The database service selection configuration file. RELATED INFORMATION
Functions: endhostent(3), freehostent(3), getaddrinfo(3), getipnodebyaddr(3), sethostent(3). Files: hostname(5), resolv.conf(4), svc.conf(4). Networks: bind_intro(7), nis_intro(7). delim off getipnodebyname(3)
All times are GMT -4. The time now is 07:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy