Sponsored Content
Top Forums Shell Programming and Scripting CSv2dat file headers and columns order Post 302898447 by grikoss on Tuesday 22nd of April 2014 08:58:32 AM
Old 04-22-2014
CSv2dat file headers and columns order

Dear all,

I have a csv file which is transformed to .dat. I have an awk file which is supposing to do the mapping of the dat file. the code from the awk file is the one below.

The content of the dat file is looking like this (tab separated):
Code:
ODT   AGE        CDT    CO    SEX    TIME    VALUE    COMMENT
P3      Y6-8         ACT    FG    F         2011       1297        
P4      Y3-4         EMP    FG    M        2011       6940           b
P1      Y7-9         GRT    FG    F          2011       0                 c

what I have to do is the following:
1. keep the headers together with the columns order - done
2. if the value for COMMENT is "c" then the column COMMENT must be changed to STRING_COM, if the value for COMMENT is "b" (or anything different than "c") the COMMENT column must be changed to STRING_STATUS
3. No matter what other columns will be received except the ones mentioned here they must be removed
4. VALUE column should be renamed in "NUMB"

Here is my code which fix only the 1st point
Code:
BEGIN { 
  FS = "," ;
  OFS = "    " ;  
}

{ 

if(NR == 1)
{
  split($0, tmp, ",");
  for(i = 1; i <= NF; i++) 
  fields[tmp[i]] = i
  print tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], tmp[8]

else
{
  split($0, tmp, ",");
  for(i = 1; i <= NF; i++) 
  fields[tmp[i]] = i

  print tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], tmp[8]
}
}

expected result in the dat file:
Code:
ODT   AGE        CDT    CO    SEX    TIME    NUMB STRING_COM STRING_STATUS
P3      Y6-8         ACT    FG    F         2011       1297        
P4      Y3-4         ERT    FG    M        2011       6940           b
P1      Y7-9         GRT    FG    F          2011       0                           c

Thank you in advance

Last edited by Franklin52; 04-23-2014 at 02:59 AM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. Programming

Question on order of headers and WEXITSTATUS

In one of the Unix Programming FAQ's they have the following headers in the program to catch SIGCHLD #include <sys/types.h> /* include this before any other sys headers */ #include <sys/wait.h> /* header for waitpid() and various macros */ #include <signal.h> /* header for signal... (5 Replies)
Discussion started by: frequency8
5 Replies

2. UNIX for Dummies Questions & Answers

Changing the order of columns in a script

I have the following script: (echo -n `curl http://www.example.com/scores.txt | grep mylocation`; date +%Y%m%d%H%M%S) >> myscores.txt This script works fine, except that it places the timestamp at the end of the file myscores.txt. How do add the timestamp as the first column and then a tab and... (4 Replies)
Discussion started by: figaro
4 Replies

3. Shell Programming and Scripting

Remove text between headers while leaving headers intact

Hi, I'm trying to strip all lines between two headers in a file: ### BEGIN ### Text to remove, contains all kinds of characters ... Antispyware-Downloadserver.com (Germany)=http://www.antispyware-downloadserver.c om/updates/ Antispyware-Downloadserver.com #2... (3 Replies)
Discussion started by: Trones
3 Replies

4. Shell Programming and Scripting

Merging of files with different headers to make combined headers file

Hi , I have a typical situation. I have 4 files and with different headers (number of headers is varible ). I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only). For example - File 1 H1|H2|H3|H4 11|12|13|14 21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies

5. Shell Programming and Scripting

Transpose multipe columns to rows and adding headers

Hi, I found the following awk script to transpose multiple (3) columns to multiple rows: #=== BEGIN {FS=","} { for (i=1;i<=NF;i++) { arr=$i; if(nf<= NF) nf=NF; } nr=NR } END { for(i=1;i<=nf;i++) { (8 Replies)
Discussion started by: Gery
8 Replies

6. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

7. Shell Programming and Scripting

copying columns with headers' specific pattern

Hi friends, I have data in tab separated file with headers like this : *sml1 *sml3 *smln7 smfk9 smllf56... Which shell command I should use if i want to extract entire columns that have header names beginning with "*" ? i want to copy these columns into another file. Thanks, (14 Replies)
Discussion started by: jacks
14 Replies

8. Shell Programming and Scripting

Re-order columns

I am trying to reorder columns based on a value in a field in the attached file. Thank you :). If $11 is not equal to "unknown", then in a new text file $4, $11, $7, $13 For example, row 24 $11 is not unknown A_16_P39089234, chr10:76765556-76765615, KAT6B, 0.9337 (5 Replies)
Discussion started by: cmccabe
5 Replies

9. UNIX for Advanced & Expert Users

Cannot find logical file format for BSD file headers.

Hi. Unix rookie here. Been looking for a few days for reference documents on how BSD UNIX lays the logical file format onto a disk. Goal is to view/edit with hex editor for data repair. Lots of docs are available for how to use Unix commands (like xxd), but I want to learn the map of how Unix... (4 Replies)
Discussion started by: Chris_top_he_r
4 Replies

10. Shell Programming and Scripting

awk to compare files and validate order of headers

The below awk verifies the count and order of each text file in the directory. The script does execute and produce output, however the order of the headers are not compared to key. The portion in bold is supposed to do that. If the order of the headers in each text file is the same as key, then... (0 Replies)
Discussion started by: cmccabe
0 Replies
EVENT2VRULE(1)						      General Commands Manual						    EVENT2VRULE(1)

NAME
event2vrule - adds 'events' to rrdtool graphs SYNOPSIS
event2vrule <params> DESCRIPTION
Adding Events to Graphs There is a new graphing feature which allows you to specify events that should be displayed in your graphs. These events are simply a list of points in time at which something of interest occurred. For instance, one could create a plain text file in the graphs directory called events.txt containing these lines: 2001/02/10 1538 added support for events to FlowScan graphs 2001/02/12 1601 allowed the events file to be named on make command line Then to generate the graphs with those events included one might run: $ make -f graphs.mf events=events.txt This feature was implemented using a new script called event2vrule that is supplied with FlowScan. This script is meant to be used as a ``wrapper'' for running rrdtool(1), similarly to how one might run nohup(1). E.g.: $ event2vrule -h 48 events.txt rrdtool graph -s -48h ... That command will cause these VRULE arguments to be passed to rrdtool, at the end of the argument list: COMMENT: VRULE:981841080#ff0000:2001/02/10 1538 added support for events to FlowScan graphs COMMENT: VRULE:982015260#ff0000:2001/02/12 1601 allowed the events file to be named on make command line COMMENT: SEE ALSO
add_ds.pl(1), add_txrx(1), flowscan(1), ip2hostname(1), locker(1) AUTHOR
This manual page was written by Anibal Monsalve Salazar, for the Debian Project. EVENT2VRULE(1)
All times are GMT -4. The time now is 11:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy