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

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

Join Date: Nov 2008
Posts: 14
awk help

I have a file

a b
c d
e f
g h

and I want output

(a-d) (b-c)
(c-f) (d-e)
(e-h) (f-g)

I have tried using the awk getline command but I always seem to skip a line.

Thanks
  #2 (permalink)  
Old 05-26-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
nawk '{ $0="(" $0 ")"; gsub(" ", "-") ; ORS=(FNR%2)?FS:RS}1' myFile


Last edited by vgersh99; 05-26-2009 at 01:13 PM.. Reason: added '-'
  #3 (permalink)  
Old 05-26-2009
BubbaJoe's Avatar
BubbaJoe BubbaJoe is offline
Registered User
  
 

Join Date: Oct 2008
Location: St Louis
Posts: 153
Can you post what you have so far? I think you are using a getline plus a next which will skip a line if you are not careful. Will help to see what you have
  #4 (permalink)  
Old 05-26-2009
garethsays garethsays is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 14
so far....

awk '{a=$1;b=$2;getline;c=$2;print a,c}' input_file

which gives me an output from my original input file of

a d
e h

and I was expecting
a d
c f
e h

Any ideas???

Thanks
  #5 (permalink)  
Old 05-26-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
ok, I've misread the requirements - hopefully this is what you need:

Code:
nawk '!a {a=$1;b=$2;next} {printf("(%s-%s)%c(%s-%s)\n", a,$2,OFS,b,$1);a=$1;b=$2}' myFile

  #6 (permalink)  
Old 05-26-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514

Code:
 awk '
NR==1{p1=$1;p2=$2;getline;printf("(%s-%s)%s(%s-%s)\n",p1,$2,OFS,p2,$1);q1=$1;q2=$2;next}
{printf("(%s-%s)%s(%s-%s)\n",q1,$2,OFS,q2,$1);q1=$1;q2=$2}' file


-Devaraj Takhellambam
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 01:14 PM.


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