Fill data in column with previous value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fill data in column with previous value
# 1  
Old 02-14-2015
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
Code:
X  4959     30010  66727.00  20457.001    1  1441  66512.00  20234.00  20520.001
X  4959     30010  66727.00  20457.001  145  2961  66524.00  20234.00  20536.001
X  4959     30010  66727.00  20457.001  297  4461  66536.00  20234.00  20532.001
X           30010  66727.00  20457.001  447  6021  66548.00  20234.00  20544.001
X           30010  66727.00  20457.001  603  7621  66560.00  20234.00  20552.001
X           30010  66727.00  20457.001  763  9261  66572.00  20234.00  20560.001
X  4959     30010  66727.00  20457.001  927 10911  66584.00  20234.00  20562.001
X  4959     30010  66727.00  20457.001 1092 12591  66596.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1260 14271  66608.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1428 15971  66620.00  20234.00  20572.001
X  4960     30010  66727.00  20457.001 1598 17561  66632.00  20234.00  20550.001
X  4960     30010  66727.00  20457.001 1757 19311  66644.00  20234.00  20582.001
X  4960     30010  66727.00  20457.001 1932 21071  66656.00  20234.00  20584.001
X           30010  66727.00  20457.001 2108 22861  66668.00  20234.00  20590.001
X           30010  66727.00  20457.001 2287 24661  66680.00  20234.00  20592.001
X           30010  66727.00  20457.001 2467 26421  66692.00  20234.00  20584.001
X  4970     30010  66727.00  20457.001 2643 28161  66704.00  20234.00  20580.001
X  4970     30010  66727.00  20457.001 2817 30001  66716.00  20234.00  20600.001
X  4970     30110  66511.00  20273.001    1  2241  66512.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  225  4481  66524.00  20050.00  20496.001
X           30110  66511.00  20273.001  449  6721  66536.00  20050.00  20496.001
X           30110  66511.00  20273.001  673  8961  66548.00  20050.00  20496.001

Output desired

Code:
X  4959     30010  66727.00  20457.001  145  2961  66524.00  20234.00  20536.001
X  4959     30010  66727.00  20457.001  297  4461  66536.00  20234.00  20532.001
X  4959     30010  66727.00  20457.001  447  6021  66548.00  20234.00  20544.001
X  4959     30010  66727.00  20457.001  603  7621  66560.00  20234.00  20552.001
X  4959     30010  66727.00  20457.001  763  9261  66572.00  20234.00  20560.001
X  4959     30010  66727.00  20457.001  927 10911  66584.00  20234.00  20562.001
X  4959     30010  66727.00  20457.001 1092 12591  66596.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1260 14271  66608.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1428 15971  66620.00  20234.00  20572.001
X  4960     30010  66727.00  20457.001 1598 17561  66632.00  20234.00  20550.001
X  4960     30010  66727.00  20457.001 1757 19311  66644.00  20234.00  20582.001
X  4960     30010  66727.00  20457.001 1932 21071  66656.00  20234.00  20584.001
X  4960     30010  66727.00  20457.001 2108 22861  66668.00  20234.00  20590.001
X  4960     30010  66727.00  20457.001 2287 24661  66680.00  20234.00  20592.001
X  4960     30010  66727.00  20457.001 2467 26421  66692.00  20234.00  20584.001
X  4970     30010  66727.00  20457.001 2643 28161  66704.00  20234.00  20580.001
X  4970     30010  66727.00  20457.001 2817 30001  66716.00  20234.00  20600.001
X  4970     30110  66511.00  20273.001    1  2241  66512.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  225  4481  66524.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  449  6721  66536.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  673  8961  66548.00  20050.00  20496.001

Thanks for your help.
# 2  
Old 02-14-2015
Try:
Code:
perl -pe '$x=$1 if /^X  (\S+)/;s/^X {6}/X  $x/' file

# 3  
Old 02-14-2015
Hello jiam912,

Following may also help but I didn't test it as don't have system now.Smilie
Code:
awk '{if($2 == ""){$2=A}} {A=$2} 1' Input_file

Thanks,
R. Singh
# 4  
Old 02-14-2015
Here is an awk solution:

Code:
$ awk '{if (substr($0,4,4)=="    ") {print substr($0,1,2),save,substr($0,9);} else {save=substr($0,4,4); print $0}}' file.txt
X  4959     30010  66727.00  20457.001    1  1441  66512.00  20234.00  20520.001
X  4959     30010  66727.00  20457.001  145  2961  66524.00  20234.00  20536.001
X  4959     30010  66727.00  20457.001  297  4461  66536.00  20234.00  20532.001
X  4959     30010  66727.00  20457.001  447  6021  66548.00  20234.00  20544.001
X  4959     30010  66727.00  20457.001  603  7621  66560.00  20234.00  20552.001
X  4959     30010  66727.00  20457.001  763  9261  66572.00  20234.00  20560.001
X  4959     30010  66727.00  20457.001  927 10911  66584.00  20234.00  20562.001
X  4959     30010  66727.00  20457.001 1092 12591  66596.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1260 14271  66608.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1428 15971  66620.00  20234.00  20572.001
X  4960     30010  66727.00  20457.001 1598 17561  66632.00  20234.00  20550.001
X  4960     30010  66727.00  20457.001 1757 19311  66644.00  20234.00  20582.001
X  4960     30010  66727.00  20457.001 1932 21071  66656.00  20234.00  20584.001
X  4960     30010  66727.00  20457.001 2108 22861  66668.00  20234.00  20590.001
X  4960     30010  66727.00  20457.001 2287 24661  66680.00  20234.00  20592.001
X  4960     30010  66727.00  20457.001 2467 26421  66692.00  20234.00  20584.001
X  4970     30010  66727.00  20457.001 2643 28161  66704.00  20234.00  20580.001
X  4970     30010  66727.00  20457.001 2817 30001  66716.00  20234.00  20600.001
X  4970     30110  66511.00  20273.001    1  2241  66512.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  225  4481  66524.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  449  6721  66536.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  673  8961  66548.00  20050.00  20496.001

Code:
awk '{if($2 == ""){$2=A}} {A=$2} 1' Input_file

RavinderSingh13's awk solution above does not appear to work as is since $2 will be valued from a different column (e.g 30010)when the expected field is missing (as file is column delimited).
# 5  
Old 02-15-2015
GNU awk:
Code:
gawk '$2~/^ *$/{$2=p}{p=$2}1' OFS= FIELDWIDTHS="2 5 999" file

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-15-2015
Dear Scrutinizer

Can you explain me a litte about
Code:
OFS= FIELDWIDTHS="2 5 999"

Thanks

---------- Post updated at 03:47 AM ---------- Previous update was at 03:47 AM ----------

Dear Scrutinizer

Can you explain me a litte about
Code:
OFS= FIELDWIDTHS="2 5 999"

Thanks
# 7  
Old 02-15-2015
OFS= output field separator, it determines which characters are put between fields in the output, in this case it is the empty string "", so no characters. The OFS is inserted between fields, once a record is recomputed, which is the case when one of the fields is modified.

FIELDWIDTHS="2 5 999" is a GNU-only extension to awk, that determines that field 1 consists of the first two characters, field 2 the next 5 and field 3 the rest of the characters..
This User Gave Thanks to Scrutinizer For This Post:
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

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
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

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

6. 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

7. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: timert34
6 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