Copy Column Value Of Next Line Into Current Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy Column Value Of Next Line Into Current Line
# 1  
Old 10-04-2016
Copy Column Value Of Next Line Into Current Line

Hello All,

I am looking for help to achieve the following:

Here is the data set

Code:
1757890237|42|55570025|1468796400|0  
1757890237|32|55570025|1471474800|0
1757890237|54|55570025|1474153200|1476745200
1757890237|34|55570026|1468796400|0
1757890237|44|55570026|1471474800|0
1757890237|56|55570026|1474153200|1476745200
1757890237|36|55570027|1468796400|0
1757890237|46|55570027|1471474800|0
1757890237|58|55570027|1474153200|1476745200
1762391149|30|55570025|1468710000|0
1762391149|38|55570025|1468796400|0
1762391149|32|55570026|1468710000|0
1762391149|40|55570026|1468796400|0
1762391149|34|55570027|1468710000|0
1762391149|42|55570027|1468796400|0

In above data we have two distinct values in column 1 which are C1 and C2. We have three distinct values in column 3 which are K1, K2 and K3. I will grep the file three times (K1, K2 and K3) for each C (C1 and C2). After grep I will replace C5 of line n by C4 of line n+1 until C5 of line n+1 is not zero.

using while loop this can be done but it will be slow. Is there any awk command which will be quicker?

Please help.

Thanks
Angsuman


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-04-2016 at 08:54 AM.. Reason: Changed HTML to CODE tags.
# 2  
Old 10-04-2016
You lost me. WHAT are you doing "After grep"? Describe and post output.

And, please post your "while loop" so we can improve it or infer from it what needs to be done.
# 3  
Old 10-04-2016
Following should be the output:

Code:
1757890237|42|55570025|1468796400|1471474800
1757890237|32|55570025|1471474800|1474153200
1757890237|54|55570025|1474153200|1476745200
1757890237|34|55570026|1468796400|1471474800
1757890237|44|55570026|1471474800|1474153200
1757890237|56|55570026|1474153200|1476745200
1757890237|36|55570027|1468796400|1471474800
1757890237|46|55570027|1471474800|1474153200
1757890237|58|55570027|1474153200|1476745200
1762391149|30|55570025|1468710000|1468796400
1762391149|38|55570025|1468796400|1468796400+30 days converted into seconds
1762391149|32|55570026|1468710000|1468796400
1762391149|40|55570026|1468796400|1468796400+30 days converted into seconds
1762391149|34|55570027|1468710000|1468796400
1762391149|42|55570027|1468796400|1468796400+30 days converted into seconds



Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 10-04-2016 at 09:10 AM.. Reason: Changed ICODE to CODE tags.
# 4  
Old 10-04-2016
Yes.

And the rest?
# 5  
Old 10-04-2016
Sorry. What do you mean by rest?
# 6  
Old 10-04-2016
The other questions in post#2.
# 7  
Old 10-11-2016
Hello RudiC,

Sorry if my question is not clear. Let me rephrase as below:

Replace value of column 5 of a line with value of column 4 of previous line provided value of column 5 is zero and value of column 1 and column 3 of both lines match.

Replace value of column 5 of a line with value of column 4 of same line + constant if value of column 5 is zero and value of column 1 and column 3 of a line does not match with its previous line.

Thanks
Angshuman

---------- Post updated at 11:58 PM ---------- Previous update was at 10:54 PM ----------

I am also trying to explain with the example:

Code:
1757890237|54|55570025|1474153200|1476745200
1757890237|42|55570025|1468796400|0  
1757890237|32|55570025|1471474800|0
1757890237|56|55570026|1474153200|1476745200
1757890237|34|55570026|1468796400|0
1757890237|44|55570026|1471474800|0
1757890237|58|55570027|1474153200|1476745200
1757890237|36|55570027|1468796400|0
1757890237|46|55570027|1471474800|0
1762391149|30|55570025|1468710000|0
1762391149|38|55570025|1468796400|0
1762391149|32|55570026|1468710000|0
1762391149|40|55570026|1468796400|0
1762391149|34|55570027|1468710000|0
1762391149|42|55570027|1468796400|0

In above example,
  1. column 5 of line 1 is not zero. Hence no action
  2. column 5 of line 2 is zero, value of column 1 and column 3 of line 2 matches with column 1 and column 3 of line 1, replace column 5 with value of column 4 of line 1
  3. In line 10, value of column 1 and column 3 does not match with value of column 1 and column 3 of column 9, also value of column 5 in line 10 is 0, replace value of column 5 of line 10 with a constant value.

Thanks
Angsuman
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append Next line with current Line bassed on condition

Hi, I have an XML file and I am tring to extract some data form it, after lot of data cleaning process, I ended up with an issue, and need your urgent support. my current input data in below format: <Node>xxxxxx <Node>yyyyy</Node> <Node>zzzzzz <Node>12345</node> I need... (9 Replies)
Discussion started by: rramkrishnas
9 Replies

2. UNIX for Dummies Questions & Answers

Printing the next line side by to the current line

Help, I have a text file which looks like disco 5674536 3456 jambo disco 453678 4578 jambo I would like to have an output which looks like below disco 3456 disco 4578 (4 Replies)
Discussion started by: Indra2011
4 Replies

3. Shell Programming and Scripting

Find in the current line and next line.

Hi, I have lines that have pattern like this. 1) productFamilyGroupIndex < Local.ProductFamilyGroup.capacity)) and 2) if (local.getProductFamilyGroup().size() >= Local.ProductFamilyGroup. capacity) So, If I need to find the pattern grep '\(< \|>= \)Local.*capacity'... (4 Replies)
Discussion started by: srikanthradix
4 Replies

4. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

5. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

6. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

7. Shell Programming and Scripting

Vi Editor - How to paste the line concatenated with current line

I generally use yy to copy a line and then p to paste the line at end of current line. But is there a way to paste the copied line in concatenation with the current line with out going to next line. (3 Replies)
Discussion started by: paragkalra
3 Replies

8. Shell Programming and Scripting

Read and copy xml line by line and preserve tab?

I'm trying to read an xml file and copy it line by line to another file and want to preserve the tabs. What i'm trying to do is if I get to a certain line in the xml, I'm going to check to see if the next line is specifically what I want. If it's not, then I want to insert a single line of text... (4 Replies)
Discussion started by: DeuceLee
4 Replies

9. Shell Programming and Scripting

awk print the next line on the current line

Ok I have a file with hundreds of lines, four columns, space delimited, TESTB.TXT for example TESTB.TXT --- AA ZZ 12 34 BB YY 56 78 CC XX 91 23 DD VV 45 67 --- I want a new file that has 7 columns, the first four are identical, and the next 3 are the last three of the next line...so... (5 Replies)
Discussion started by: ajp7701
5 Replies

10. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies
Login or Register to Ask a Question