The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
print line whatever line i want in a file... there any way kittusri9 Shell Programming and Scripting 1 05-15-2008 09:37 AM
How do i print this output all on the same line? rcon1 UNIX for Dummies Questions & Answers 1 02-05-2008 06:52 AM
use variable for sed print line atchleykl UNIX for Dummies Questions & Answers 2 04-24-2007 09:44 AM
awk print line Chiefos UNIX for Dummies Questions & Answers 7 06-15-2006 02:28 AM
Print file line by line handak9 Shell Programming and Scripting 2 10-20-2005 05:44 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-17-2008
Supporter
 

Join Date: Dec 2007
Posts: 42
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-17-2008
Registered User
 

Join Date: Sep 2004
Posts: 9
$[/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
Reply With Quote
  #3 (permalink)  
Old 04-17-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,665
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
Reply With Quote
  #4 (permalink)  
Old 04-17-2008
Supporter
 

Join Date: Dec 2007
Posts: 42
thx

the paste command will work! thats perfect. I did not even know about the paste command. thank you!
Reply With Quote
  #5 (permalink)  
Old 04-17-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
Code:
awk 'END{print __}{_=__}{__=$0;$1=_}_' file
Use nawk or /usr/xpg4/bin/awk on Solaris.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0