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   text desc ccc text text text text
                    text text text text
                    text text text text
                    text text text text
DDD   test desc ddd text text text text

and I'm expecting the following

ABBR  DESC          COL3 COL4 COL5 COL6
AAA   text desc aaa text text text text
AAA   text desc aaa text text text text
AAA   text desc aaa text text text text
BBB   text desc bbb text text text text
BBB   text desc bbb text text text text
CCC   text desc ccc text text text text
CCC   text desc ccc text text text text
CCC   text desc ccc text text text text
CCC   text desc ccc text text text text
DDD   test desc ddd text text text text

Appreciate your help

making some assumptions.... given myFile:

ABBR  DESC          COL3 COL4 COL5 COL6
AAA   text desc aaa text1 text text text
                    text text text text
                    text text text text
BBB   text desc bbb text2 text text text
                    text text text text
CCC   text desc ccc text3 text text text
                    text text text text
                    text text text text
                    text text text text
DDD   test desc ddd text4 text text text
awk '
  FNR>1 {
    if (NF>4)
       miss=substr($0,1, length-index($0, $(NF-3)))
    else
       $1=miss OFS $1
}
1' myFile

we get:

ABBR  DESC          COL3 COL4 COL5 COL6
AAA   text desc aaa text1 text text text
AAA   text desc aaa text text text text
AAA   text desc aaa text text text text
BBB   text desc bbb text2 text text text
BBB   text desc bbb text text text text
CCC   text desc ccc text3 text text text
CCC   text desc ccc text text text text
CCC   text desc ccc text text text text
CCC   text desc ccc text text text text
DDD   test desc ddd text4 text text text

Dear bobbygsk ,

With 149 posts to date, I'm hoping you could answer these basic questions first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

---------- Post updated at 11:24 AM ---------- Previous update was at 11:23 AM ----------

Hi Vgersh99...

The data has tab between columns. Not working.
Appreciate your help though

---------- Post updated at 11:24 AM ---------- Previous update was at 11:24 AM ----------

rbatte1... This is not homework question.
1) did not touch unix for long time.
2) I tried prev posts here. but none of them have space in a column value. So did not get the required results.
3) So much of other deadlines to meet so not able to think properly.

should have mentioned the field separator in the beginning...
try this:

awk -F'\t' '
  FNR>1  && NF{
    if ($1)
      miss=$1 OFS $2
    else
      $0=(miss OFS $(NF-3) OFS $(NF-2) OFS $(NF-1) OFS $NF)
}
1' OFS='\t' myFile
2 Likes

Hello bobbygsk,

Could you please try following and let me know if this helps you.

awk 'FNR==1{$2=$2 OFS OFS;print;next}!/^ /{$1=$1;VAL=$1 OFS $2 OFS $3 OFS $4;print;next} {$1=$1;print VAL,$0}' OFS="\t"   Input_file

If you have TAB delimited Input_file then add FS="\t" before OFS="\t" .

Thanks,
R. Singh

Thanks. It works. Appreciate your help

---------- Post updated at 01:40 PM ---------- Previous update was at 01:37 PM ----------

Tried with FS="\t" before OFS="\t" but did not work.

Appreciate your time trying to solve.

Thanks

Hello bobbygsk,

Your Input_file was NOT TAB delimited, so I made it as tab delimited and tried my command and it is working fine only.

cat Input_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     text    desc    ccc     text    text    text    text
                        text    text    text    text
                        text    text    text    text
                        text    text    text    text
DDD     test    desc    ddd     text    text    text    text

After running script:

awk 'FNR==1{$2=$2 OFS OFS;print;next}!/^ /{$1=$1;VAL=$1 OFS $2 OFS $3 OFS $4;print;next} {$1=$1;print VAL,$0}' OFS="\t"  Input_file
ABBR    DESC                    COL3    COL4    COL5    COL6
AAA     text    desc    aaa     text    text    text    text
AAA     text    desc    aaa     text    text    text    text
AAA     text    desc    aaa     text    text    text    text
BBB     text    desc    bbb     text    text    text    text
BBB     text    desc    bbb     text    text    text    text
CCC     text    desc    ccc     text    text    text    text
CCC     text    desc    ccc     text    text    text    text
CCC     text    desc    ccc     text    text    text    text
CCC     text    desc    ccc     text    text    text    text
DDD     test    desc    ddd     text    text    text    text

Thanks,
R. Singh

2 Likes

text desc aaa (and the like) are NOT tab delimited - they're ONE field with multiple space-separated strings.

The input is tab delimited and output should also be the same. But I do not need to change the space to tab.

Appreciate your time to help.

Although your real input may be tab delimited and the output you want may also be tab delimited, the sample input and output files you have provided in this thread do not contain any <tab> characters; only <space>s.

You are much more likely to get code suggestions that will work with your input data to produce the output you want if you post sample input and output that is representative of your actual data. Providing spaces in your sample input and output instead of tabs and then complaining that the code suggestions you have received don't preserve tabs is disingenuous.

1 Like