The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
cat file1 read line-per-line then grep -A 15 lines down in fileb irongeekio Shell Programming and Scripting 6 01-28-2009 06:30 AM
Joining lines in reverse. append line 1 to line 2. dwalley Shell Programming and Scripting 7 08-04-2008 07:11 AM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 10:19 AM
to make 2 lines into 1 line using awk cdfd123 Shell Programming and Scripting 2 10-03-2007 07:44 PM
Appending line/lines with sed nir_s Shell Programming and Scripting 28 07-24-2005 02:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Awk 2 lines to 1 line

Input File:

Quote:
Input:
host1 server1
database1 5
host2 server2
database2 5
host3 server3
database3 5
Required Result:
Quote:
host1 server1 database1 5
host2 server2 database2 5
host3 server3 database3 5
I have tried the following code:
Code:
nawk '{for(i=1; i<=NR; i+2) {print NR,$0; getline ;print \n $0; NR=NR+2}}' temp
But doesnt gives the right result.
Help is appreciated
  #2 (permalink)  
Old 04-22-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
paste -d' ' - - < temp
OR
nawk 'ORS=(FNR%2)?FS:RS' temp
  #3 (permalink)  
Old 04-22-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by vgersh99 View Post
Code:
paste -d' ' - - < temp
OR
nawk 'ORS=(FNR%2)?FS:RS' temp
Thank you pro

Code:
paste -d' ' - - < temp
nawk 'ORS=(FNR%2)?FS:RS' temp
Would appreciate if you can explain this.
  #4 (permalink)  
Old 04-22-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by zenith View Post
Thank you pro

Code:
paste -d' ' - - < temp
nawk 'ORS=(FNR%2)?FS:RS' temp
Would appreciate if you can explain this.
You can read the man pages for 'paste' to give you a high-level of what it does - the rest should be easy.
awk:
Code:
(FNR%2) - get a 'modulo' of the current file RecordNumber (FNR) over 2 - every OTHER line.
If the mod is NON-zero, return 'FS' (FieldSeparator)
If the mod is zero, return RecordSeparator (RS)
ORS= - assign the returned value to the OutputRecordSeparator (ORS)

In other words...
If we're dealing with the ODD record/line numbers (1,3,5,7 etc), print the line and FS (separate the next line)
If we're dealing with the EVEN record/line numbers (2.4.6.8 etc), print the line and the ORS (which is by default is newLine).
  #5 (permalink)  
Old 04-22-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by vgersh99 View Post
You can read the man pages for 'paste' to give you a high-level of what it does - the rest should be easy.
awk:
Code:
(FNR%2) - get a 'modulo' of the current file RecordNumber (FNR) over 2 - every OTHER line.
If the mod is NON-zero, return 'FS' (FieldSeparator)
If the mod is zero, return RecordSeparator (RS)
ORS= - assign the returned value to the OutputRecordSeparator (ORS)
 
In other words...
If we're dealing with the ODD record/line numbers (1,3,5,7 etc), print the line and FS (separate the next line)
If we're dealing with the EVEN record/line numbers (2.4.6.8 etc), print the line and the ORS (which is by default is newLine).
Thank you Sir,

Minor change; need output comma seperated.

I have tried this but has syntax error.

Code:
nawk '{ORS=(FNR%2)?FS:RS}; OFS=,' temp
Appreciate your help
  #6 (permalink)  
Old 04-22-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
nawk -F, 'ORS=(FNR%2)?FS:RS' temp
  #7 (permalink)  
Old 04-22-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Awk with weird spaces (output from sybase server)

Quote:
Originally Posted by vgersh99 View Post
Code:
nawk -F, 'ORS=(FNR%2)?FS:RS' temp
When i pasted input file on website. i have some weird spaces but when i pasted i think website removed it. hence i am attaching it.


Requried Result
host1,server1,database1,5
host2,server2,database2,5
host3,server3,database3,5

Result i am getting is also attached

Appreciate help
Attached Files
File Type: txt inputfile.txt (375 Bytes, 20 views)
File Type: txt output_i_am_getting.txt (363 Bytes, 18 views)
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0