LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-30-2013, 01:51 AM   #1
akeka
Member
 
Registered: May 2012
Posts: 35

Rep: Reputation: Disabled
AWK fill column from previuos line column


Dear guru's

I've this input file

Code:
         1        3284510.7        2393630.5                0    1551.65893555
                                                            0    1551.65893555
         2 3284510.71875000 2392907.50000000                0    1551.65893555
                                                            0    1551.65893555
                                                   0.11904644    1551.65893555
                                                   0.16666490    1551.65893555
                                                   0.21428336    1551.65893555
                                                   0.26190186    1551.65893555
                                                   0.30952030    1551.65893555
                                                   0.35713875    1551.65893555
                                                   0.40475720    1551.65893555
         3 3284510.71875000 2392184.50000000                0    1551.65893555
                                                            0    1551.65893555
                                                   0.11904646    1551.65893555
                                                   0.16666491    1551.65893555
                                                   0.21428338    1551.65893555
                                                   0.26190186    1551.65893555
As you may aware of, I want to fill null column 1,2 and 3 from previous line that has value

Like this

Code:
         1        3284510.7        2393630.5                0    1551.65893555
         1        3284510.7        2393630.5                0    1551.65893555
         2 3284510.71875000 2392907.50000000                0    1551.65893555
         2 3284510.71875000 2392907.50000000                0    1551.65893555
         2 3284510.71875000 2392907.50000000                0.11904644    1551.65893555
         2 3284510.71875000 2392907.50000000          		0.16666490    1551.65893555
         2 3284510.71875000 2392907.50000000    			0.21428336    1551.65893555
         2 3284510.71875000 2392907.50000000    			0.26190186    1551.65893555
         2 3284510.71875000 2392907.50000000    			0.30952030    1551.65893555
         2 3284510.71875000 2392907.50000000    			0.35713875    1551.65893555
         2 3284510.71875000 2392907.50000000   				0.40475720    1551.65893555
         3 3284510.71875000 2392184.50000000                0    1551.65893555
         3 3284510.71875000 2392184.50000000     	        0    1551.65893555
         3 3284510.71875000 2392184.50000000    			0.11904646    1551.65893555
         3 3284510.71875000 2392184.50000000    			0.16666491    1551.65893555
         3 3284510.71875000 2392184.50000000   				0.21428338    1551.65893555
         3 3284510.71875000 2392184.50000000   				0.26190186    1551.65893555
Thank you for any help
 
Old 01-30-2013, 04:54 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 347

Rep: Reputation: 145Reputation: 145
Code:
NF == 5 {
	store_1 = $1
	store_2 = $2
	store_3 = $3
	print
}
NF == 2 {
	print store_1, store_2, store_3, $1, $2
}
You can pipe it through “columns -t” to get nicely aligned columns.
 
1 members found this post helpful.
Old 01-30-2013, 05:18 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,452

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
A slight variation to the code suggested by mina86 in order to preserve the original (fixed-width) format:
Code:
BEGIN {
  FIELDWIDTHS = "10 17 17 17 17"
}

$1 ~ /^ +$/ {
   print store_1 store_2 store_3 $4 $5
}

$1 ~ /[0-9]/ {
  store_1 = $1
  store_2 = $2
  store_3 = $3
  print
}
 
1 members found this post helpful.
Old 01-30-2013, 07:43 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
Same fish, different fillet:
Code:
awk 'BEGIN{FIELDWIDTHS = "10 17 17 17 17"}$0 = ($1 !~ /^ +$/ && a = $1 $2 $3)?$0:a $4 $5' file
 
1 members found this post helpful.
Old 01-30-2013, 07:16 PM   #5
akeka
Member
 
Registered: May 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Thanks guru for all your help

I've solved the problem with your help

Thanks again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare second column of a file then print the first column of it in a ne fil if true java_girl Linux - Newbie 2 03-16-2012 04:50 AM
awk print word on each line on 3 column mad_penguin Programming 9 09-22-2011 05:59 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM


All times are GMT -5. The time now is 03:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration