Sponsored Content
Top Forums Shell Programming and Scripting Find and replace a column that has '' to NULL in a comma delimited using awk or sed Post 302421181 by ygemici on Thursday 13th of May 2010 04:33:20 PM
Old 05-13-2010
MySQL

You can try this Smilie

Code:
#!/bin/bash
#goal is the change value in column 59 if equal ''
oIFS=$IFS
cp mydata mydatatmp
IFS=' , '
while read char
     do
       changecharacter=$( (echo "$char" | tr -d , | awk {'print $59'}) )

        if [ $changecharacter = "''" ]
            then
                changelineport=$( (cut -d , -f1-59 mydata) )
                newlineport=$( (cut -d , -f1-58 mydata) )
                sed -i "s/$changelineport/$newlineport, NULL /" mydata
        fi
     done < mydatatmp
cat mydata
rm mydatatmp
IFS=$oIFS

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

2. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies

3. Shell Programming and Scripting

Need an awk for a global find/replace in a file, specific column

I am new to unix and awk/sed etc... using C-Shell. Basically, I have a fixed length file that has 4 different record types on it, H, D, V, W all in column 1. I need to change all the W's in column 1 to D's. in the entire file. The W's can be anywhere in the file and must remain in the same... (3 Replies)
Discussion started by: jclanc8
3 Replies

4. Shell Programming and Scripting

Replace last row of a column in bash/awk/sed

Hi, I've got a file with 3 columns which ends like this: ... 1234 345 1400 5287 733 1400 8472 874 1400 9317 726 1400 I want to replace the last row of the last column with the value 0. So my new file will end: ... 1234 345 1400 5287 733 1400 8472 874 1400 9317 726 ... (5 Replies)
Discussion started by: jhunter87
5 Replies

5. Shell Programming and Scripting

Find in first column and replace the line with Awk, and output new file

Find in first column and replace the line with Awk, and output new file File1.txt"2011-11-02","Georgia","Atlanta","x","","" "2011-11-03","California","Los Angeles","x","","" "2011-11-04","Georgia","Atlanta","x","x","x" "2011-11-05","Georgia","Atlanta","x","x","" ... (4 Replies)
Discussion started by: charles33
4 Replies

6. Shell Programming and Scripting

Count number of column in a comma delimited file

I have a comma (,) delimited file. 106232145,"medicare","medicare,medicaid",789 I would like to count the number of fields in each line. I tried the below code awk -F ',' '{print NF-1}' This returns me the result as 5 instead of 4. This is because the awk takes... (9 Replies)
Discussion started by: machomaddy
9 Replies

7. Shell Programming and Scripting

Find for line with not null values at nth place in pipe delimited file

Hi, I am trying to find the lines in a pipe delimited file where 11th column has not null values. Any help is appreciated. Need help asap please. thanks in advance. (3 Replies)
Discussion started by: manikms
3 Replies

8. Shell Programming and Scripting

Replace pipe delimited column string to null

Hi All, I have a large dat file where each lines are pipe delimited values. I need to parse the file depending on the request. For example: sometimes I have told to remove all the values in the 7th column (this case remove values '3333' only from the first line and '3543' from the second line)... (4 Replies)
Discussion started by: express14
4 Replies

9. UNIX for Dummies Questions & Answers

Check for not null column in a pipe delimited file

Hi, I have a requirement where I have to check whether the mandatory columns in a pipe delimited file is null and print error message. For eg, I have to check if the 3rd,5th,6th,7th and 8th column are null and print the message "<column name> is null". The data file will have aroung 100,000... (6 Replies)
Discussion started by: reshma15193
6 Replies

10. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies
ARCHIVE_READ(3) 					   BSD Library Functions Manual 					   ARCHIVE_READ(3)

NAME
archive_read -- functions for reading streaming archives LIBRARY
Streaming Archive Library (libarchive, -larchive) SYNOPSIS
#include <archive.h> DESCRIPTION
These functions provide a complete API for reading streaming archives. The general process is to first create the struct archive object, set options, initialize the reader, iterate over the archive headers and associated data, then close the archive and release all resources. Create archive object See archive_read_new(3). To read an archive, you must first obtain an initialized struct archive object from archive_read_new(). Enable filters and formats See archive_read_filter(3) and archive_read_format(3). You can then modify this object for the desired operations with the various archive_read_set_XXX() and archive_read_support_XXX() functions. In particular, you will need to invoke appropriate archive_read_support_XXX() functions to enable the corresponding compression and format support. Note that these latter functions perform two distinct operations: they cause the corresponding support code to be linked into your program, and they enable the corresponding auto-detect code. Unless you have specific constraints, you will generally want to invoke archive_read_support_filter_all() and archive_read_support_format_all() to enable auto-detect for all formats and compression types currently supported by the library. Set options See archive_read_set_options(3). Open archive See archive_read_open(3). Once you have prepared the struct archive object, you call archive_read_open() to actually open the archive and prepare it for reading. There are several variants of this function; the most basic expects you to provide pointers to several functions that can provide blocks of bytes from the archive. There are convenience forms that allow you to specify a filename, file descriptor, FILE * object, or a block of mem- ory from which to read the archive data. Note that the core library makes no assumptions about the size of the blocks read; callback func- tions are free to read whatever block size is most appropriate for the medium. Consume archive See archive_read_header(3), archive_read_data(3) and archive_read_extract(3). Each archive entry consists of a header followed by a certain amount of data. You can obtain the next header with archive_read_next_header(), which returns a pointer to an struct archive_entry structure with information about the current archive element. If the entry is a regular file, then the header will be followed by the file data. You can use archive_read_data() (which works much like the read(2) system call) to read this data from the archive, or archive_read_data_block() which provides a slightly more efficient interface. You may prefer to use the higher-level archive_read_data_skip(), which reads and discards the data for this entry, archive_read_data_to_file(), which copies the data to the provided file descriptor, or archive_read_extract(), which recreates the specified entry on disk and copies data from the archive. In particular, note that archive_read_extract() uses the struct archive_entry structure that you provide it, which may differ from the entry just read from the archive. In particular, many applications will want to override the path- name, file permissions, or ownership. Release resources See archive_read_free(3). Once you have finished reading data from the archive, you should call archive_read_close() to close the archive, then call archive_read_free() to release all resources, including all memory allocated by the library. EXAMPLE
The following illustrates basic usage of the library. In this example, the callback functions are simply wrappers around the standard open(2), read(2), and close(2) system calls. void list_archive(const char *name) { struct mydata *mydata; struct archive *a; struct archive_entry *entry; mydata = malloc(sizeof(struct mydata)); a = archive_read_new(); mydata->name = name; archive_read_support_filter_all(a); archive_read_support_format_all(a); archive_read_open(a, mydata, myopen, myread, myclose); while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { printf("%s ",archive_entry_pathname(entry)); archive_read_data_skip(a); } archive_read_free(a); free(mydata); } ssize_t myread(struct archive *a, void *client_data, const void **buff) { struct mydata *mydata = client_data; *buff = mydata->buff; return (read(mydata->fd, mydata->buff, 10240)); } int myopen(struct archive *a, void *client_data) { struct mydata *mydata = client_data; mydata->fd = open(mydata->name, O_RDONLY); return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL); } int myclose(struct archive *a, void *client_data) { struct mydata *mydata = client_data; if (mydata->fd > 0) close(mydata->fd); return (ARCHIVE_OK); } SEE ALSO
tar(1), libarchive(3), archive_read_new(3), archive_read_data(3), archive_read_extract(3), archive_read_filter(3), archive_read_format(3), archive_read_header(3), archive_read_open(3), archive_read_set_options(3), archive_util(3), tar(5) HISTORY
The libarchive library first appeared in FreeBSD 5.3. AUTHORS
The libarchive library was written by Tim Kientzle <kientzle@acm.org>. BUGS
Many traditional archiver programs treat empty files as valid empty archives. For example, many implementations of tar(1) allow you to append entries to an empty file. Of course, it is impossible to determine the format of an empty file by inspecting the contents, so this library treats empty files as having a special ``empty'' format. BSD
February 2, 2012 BSD
All times are GMT -4. The time now is 03:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy