cut and paste?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut and paste?
# 1  
Old 08-26-2009
cut and paste?

hi,

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

Code:
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:

Code:
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

Last edited by vgersh99; 08-26-2009 at 04:47 PM.. Reason: code tags, PLEASE!
# 2  
Old 08-26-2009
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 [code] and [/code] 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 ----------

Code:
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

# 3  
Old 08-27-2009
Here is another way using Perl...

Assuming you have a file "pay1" containing:
Code:
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"...
Code:
#!/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...
Code:
cat pay1 | perl pay1.pl

Output is...
Code:
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

# 4  
Old 09-01-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.

Last edited by jbchen; 09-01-2009 at 01:54 PM..
# 5  
Old 09-01-2009
Quote:
Originally Posted by jbchen
Thanks.

I don't have nawk in my box. I have awk.
wouldn't hurt to try.....
Quote:
Originally Posted by jbchen
Will the script work with awk?

BTW. Perl works fine.
# 6  
Old 09-01-2009
Quote:
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
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

Last edited by jbchen; 09-01-2009 at 04:03 PM..
# 7  
Old 09-01-2009
Quote:
Originally Posted by jbchen
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
in mnemonic code:
Code:
if (c != 0) {
   c=c-1
   if (c != 0)
      doSomethingHere

Quote:
Originally Posted by jbchen
2. next; does that mean exit the program action and going to next program action?
it means, don't execute any of the following awk code AND advance to the 'next' data record/line.
Quote:
Originally Posted by jbchen


Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in cut or paste

Hello, I have some problem in cut or paste command for my text data. Input1.txt : I use cut command : cut -d ' ' -f1 Input1.txt > result.txt result.txt : Then, I use paste command to merge result.txt. paste -d ' ' result.txt Input1.txt > output.txt output.txt showed : I use cut... (1 Reply)
Discussion started by: awil
1 Replies

2. Shell Programming and Scripting

Issue with cut and paste

let i have A file and B file A has contains 4 fields as below ---------------- f1 f2 f3 f4 B file consists of 5 fields as below -------------------- f5 f6 f7 f8 f9 need to display as below output: f5 f1 f3 f8 f9 (2 Replies)
Discussion started by: ANSHUMAN1983
2 Replies

3. Shell Programming and Scripting

need help with cut and paste command

I have a file which contains 3 fields separated by tabs example andrew kid baker I need to swap kid and baker using cut and paste commands how is this to be done? Thanks (3 Replies)
Discussion started by: drew211
3 Replies

4. UNIX for Dummies Questions & Answers

Cut paste from one file to other

Hello, I am working on unix for the first time. I have to write a shell script where i want to cut paste from one file to other. File "1234.abc" is 03,12345555 16,936,x,x,120 16,936,x,x,100 49,12345555 03,12347710 16,936,x,x,115 16,936,x,x,122 49,12347710 03,12342222... (9 Replies)
Discussion started by: swapsb
9 Replies

5. Shell Programming and Scripting

cut and paste

Hi, Need a help with shell script. I have to search for a string in one of the file, if match found, copy the line to a new file and delete the line from the exisiting file. eg: 83510000000000063800000.1800000.1600000.1600000.2400000.1800000.2000000.21... (6 Replies)
Discussion started by: gpaulose
6 Replies

6. UNIX for Dummies Questions & Answers

cut and paste from two tables

Hello, I want to be able to cut and paste columns from two tables in one command. Presently I do the following: cut -f 1,3-6,9 table1.in > table1.out cut -f 7,6,1-3 table2.in > table2.out paste table1.out -d '\t' table2.out > MergedTable.out Is there a better way to do this? ... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

7. Shell Programming and Scripting

cut and paste using awk

Hi i need a favour i have a file which has some trillions of records. The file is like this 11111000000000192831840914000000000000000000000000000 45789899090000000000000000011111111111111111111111111 I want to cut specific postions in each line like cut1-3 and assisgn it to a variable and... (5 Replies)
Discussion started by: richa2.m
5 Replies

8. Shell Programming and Scripting

Cut Paste and Insert Help

Hello I have a very large file where say each line is made up of 80 characters. I want to cut the characters from 20-30 and 50-60 from each line and then insert a delimiter between them (# or | etc). eg input file 000000000131.12.20990000590425246363375670011200140406... (5 Replies)
Discussion started by: PradeepRed
5 Replies

9. Shell Programming and Scripting

cut & paste

hi i am new to shell scripting, i have been trying to cut columns numbered 1,4 of a file consisiting of 4 columns. Each column is seperated by 2 spaces. for example: john 6102097199 tennessee usa michel 6734590899 texas USA now, i need to cut the name... (3 Replies)
Discussion started by: t_harsha18
3 Replies

10. UNIX Desktop Questions & Answers

Cut, Copy and Paste with X

One of the things that I have learned to take for granted in the Win32 world is the cut, copy and paste hotkeys of ^X, ^C and ^V. I use these keys all the time under Win32 to copy and paste information from one GUI into another GUI. My question is, does X have a similiar standard? ... (4 Replies)
Discussion started by: auswipe
4 Replies
Login or Register to Ask a Question