Sponsored Content
Top Forums Shell Programming and Scripting Extract columns into seperate file Post 303000208 by Ads89 on Thursday 6th of July 2017 09:30:58 AM
Old 07-06-2017
Extract columns into seperate file

I have a comma delimited file as per the one below and I am currently extracting the values in 2 columns (COL1 & COL6) to produce a smaller trimmed down version of the file which only contains the columns we need;

Code:
COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9
1111,AAAA,AAAA,AAAA,AAAA,X100,AAAA,AAAA,XXXX
2222,AAAA,AAAA,AAAA,AAAA,X200,AAAA,AAAA,
3333,AAAA,AAAA,AAAA,AAAA,X300,AAAA,AAAA,XXXX
4444,AAAA,AAAA,AAAA,AAAA,X400,AAAA,AAAA,XXXX
5555,AAAA,AAAA,AAAA,AAAA,X500,AAAA,AAAA,

I now have an additional requirement to only extract the values of COL1 & COL6 when COL9 has value present(could be anything) i.e. lines 1,3,4
The output produced would therefore look something like;

Code:
COL1,COL2
1111,X100
3333,X300
4444,X400

I have the below code which extracts only COL1 & COL2, but need to additional functionality


Code:
 
 awk -F, 'BEGIN {OFS=","} {gsub(/^[ \t]+/, "", $1); gsub(/[ \t]+$/, "", $1); gsub(/^[ \t]+/, "", $6); gsub(/[ \t]+$/, "", $6)} {if (NR>1) {print $1,$6}}' input.csv > output.csv

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to extract columns from a text file

Hi, In ksh, I have a file with similar rows as follows: Department = 1234 G/L Asset Acct No = 12.0000. 2/29/2008 Department = 1234 G/L Asset Acct No = 13.0000. 3/29/2008. I want to create a new text file that contains only the numbers and date: 1234 12.0000. 2/29/2008 1234 13.0000. ... (16 Replies)
Discussion started by: ihot
16 Replies

2. Shell Programming and Scripting

Help, need to extract columns from file

I have huge fixed width, text file in unix box and I need to extract columns found between the width 105 and 200 and output it to a new file. Can anyone tell me how to extract it? Thanks for your help. (1 Reply)
Discussion started by: kiran2k
1 Replies

3. UNIX for Dummies Questions & Answers

extract only the "numbers" that are present in this file to a seperate file..

It may sounds too easy but appreciate any help on this.. i have a file with the below details: ****************************************** Please find the locked pernr details for the Time evaluation Personnel number could not be locked Person rejected: 04552737 Personnel number could not... (4 Replies)
Discussion started by: rohit.shetty84
4 Replies

4. Shell Programming and Scripting

extract columns from 2 different files and create new file

Hi All, I have 2 issues while working with file. 1. I have 2 delimited(~) files. I want to extract column numbner 3 from file1 and column number 8 from file2 and paste it into file3. I have tried using cut, but not able to get answer. 2. i have 2 filxed-width file. I wanted to do same... (1 Reply)
Discussion started by: Amit.Sagpariya
1 Replies

5. Shell Programming and Scripting

Extract Columns from file

Hi All, Could you please help me with following: I have to parse a .csv file. For example: If the csv file contains 3 columns, then i have to print the column names. The field separator is (comma). example.csv (contains 2 lines as follows) This is,a test file, for validation... (2 Replies)
Discussion started by: vfrg
2 Replies

6. Shell Programming and Scripting

fileutility to extract columns from source file

Hi experts,Please help me for the below requirement.i have a source file.(lets say contains 50 columns). I am extarcting five columns from the source file by using pattern file.for exampleinput file:--------a,b,c,d,"a,g","v b",s,koutputfile=======a,"a,g","v b",s,kThanks in advancesubhendu (1 Reply)
Discussion started by: subhendu81
1 Replies

7. Shell Programming and Scripting

extract columns from file and send mail

Hi I have a file of the form name1,lastname1,email1@gmail.com,9.08243E+12,team1,role1,username1,password1 name2,lastname2,email2@gmail.com,9.08243E+11,team2,role2,username2,password2 I need to extract the email (column 3) and send a mail to each person, with their details ( specifically... (3 Replies)
Discussion started by: pkabali
3 Replies

8. UNIX for Dummies Questions & Answers

Seperate columns according to delimiters

Hi all I need your help to separate colomns based on "-" delimiter for a very big file 30 millions rows I have a colmun looking like this : clomun 1 1-100000989-A_ATC 1-10000179-AAAAA 1-100002154-TGTTA 1-100002155-GTTAG 1-100002443 1-100002490 1-100002619 I need to separte in three... (5 Replies)
Discussion started by: biopsy
5 Replies

9. Shell Programming and Scripting

Command to extract all columns except the last few from a txt file

hello, i have publicly available txt file with little less than 300000 rows. i want to extract from column 1 to column 218 and save it in another text file. i use the cut command but the file is saved with multiple rows from the source file onto a single row in the destination. basically it is... (6 Replies)
Discussion started by: madrazzii
6 Replies

10. Shell Programming and Scripting

Match Columns in one file and extract columns from another file

Kindly help merging information from two files with the following data structure. I want to match for the CHR-SNP in Foo and get the columns that match from CHROM-rsID Fields 1 & 2 of foo may have duplicates, however, a joint key of Fields $1$2$3$4 is unique. Also would be helpful to clean up... (4 Replies)
Discussion started by: genehunter
4 Replies
GETIPNODEBYNAME(3)					   BSD Library Functions Manual 					GETIPNODEBYNAME(3)

NAME
getipnodebyname, getipnodebyaddr, freehostent -- nodename-to-address and address-to-nodename translation LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> struct hostent * getipnodebyname(const char *name, int af, int flags, int *error_num); struct hostent * getipnodebyaddr(const void *src, size_t len, int af, int *error_num); void freehostent(struct hostent *ptr); DESCRIPTION
The getipnodebyname() and getipnodebyaddr() functions are very similar to gethostbyname(3), gethostbyname2(3) and gethostbyaddr(3). The functions cover all the functionalities provided by the older ones, and provide better interface to programmers. The functions require addi- tional arguments, af, and flags, for specifying address family and operation mode. The additional arguments allow programmer to get address for a nodename, for specific address family (such as AF_INET or AF_INET6). The functions also require an additional pointer argument, error_num to return the appropriate error code, to support thread safe error code returns. The type and usage of the return value, struct hostent is described in gethostbyname(3). For getipnodebyname(), the name argument can be either a node name or a numeric address string (i.e., a dotted-decimal IPv4 address or an IPv6 hex address). The af argument specifies the address family, either AF_INET or AF_INET6. The flags argument specifies the types of addresses that are searched for, and the types of addresses that are returned. We note that a special flags value of AI_DEFAULT (defined below) should handle most applications. That is, porting simple applications to use IPv6 replaces the call hptr = gethostbyname(name); with hptr = getipnodebyname(name, AF_INET6, AI_DEFAULT, &error_num); Applications desiring finer control over the types of addresses searched for and returned, can specify other combinations of the flags argu- ment. A flags of 0 implies a strict interpretation of the af argument: o If flags is 0 and af is AF_INET, then the caller wants only IPv4 addresses. A query is made for A records. If successful, the IPv4 addresses are returned and the h_length member of the hostent structure will be 4, else the function returns a NULL pointer. o If flags is 0 and if af is AF_INET6, then the caller wants only IPv6 addresses. A query is made for AAAA records. If successful, the IPv6 addresses are returned and the h_length member of the hostent structure will be 16, else the function returns a NULL pointer. Other constants can be logically-ORed into the flags argument, to modify the behavior of the function. o If the AI_V4MAPPED flag is specified along with an af of AF_INET6, then the caller will accept IPv4-mapped IPv6 addresses. That is, if no AAAA records are found then a query is made for A records and any found are returned as IPv4-mapped IPv6 addresses (h_length will be 16). The AI_V4MAPPED flag is ignored unless af equals AF_INET6. o The AI_V4MAPPED_CFG flag is exact same as the AI_V4MAPPED flag only if the kernel supports IPv4-mapped IPv6 address. o If the AI_ALL flag is used in conjunction with the AI_V4MAPPED flag, and only used with the IPv6 address family. When AI_ALL is logi- cally or'd with AI_V4MAPPED flag then the caller wants all addresses: IPv6 and IPv4-mapped IPv6. A query is first made for AAAA records and if successful, the IPv6 addresses are returned. Another query is then made for A records and any found are returned as IPv4-mapped IPv6 addresses. h_length will be 16. Only if both queries fail does the function return a NULL pointer. This flag is ignored unless af equals AF_INET6. If both AI_ALL and AI_V4MAPPED are specified, AI_ALL takes precedence. o The AI_ADDRCONFIG flag specifies that a query for AAAA records should occur only if the node has at least one IPv6 source address config- ured and a query for A records should occur only if the node has at least one IPv4 source address configured. For example, if the node has no IPv6 source addresses configured, and af equals AF_INET6, and the node name being looked up has both AAAA and A records, then: (a) if only AI_ADDRCONFIG is specified, the function returns a NULL pointer; (b) if AI_ADDRCONFIG | AI_V4MAPPED is specified, the A records are returned as IPv4-mapped IPv6 addresses; The special flags value of AI_DEFAULT is defined as #define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG) We noted that the getipnodebyname() function must allow the name argument to be either a node name or a literal address string (i.e., a dot- ted-decimal IPv4 address or an IPv6 hex address). This saves applications from having to call inet_pton(3) to handle literal address strings. When the name argument is a literal address string, the flags argument is always ignored. There are four scenarios based on the type of literal address string and the value of the af argument. The two simple cases are when name is a dotted-decimal IPv4 address and af equals AF_INET, or when name is an IPv6 hex address and af equals AF_INET6. The members of the returned hostent structure are: h_name points to a copy of the name argument, h_aliases is a NULL pointer, h_addrtype is a copy of the af argument, h_length is either 4 (for AF_INET) or 16 (for AF_INET6), h_addr_list[0] is a pointer to the 4-byte or 16-byte binary address, and h_addr_list[1] is a NULL pointer. When name is a dotted-decimal IPv4 address and af equals AF_INET6, and AI_V4MAPPED is specified, an IPv4-mapped IPv6 address is returned: h_name points to an IPv6 hex address containing the IPv4-mapped IPv6 address, h_aliases is a NULL pointer, h_addrtype is AF_INET6, h_length is 16, h_addr_list[0] is a pointer to the 16-byte binary address, and h_addr_list[1] is a NULL pointer. It is an error when name is an IPv6 hex address and af equals AF_INET. The function's return value is a NULL pointer and the value pointed to by error_num equals HOST_NOT_FOUND. The getipnodebyaddr() function takes almost the same argument as gethostbyaddr(3), but adds a pointer to return an error number. Addition- ally it takes care of IPv4-mapped IPv6 addresses, and IPv4-compatible IPv6 addresses. The getipnodebyname() and getipnodebyaddr() functions dynamically allocate the structure to be returned to the caller. The freehostent() function reclaims memory region allocated and returned by getipnodebyname() or getipnodebyaddr(). FILES
/etc/hosts /etc/resolv.conf DIAGNOSTICS
The getipnodebyname() and getipnodebyaddr() functions returns NULL on errors. The integer values pointed to by error_num may then be checked to see whether this is a temporary failure or an invalid or unknown host. The meanings of each error code are described in gethostbyname(3). SEE ALSO
getaddrinfo(3), gethostbyaddr(3), gethostbyname(3), getnameinfo(3), hosts(5), nsswitch.conf(5), services(5), hostname(7), named(8) R. Gilligan, S. Thomson, J. Bound, and W. Stevens, Basic Socket Interface Extensions for IPv6, RFC2553, March 1999. STANDARDS
The getipnodebyname() and getipnodebyaddr() functions are documented in ``Basic Socket Interface Extensions for IPv6'' (RFC2553). HISTORY
The implementation first appeared in KAME advanced networking kit. BUGS
The getipnodebyname() and getipnodebyaddr() functions do not handle scoped IPv6 address properly. If you use these functions, your program will not be able to handle scoped IPv6 addresses. For IPv6 address manipulation, getaddrinfo(3) and getnameinfo(3) are recommended. The text was shamelessly copied from RFC2553. BSD
August 6, 2004 BSD
All times are GMT -4. The time now is 04:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy