Copying down first row in to all the below blank rows in a .csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying down first row in to all the below blank rows in a .csv file
# 1  
Old 04-02-2012
Copying down first row in to all the below blank rows in a .csv file

Hi All,
I have many of files(.csv) of the format given below.
Code:
Date,Name,Location
04/02/2012,A,India
,B,China
,C,USA

Like this I have 1000's of rows and many columns in all my files.

I need a shell script to copy down the Date(in this example column1) to the next 2 rows below(in the same column) also.
Required output.
Code:
Date,Name,Location
04/02/2012,A,India
04/02/2012,B,China
04/02/2012,C,USA

Many thanks in advance..
Sidda

Last edited by Scrutinizer; 04-02-2012 at 08:21 AM.. Reason: code tags, formatting
# 2  
Old 04-02-2012
Code:
awk -F, -vOFS=, '$1=="" {$1=oldone}1 $1!="" {oldone=$1}1' file.csv

# 3  
Old 04-02-2012
Code:
sed "s#^,#$(date "+%m/%d/%Y")&#" infile
Date,Name,Location
04/02/2012,A,India
04/02/2012,B,China
04/02/2012,C,USA

# 4  
Old 04-02-2012
Code:
awk -F, '$1=$1?p=$1:p' OFS=, infile


Last edited by Scrutinizer; 04-02-2012 at 08:42 AM..
# 5  
Old 04-02-2012
Could you please explain this

Code:
'$1=$1?p=$1:p'

Thanks
# 6  
Old 04-03-2012
Problem with input data

Thanks Carlo and Scrutinizer

Both of your scripts worked very well. But what I found is that some of the .csv files have the following type of data in them(example).

Code:
SerialNo, Location, Country
1,Delhi,India
,Mumbai,India
,Bangalore,India
2,NewYork,USA
,SantaClara,USA
3,Frankfurt,Germany

What I need is the following output.
SerialNo, Location, Country
Code:
1,Delhi,India
1,Mumbai,India
1,Bangalore,India
2,NewYork,USA
2,SantaClara,USA
3,Frankfurt,Germany

Can you modify the scripts written by you to get my desired output.
Thanks

Last edited by Scrutinizer; 04-03-2012 at 01:51 AM..
# 7  
Old 04-03-2012
You mean skip the first line?
Code:
awk -F, 'NR>1{$1=$1""?p=$1:p}1' OFS=, infile

or more readable (more like CarloM's suggestion):
Code:
awk -F, 'NR>1{if($1=="")$1=p; else p=$1}1' OFS=, infile

----

Quote:
Originally Posted by avatar_007
Could you please explain this

Code:
'$1=$1?p=$1:p'

Thanks
This means: if $1 is unequal to "" or 0 then $1 remains $1, while we are setting p to the current $1 value ($1=p=$1) else set $1 to p.
There was a bug in this code, it should replace the value in column 1 if it is empty, not when it is 0. This could be corrected like this:
Code:
awk -F, '{$1=x$1?p=$1:p}1' OFS=, infile

So now it reads: if x$1 is unequal to "" or 0 then $1 remains $1 .. etc..
x is empty and therefore x is equal to "" . By concatenating the empty string x to $1 , $1 if forced to be interpreted as a string, not as a numerical value. Why is this necessary? If $1 contains the numerical value 0 then this would translate to false, but if $1 contains the string value "0" then this translates to "true" (only the string value "" is translates to false).

This can seem a bit complex, so a more readable version may be preferable and I think this should be considered more for learning the intricacies of awk...

Last edited by Scrutinizer; 04-03-2012 at 02:51 AM..
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

[bash] - Replace blank and string in csv file

Hi all, i have a .csv file with only two columns, like: Login;Status Luca;S Marco; Stefano; Elettra;S Laura; ... I need to replace the blank space on Status column whit Enabled end, on the same column, S whit Disabled, like: Login;Status Luca;Disabled Marco;Enabled Stefano;Enabled... (10 Replies)
Discussion started by: kamose
10 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. Shell Programming and Scripting

Detect blank spaces in a CSV file

I'm monitoring a WLAN network to keep track of new SSIDs popping up. The SSIDs are stored along with the AP MAC address and a few other parameters in a CSV file. A typical line could look like this: 18:70:9f:e3:80:aa 10:11:15 MyNetwork 2437 Now, the problem is that some networks use SSIDs... (4 Replies)
Discussion started by: Zooma
4 Replies

4. Shell Programming and Scripting

Inserting blank columns in already present CSV file

Hi, i have a csv file which have headers and values of it like below : headers --> CI Ref SerialNumber LastScanDate values --> VMware-42,VMware-42,Tue, 20 May 2014 11:03:44 +0000 i want to have a above csv in below format : headers --> CI Name CI Description CI Ref... (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

5. Shell Programming and Scripting

Moving or copying first rows and last rows into another file

Hi I would like to move the first 1000 rows of my file into an output file and then move the last 1000 rows into another output file. Any help would be great Thanks (6 Replies)
Discussion started by: kylle345
6 Replies

6. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. Shell Programming and Scripting

How to remove Blank rows in a csv file

Hi, I need help to remove blank rows at the end of file. Sample data: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 "Tønsberg, Brygga Kino SF",Tønsberg,202-1,Tønsberg SF 4,202-1-4 ,,,, ,,,, ,,,, ,,,, Expected data: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 "Tønsberg, Brygga... (6 Replies)
Discussion started by: cnraja
6 Replies

8. Shell Programming and Scripting

Blank Space is not appending in each row of CSV File - Shell Script

I am calling SQL script in my UNIX Shell script and trying to create the CSV file and my last column value of each row is 23 blank spaces. In my SQL script,the last column is like below. RPAD(' ',23,' ') -- Padding 23 blank Spaces The CSV file is generated but the sapce(23 spaces) is... (2 Replies)
Discussion started by: praka
2 Replies

9. Shell Programming and Scripting

how to delete blank rows in a log file

Help How to delete all blank rows in log file (4 Replies)
Discussion started by: suryanarayana
4 Replies

10. Solaris

finding & replacing blank rows/spaces in a file

Can anyone help me find and replace blank rows in a file with a numeric value (ie blankrow=someTxtOrNumValue), the file is over 500,000 rows long so it would need to be the quickest way as I'll need to do this for multiple files...I would be greatfull for any suggestions....thanks sample file:... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question