Prayag
Prayag

Reputation: 211

Repeat values of 1st column in a 1st row in below empty space of next row

I have an input as below from a system

a b c d
  h i j
  e f g

a q w e 
  r r t
  y u i

a i o p
  j k l
  t y u

and I need to arrange the output as

a b c d
a h i j
a e f g

a q w e 
a r r t
a y u i

a i o p
a j k l
a t y u

I have already tried with 'awk' as

awk -v FS="[[:space:]][[:space:]]+" -v OFS="/t" '{print $1,$2,$3,$4}' .

Upvotes: 1

Views: 84

Answers (5)

Ed Morton
Ed Morton

Reputation: 203532

$ awk '{if (/^ /) $1=val FS $1; else val=$1} 1' file
a b c d
a h i j
a e f g

a q w e
a r r t
a y u i

a i o p
a j k l
a t y u

Upvotes: 2

potong
potong

Reputation: 58420

This might work for you (GNU sed):

 sed 'N;s/^\(\(\S*\s*\).*\n\)\s*\b/\1\2/;P;D' file

Replace the empty space at the beginning of a non-empty line with the previous lines first column and its following spaces.

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133518

Could you please try following it takes care of initial space removing too.

awk '{value=$0~/^ /?value:$1} /^ /{sub(/^ +/,value OFS)} 1' Input_file

Explanation: Adding explanation for above code too.

awk '
{
  value=$0~/^ /?value:$1  ##Create variable named value whose value is variable value when line starts with space else it will be first field value.
}
/^ /{                     ##Checking if a line starts with space then do following.
  sub(/^ +/,value OFS)    ##Substituting initial space with variable value and OFS value in current line.
}
1                         ##Mentioning 1 here to print edited/non-edited value of current line.
' Input_file              ##Mentioning Input_file name here.

Output will be as follows.

a b c d
a h i j
a e f g

a q w e
a r r t
a y u i

a i o p
a j k l
a t y u

Upvotes: 1

Sundeep
Sundeep

Reputation: 23667

Since it is tagged linux, I'm assuming gawk is available

awk -v FIELDWIDTHS='2 2 2 1' -v OFS= '{if($1=="  ")$1=p; else p=$1} 1' ip.txt
  • FIELDWIDTHS allows to specify width of each field
  • 2 means two characters, 1 means one character and so on
  • -v OFS= empty OFS since field separator is part of field values
  • if($1==" ")$1=p; else p=$1 if first field is empty(meaning two spaces in this example), assign it previously stored value


awk -v OFS='\t' 'NF==3{$1 = p OFS $1} NF==4{p=$1; $1=$1} 1' ip.txt

This will work for varying whitespace in-between input fields and format the output to use tab as field separator

Upvotes: 1

Inian
Inian

Reputation: 85590

You could use an awk logic as below

awk 'a=/^ /{sub(/^[[:space:]]/,"",$0);$0=x$0}!a{x=$1}1' file

which should print the output as expected.

How it works?

  1. The condition a=/^ / would return 0 for the lines that do not start with an empty field. So in those lines we backup the value of $1 in variable x
  2. For the lines starting with an empty field the condition would be 1, so we strip our a single leading space from the existing line and append with the previous value of $1 (variable x)

The logic is compatible in any awk version and independent of the number of fields present in each line.

Upvotes: 1

Related Questions