Fill column from previous row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fill column from previous row
# 8  
Old 06-02-2017
Quote:
Originally Posted by bobbygsk
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.
Code:
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:
Code:
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
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 06-02-2017
text desc aaa (and the like) are NOT tab delimited - they're ONE field with multiple space-separated strings.
# 10  
Old 06-04-2017
Quote:
Originally Posted by RavinderSingh13
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.
Code:
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:
Code:
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
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.
# 11  
Old 06-04-2017
Quote:
Originally Posted by bobbygsk
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.
This User Gave Thanks to Don Cragun 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

Perl script to fill the entire row of Excel file with color based on pattern match

Hi All , I have to write one Perl script in which I need to read one pre-existing xls and based on pattern match for one word in some cells of the XLS , I need to fill the entire row with one color of that matched cell and write the content to another excel Please find the below stated... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

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 X 4959 30010 66727.00 20457.001 1 1441 66512.00 20234.00 20520.001 X 4959 30010 66727.00 20457.001 145 ... (7 Replies)
Discussion started by: jiam912
7 Replies

3. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

4. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

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

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

7. Shell Programming and Scripting

remove row if string is same as previous row

I have data like: Blue Apple 6 Red Apple 7 Yellow Apple 8 Green Banana 2 Purple Banana 8 Orange Pear 11 What I want to do is if $2 in a row is the same as $2 in the previous row remove that row. An identical $2 may exist more than one time. So the out file would look like: Blue... (4 Replies)
Discussion started by: dcfargo
4 Replies

8. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

9. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies

10. Shell Programming and Scripting

Value of previous row using awk

I would like to know value for previous row, in addition to current row. How would I will get value for previous row? How can I perform this with awk? (2 Replies)
Discussion started by: videsh77
2 Replies
Login or Register to Ask a Question