fill in last column of data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fill in last column of data
# 1  
Old 02-29-2012
fill in last column of data

Hello,

I am fairly new to awk, and I have the following problem.

My file has missing data in the last column, and the program I am pre-processing this file for cannot interpret correctly shortened rows (it just wraps the data around).

Is there a way to force awk to create the same number of columns for each row (based on the number of headers in the first row), even if the column is blank? Or, alternatively, how can I fill in missing data in the last column with "N/A" or something like that.

Thank you for your help.
# 2  
Old 02-29-2012
Try using printf instead of print...
This User Gave Thanks to Vidhyaprakash For This Post:
# 3  
Old 02-29-2012
Thanks. Is it possible to make this work without knowing how many columns the file has? (i.e. can I put a for loop inside a printf statement?)
# 4  
Old 02-29-2012
the below code checks for three fileds, and if it is not there, then fill with "NA"
Code:
 
$ cat test.txt 
one two three
one two
one 
one two three
$ nawk '{if(NF<3){for(i=1;i<=3;i++){if($i!~/./){printf("NA ")}else{printf("%s ",$i)}if(i==3){printf("\n")}}}else{print}}' test.txt
one two three
NA NA NA 
one two NA 
NA NA NA 
one NA NA 
NA NA NA 
one two three

# 5  
Old 02-29-2012
Can you give a sample input and sample output you are expecting please???
# 6  
Old 02-29-2012
Thanks for your help. Here is what I ended up doing:

Code:
{
 i = 1
 if (NR == 1) {
  numfields=NF;
  for (i=1; i<=(numfields-1); i++) {
   printf "%s\t", $(i) ;
  }
  printf "%s\n", $(numfields) ;
 } else if (NR < 10) {
  for (i=1; i<=(numfields-1); i++) {
   printf "%s\t", $(i) ;
  }
  printf "%s\n", $(numfields) ;
 } 
}

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 03-01-2012 at 03:08 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Old 02-29-2012
Little modify in kamaraj's code ..
Code:
$ a=$(awk 'NR==1{print NF}' infile)
$ awk -v val="$a" '{if(NF<val){for(i=1;i<=val;i++){if($i!~/./){printf("NA ")}else{printf("%s ",$i)}if(i==val){printf("\n")}}}else{print}}' infile
one two three
one two NA
one NA NA
one two three

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fill column from previous row

Hi, I have the following content in file ABBR DESC COL3 COL4 COL5 COL6 AAA text desc aaa text text text text text text text text text text text text BBB text desc bbb text text text text text text text text CCC ... (10 Replies)
Discussion started by: bobbygsk
10 Replies

2. UNIX for Dummies Questions & Answers

Fill csv entire column with content from another text file

I have a csv that looks like this: ,yude-to-nap2,0,0,0,0,0 ,2twis-yude-to-nap2,0,0,0,0,0 ,2tiws-yude-to-nap2,0,0,0,0,0 ,2arcos-yude-to-nap2,0,0,0,0,0 and another file named m1 that has a single line of text as content: Feb 1 15:30:20 How can I fill the whole the empty column of the... (1 Reply)
Discussion started by: RobertoRivera
1 Replies

3. Shell Programming and Scripting

Fill data in column with previous value

Gents, Kindly help me. I have a file with empty values in selected column, I will like to fill the empty values with the previous value. Example Input file X 4959 30010 66727.00 20457.001 1 1441 66512.00 20234.00 20520.001 X 4959 30010 66727.00 20457.001 145 ... (7 Replies)
Discussion started by: jiam912
7 Replies

4. Shell Programming and Scripting

Fill data if number range is given

Hi I want to get all numbers if number range is given as input. Eg: INPUT FILE 100-105 107 108-112 OUTPUT REQUIRED: 100 101 102 103 104 105 107 108 109 110 111 112 How can I do it using shell? :confused: Thanks in advance. (11 Replies)
Discussion started by: dashing201
11 Replies

5. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

6. Shell Programming and Scripting

Script which fill data in XML file

Hello, I need help for writing a script that fills already generated xml file with data from oracle database and random sequences. For example if we have the following tags: <ns1:message> <ns1:messageId> </ns1:messageId> <ns1:languageCode> </ns1:languageCode>... (10 Replies)
Discussion started by: zb99
10 Replies

7. Shell Programming and Scripting

Fill in missing Data

hello everyone, I have a task to input missing data into a file. example of my data below: Wed Feb 01 09:00:02 EST 2012,,,0.4,0.3,,0.3,,0.3,,0.5,,0.3,,,0.4,0.3, Wed Feb 01 09:00:11 EST 2012,,,,,,,0.2,,,,,,,,,, Wed Feb 01 09:00:22 EST... (23 Replies)
Discussion started by: Nolph
23 Replies

8. Shell Programming and Scripting

Fill missing numbers in second column with zeros

Hi All, I have 100 files with names like this: 1.dat, 2.dat, 3.dat until 100.dat. My dat files look like this: 42323 0 438939 1 434 0 0.9383 3434 120.23 3 234 As you can see in the second column, some numbers are missing. I want to fill those missing places with 0's in all... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

9. Shell Programming and Scripting

How to fill data from other file and get some output

Greetings, I have a hard time creating a large number of user profiles in a database. The data file looks like this : 01/01/80 Mitch Conley . . . . And I need to put the output into: Name: Mitch Surname: Conley Birthday: 01/01/80 Thanks in advance! (3 Replies)
Discussion started by: hemo21
3 Replies

10. UNIX for Dummies Questions & Answers

how to fill the name of the spread sheet as a cell's data

In open office spreadsheet, i would like to fill a cell with the name of the spread sheet. That is if the file name of spread sheet is, team.ods, then i have to fill a specific cell with that name 'team'. How to do that ? I have more spread sheets to be created, so i want it to be done... (0 Replies)
Discussion started by: thegeek
0 Replies
Login or Register to Ask a Question