awk print the next line on the current line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print the next line on the current line
# 1  
Old 04-17-2008
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 for example
TESTB2.TXT
---

AA ZZ 12 34 YY 56 78
BB YY 56 78 XX 91 23
CC XX 91 23 VV 45 67
DD VV 45 67


---


I hope I typed that right and I hope the pattern is obvious. Obviously the last line of the file wont have a next line, so its last3 columns are blank. no problem there. or if you know of a similar script just point me to it and I'll hack out the details.
# 2  
Old 04-17-2008
$[/tmp] > more test.txt
AA ZZ 12 34
BB YY 56 78
CC XX 91 23
DD VV 45 67

$[/tmp] > echo "" > test1.txt
$[/tmp] > more test1.txt

$[/tmp] > cat test1.txt test.txt > test2.txt
$[/tmp] > more test2.txt

AA ZZ 12 34
BB YY 56 78
CC XX 91 23
DD VV 45 67

$[/tmp] > cat test.txt | awk '{print $2 " " $3 " " $4}' > test3.txt
$[/tmp] > more test3.txt
ZZ 12 34
YY 56 78
XX 91 23
VV 45 67

$[/tmp] > paste test2.txt test3.txt > test4.txt
$[/tmp] > more test4.txt
ZZ 12 34
AA ZZ 12 34 YY 56 78
BB YY 56 78 XX 91 23
CC XX 91 23 VV 45 67
DD VV 45 67
# 3  
Old 04-17-2008
With awk:

Code:
awk ' 
NR==1{printf("%s", $0);next}
{print FS $2,$3,$4;printf("%s", $0)}
END{if(!(NR%2)){print ""}}
' file

Regards
# 4  
Old 04-17-2008
thx

the paste command will work! thats perfect. I did not even know about the paste command. thank you!
# 5  
Old 04-17-2008
Code:
awk 'END{print __}{_=__}{__=$0;$1=_}_' file

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 6  
Old 05-25-2009
chek it this!

awk '{ print last,$0; last=$0 }' file.txt

Usefull.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tput cup, print on current line?

Heyas I'm thinking about a new approach for my core display, basicly as it should make aligments easier. Issue i'm currently facing, is tput cup capable of printing on the current line? My best achievements were: :) tui $ tput cup - 60;echo " ------ testing" ------ testing... (5 Replies)
Discussion started by: sea
5 Replies

2. Shell Programming and Scripting

Compare between current and next line and print

Dear All I want below to compare two Consecutive line(i.e. current and next line). Based in that i need OP. Below is the IP file in that in i find "M" and if in next line i find "*" then i need both line in single line. If i dont find "*" in next line then i need to put commend "DOWN" . I am... (4 Replies)
Discussion started by: jaydeep_sadaria
4 Replies

3. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

4. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

5. Shell Programming and Scripting

AWK print line no.x to line no.y

I am learning AWK and quite fresh now. My Q: How could I use AWK to print lines from e.g. first to 8th, or 5th to 9th? Thanks!!! (4 Replies)
Discussion started by: cristalp
4 Replies

6. UNIX for Dummies Questions & Answers

Awk to print data from current and previous line

Hi guys, I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr). I am trying to process my data so I can use it to... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

7. UNIX for Dummies Questions & Answers

Using current line in a command in AWK

Hi, Im trying to get current line in the AGREP command I use in AWK. My script looks like this: list.txt car bus checklist.txt cer buss cat list.txt | awk -v mycmd="$(agrep -2 -i $0 checklist.txt)" '{print $mycmd}' It doesnt work. How can I get the current line in the $0... (6 Replies)
Discussion started by: m4rty
6 Replies

8. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

9. Shell Programming and Scripting

Print previous, current and next line using sed

Hi, how can i print the previous, current and next line using sed? current line is the matching line. The following prints all lines containing 'Failure' and also the immediate next line cat $file | sed -n -e '/Failure/{N;p;}' Now, i also want to print the previous line too. Thanks,... (8 Replies)
Discussion started by: ysrinu
8 Replies

10. Shell Programming and Scripting

print any required line by its line no using awk and its NR variable

how to print any required line by its line no using awk and its NR variable for eg: ------------ 121343 adfdafd 21213sds dafadfe432 adf.adf%adf --------------- requied o/p if give num=3 it print: 21213sds -------------------------------------- (2 Replies)
Discussion started by: RahulJoshi
2 Replies
Login or Register to Ask a Question