cut and paste?

hi,

I have a file with content like this for an employee:

EmployeeID
101
Day_type, day
vacation,1/2/2009
sick day, 3/2/2009
personal day, 4/5/2009
jury duty day, 5/5/2009

how do I make the result to show:

EmployeeID,Day_type,day
101,vacation,1/2/2009
101,sick day, 3/2/2009
101,personal day, 4/5/2009
101,jury duty day, 5/5/2009
 

I thought I could do copy and paste, but if I do copy and paste, isn't EmployeeID only be in the first row?

thanks

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 04:00 PM ---------- Previous update was at 03:47 PM ----------

nawk '
  /EmployeeID/ {c=3; header=$0;next}
  c && --c {if(c==2) id=$0; if (c==1) print header OFS $0;next}
  { $0=id OFS $0;print}' OFS=, myFile

Here is another way using Perl...

Assuming you have a file "pay1" containing:

EmployeeID
101
Day_type, day
vacation,1/2/2009
sick day, 3/2/2009
personal day, 4/5/2009
jury duty day, 5/5/2009

Here is a perl program "pay1.pl"...

#!/usr/bin/perl -w
while (<>) {                    #while not end of standard input
   chomp;                       #Take of end of line char
   $line = $_;                  #whole line string wo/end of line char
   @a = split (/,/, $line);     #Split line by comma into array
   if ($. == 1) {               #only on line 1
      $col1_title = $a[0];      #Grab text EmployeeID
   }
   elsif ($. == 2) {            #only on line 2
      $col1_value = $a[0];      #Grab 101
   }
   elsif ($. == 3) {            #only on line 3
      $col2_title = $a[0];      #Grab Day_type
      $col3_title = $a[1];      #Grab day
                                #print heading line
      printf("%s,%s,%s\n", $col1_title, $col2_title, $col3_title);
   }
   else {                       #any line other than 1,2,3
      $col2_value = $a[0];      #Grab value of day type
      $col3_value = $a[1];      #Grab value of day
                                #print out 101, type value, day value
      printf("%s,%s,%s\n", $col1_value, $col2_value, $col3_value);
   }
}

You run the program passing the file using standard input...

cat pay1 | perl pay1.pl

Output is...

EmployeeID,Day_type, day
101,vacation,1/2/2009
101,sick day, 3/2/2009
101,personal day, 4/5/2009
101,jury duty day, 5/5/2009

Thanks.

I don't have nawk in my box. I have awk.
I used awk instead of nawk and it worked fine.

I understand nawk is newer version. what are somethings nawk can do but awk can't do?

Perl works fine,too.

wouldn't hurt to try.....

Sorry, after I google about AWK and learned it, I still have trouble to understand your syntax. I understand EmployeeID is the search pattern. OFS is Field Separator comma.

awk <search pattern> {<program actions>}

What I am confused is program action. Could you kindly explain?

  1. what is that mean? c && --c
  2. next; does that mean exit the program action and going to next program action?

Thanks

in mnemonic code:

if (c != 0) {
   c=c-1
   if (c != 0)
      doSomethingHere
   

it means, don't execute any of the following awk code AND advance to the 'next' data record/line.