Add row as a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add row as a column
# 1  
Old 08-03-2013
Add row as a column

I have a unix file which has the below format. one row with one column and few rows with 6 columns. I want to add the first row as column to the below rows.

Current file
Code:
br95
sbd 234 567 abc dsb xyz
dcb 434 567 abc dsb xyz
cbd 434 567 abc dsb xyz
2bd 234 567 abc dsb xyz
br75
sbd 234 567 abc dsb xyz
dcb 434 567 abc dsb xyz
db83
cbd 434 567 abc dsb xyz
2bd 234 567 abc dsb xyz
sbd 234 567 abc dsb xyz

Expected file

Code:
br95 sbd 234 567 abc dsb xyz
br95 dcb 434 567 abc dsb xyz
br95 cbd 434 567 abc dsb xyz
br95 2bd 234 567 abc dsb xyz
br75 sbd 234 567 abc dsb xyz
br75 dcb 434 567 abc dsb xyz
db83 cbd 434 567 abc dsb xyz
db83 2bd 234 567 abc dsb xyz
db83 sbd 234 567 abc dsb xyz

# 2  
Old 08-03-2013
Here is one method using awk:

Code:
awk '{if (NF==1) x=$1; else print x,$0}' input.txt

Code:
br95 sbd 234 567 abc dsb xyz
br95 dcb 434 567 abc dsb xyz
br95 cbd 434 567 abc dsb xyz
br95 2bd 234 567 abc dsb xyz
br75 sbd 234 567 abc dsb xyz
br75 dcb 434 567 abc dsb xyz
db83 cbd 434 567 abc dsb xyz
db83 2bd 234 567 abc dsb xyz
db83 sbd 234 567 abc dsb xyz

# 3  
Old 08-03-2013
syntax error

Thanks for you response. I have tried this but getting syntax error

Code:
awk '{if (NF==1) x=$1 else print x,$0}' LOG_FILE1

Code:
awk: {if (NF==1) x=$1 else print x,$0}
awk:                  ^ syntax error

# 4  
Old 08-03-2013
Quote:
Originally Posted by anita81
I have tried this but getting syntax error
Take a closer look at your code. It is not identical to what was suggested.

Regards,
Alister
# 5  
Old 08-03-2013
Thanks I missed the semi colon.

I have tried the code
Code:
 
 
awk '{if (NF==1) x=$1; else print x,$0}' LOG_FILE1

I'm getting the row copied as a column only to the next row but not all the rows below.


Code:
br95 sbd 234 567 abc dsb xyz
       dcb 434 567 abc dsb xyz
       cbd 434 567 abc dsb xyz
       2bd 234 567 abc dsb xyz
br75 sbd 234 567 abc dsb xyz
       dcb 434 567 abc dsb xyz
db83 cbd 434 567 abc dsb xyz
        2bd 234 567 abc dsb xyz
        sbd 234 567 abc dsb xyz

But not as

Code:
 
br95 sbd 234 567 abc dsb xyz
br95 dcb 434 567 abc dsb xyz
br95 cbd 434 567 abc dsb xyz
br95 2bd 234 567 abc dsb xyz
br75 sbd 234 567 abc dsb xyz
br75 dcb 434 567 abc dsb xyz
db83 cbd 434 567 abc dsb xyz
db83 2bd 234 567 abc dsb xyz
db83 sbd 234 567 abc dsb xyz

# 6  
Old 08-04-2013
Are you by any chance using a Solaris system? If so, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of just awk.
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

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
3 Replies

2. Programming

How to add one to each row values and keep it after the value in the column?

Dear Folks Hello I have a column of numbers, say: 26 79 68 I want to add one to each row value and get this desire column: 26 27 79 80 68 69 (6 Replies)
Discussion started by: sajmar
6 Replies

3. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

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

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

6. Shell Programming and Scripting

Awk to add selected row column data

Looks at the most efficient way to add up the column of data based off of the rows. Random data Name-Number-ID Sarah-2.0-15 Bob-6.3-15 Sally-1.0-10 James-1.0-10 Scotty-10.7-15 So I would select all those who have ID = 15 and then add up total number read - p "Enter ID number" Num ... (3 Replies)
Discussion started by: Ironguru
3 Replies

7. Shell Programming and Scripting

Add key column to the row

I have source like this with tab delimited Jon Newyork 7024051234 jon@email.com eric Newjersy 5021236541 eric@email.com johnson Baltimore 9142013210 john@email.com I want to add key column to each row 1 Jon Newyork 7024051234 jon@email.com 2 eric Newjersy 5021236541 eric@email.com... (4 Replies)
Discussion started by: elamurugu
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

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

10. 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
Login or Register to Ask a Question