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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing the column for a row in a text file and adding another row
# 1  
Old 04-26-2005
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 this :
abc xyz lmn 89 lm nk o p
abc xyz lmn 90 lm nk o p

Incrementing column 4th and creating a new row for it .

awk ' { ++$4 } print $0 ' inputfile

Now this will increment the column but I dont know how to create a new row without disturbing the original row .
Infact I would like to get 10 rows like that , with 4th column of each row getting incremented . I guess once I know how it can be done for 1 row, i can put that in for loop .

Any pointers or help would be appreciated .
# 2  
Old 04-26-2005
Code:
$ more process.awk
BEGIN { c = 0 }

{ print $0; lastrow = $0 }

END { while ( c < 10 ) {
   $0 = lastrow
   ++$4
   print $0
   lastrow = $0
   ++c
   }
}
$ more tmp/foofile
abc xyz lmn 89 lm nk o p
$ awk -f process.awk tmp/foofile
abc xyz lmn 89 lm nk o p
abc xyz lmn 90 lm nk o p
abc xyz lmn 91 lm nk o p
abc xyz lmn 92 lm nk o p
abc xyz lmn 93 lm nk o p
abc xyz lmn 94 lm nk o p
abc xyz lmn 95 lm nk o p
abc xyz lmn 96 lm nk o p
abc xyz lmn 97 lm nk o p
abc xyz lmn 98 lm nk o p
abc xyz lmn 99 lm nk o p

Cheers
ZB
# 3  
Old 04-26-2005
Thanks Zazzybob . I appreciate your response . I tried your script but I am getting this error .

abc xyz lmn 89 lm nk o p
awk: can't set $0
record number 1

Do you know what could cause this ?
# 4  
Old 04-26-2005
Actually I got it . I had to use nawk . Thanks again for response . I appreciate it .
# 5  
Old 04-27-2005
Hi,
I was trying this script today . If I had to add another column and add 24 instead of 1 on that column and add new row after that , how can I do it .
For example if the input line is :
abc xyz lmn 89 lm nk o p 2 p

and I want

abc xyz lmn 89 lm nk o p 2 p
abc xyz lmn 90 lm nk o p 26 p
abc xyz lmn 91 lm nk o p 50 p
.
.
.
.
upto 10 lines .

I tried adding 24 to the $9 in the script above , like this .

BEGIN { c = 0 }

{ print $0; lastrow = $0 }

END { while ( c < 10 ) {
$0 = lastrow
++$4
$9 = $9 + 24
print $0
lastrow = $0
++c
}
}

But the result says :
abc xyz lmn 89 lm nk o p 2 p

1 24
1 24 1 24
1 24 1 25 24
1 24 1 26 24 24
1 24 1 27 24 24 24
1 24 1 28 24 24 24 24
1 24 1 29 24 24 24 24 24
1 24 1 30 24 24 24 24 48
1 24 1 31 24 24 24 24 72
1 24 1 32 24 24 24 24 96

I think I am messing up the addition in the column 9 .
Whats the correct way to do it ?
Any help would be appreciated .

Thanks.
# 6  
Old 04-28-2005
It worked for me:
Code:
$ more foofile
abc xyz lmn 89 lm nk o p 2 p
$ more process.awk
BEGIN { c = 0 }

{ print $0; lastrow = $0 }

END { while ( c < 10 ) {
$0 = lastrow
++$4
$9 = $9 + 24 
print $0
lastrow = $0
++c
}
}
$ awk -f process.awk foofile 
abc xyz lmn 89 lm nk o p 2 p
abc xyz lmn 90 lm nk o p 26 p
abc xyz lmn 91 lm nk o p 50 p
abc xyz lmn 92 lm nk o p 74 p
abc xyz lmn 93 lm nk o p 98 p
abc xyz lmn 94 lm nk o p 122 p
abc xyz lmn 95 lm nk o p 146 p
abc xyz lmn 96 lm nk o p 170 p
abc xyz lmn 97 lm nk o p 194 p
abc xyz lmn 98 lm nk o p 218 p
abc xyz lmn 99 lm nk o p 242 p

I wonder what is the significant difference between our systems.
# 7  
Old 04-28-2005
Ok, I got a similar (but not exactly identical) result by leaving out "{ print $0; lastrow = $0 }". Are you sure there isn't a typo somewhere? Try copying the code directly from your message, because that code works fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print every alternate column in row in a text file

Hi, I have a comma separated file. I would like to print every alternate columns into a new row. Example input file: Name : John, Age : 30, DOB : 30-Oct-2018 Example output: Name,Age,DOB John,30,30-Oct-2018 (3 Replies)
Discussion started by: Lini
3 Replies

2. Shell Programming and Scripting

Column to row and position data in a text file

Hi everyone.. I have a list of values in a file... a, b, c, 1, 2, 3, aaaa, bbbbb, I am interested in converting this column to a row.. "text",aaaa, bbbb a,1 (7 Replies)
Discussion started by: manihi
7 Replies

3. Shell Programming and Scripting

Adding a timestamp every N row in another column

Hi, i have a raw output file like this 167,63.50 167,63.50 168,63.68 166,63.68 168,63.68 I would like to add every each N rows (for example 60) and in a third column , a timestamp using the command date +"%H:%M"how can i do it with one single command ? Thank you !! (5 Replies)
Discussion started by: Board27
5 Replies

4. Shell Programming and Scripting

Script changing row to column

Hi Gurus, I have an I/P file which looks like 100 1 200 1 300 4 100 2 200 3 300 4 100 9 200 8 300 7 I would liek to get O/P as 100 200 300 1 1 4 2 3 4 (8 Replies)
Discussion started by: Indra2011
8 Replies

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

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

7. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

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

9. UNIX for Dummies Questions & Answers

How do you delete cells from a space delimited text file given row and column number?

How do you delete cells from a space delimited text file given row and column number? Letś say the row number is r and the column number is c. Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

10. UNIX for Dummies Questions & Answers

Adding a column with the row number using awk

Is there anyway to use awk to add a first column to my data that automatically goes from 1 to n , where n is the numbers of my rows?:confused: (4 Replies)
Discussion started by: cosmologist
4 Replies
Login or Register to Ask a Question