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
sorting fields of a line Digby UNIX for Dummies Questions & Answers 6 03-12-2008 09:29 AM
displaying mutliple fields on command line Trellot UNIX for Dummies Questions & Answers 3 11-02-2007 07:37 AM
Grep Line with Matching Fields hemangjani UNIX for Advanced & Expert Users 13 08-10-2007 11:46 AM
how to include field separator if there are blank fields? ReV Shell Programming and Scripting 19 07-13-2005 04:50 AM
AWK create loop for fields tonet Shell Programming and Scripting 1 07-07-2005 09:50 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 03-23-2007
prkfriryce prkfriryce is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 44
need for loop to include fields as one line

another question that could possibly be answered with awk...

Code:
#cat filename
ab   cd   ef:ghi:jk  lm:nop  qrs
ab   cd   ef:ghi:jk  lm:nop  qrs
ab   cd   ef:ghi:jk  lm:nop  qrs
ab   cd   ef:ghi:jk  lm:nop  qrs

# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk  
lm:nop
qrs
ef:ghi:jk  
lm:nop
qrs
ef:ghi:jk  
lm:nop
qrs
ef:ghi:jk  
lm:nop
qrs
Is there awk syntax to include all fields on one line so the for loop reads 4 lines and not 12?

IE


Code:
# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk  lm:nop  qrs
ef:ghi:jk  lm:nop  qrs
ef:ghi:jk  lm:nop  qrs
ef:ghi:jk  lm:nop  qrs

Last edited by prkfriryce; 03-23-2007 at 11:01 AM..
  #2 (permalink)  
Old 03-23-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
Is there awk syntax to include all fields on one line so the for loop reads 4 lines and not 12?
awk reads the line based on the record seperator,

just read and prints the line
Code:
awk '{ print }' file

Sorry if I have not understood correctly!

Could you please post the expected output ?
  #3 (permalink)  
Old 03-23-2007
prkfriryce prkfriryce is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 44
Expected/desired output:

# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs

the 'for loop' separates each awk line into three variables, so instead of 4 lines, there are 12:

actual output:

# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
  #4 (permalink)  
Old 03-23-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
awk -F " " '{ print $3, $4, $5 }' filename
or

Code:
awk -F " " '{ for ( i=3; i<=NF; i++ ) { printf "%s ", $i } printf "\n" }' filename

Last edited by matrixmadhan; 03-23-2007 at 11:19 AM..
  #5 (permalink)  
Old 03-23-2007
prkfriryce prkfriryce is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 44
Quote:
Originally Posted by matrixmadhan
Code:
awk -F " " '{ print $3, $4, $5 }' filename
or

Code:
awk -F " " '{ for ( i=3; i<=NF; i++ ) { printf "%s ", $i } printf "\n" }' filename
both yield the same result:

ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs

Regardless of the awk output, the 'for loop' logic still interprets each awk oupt line as a three variables. I would liek to set each awk output line as a string to be able to parse in a later function
  #6 (permalink)  
Old 03-23-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
you don't use the for loop anymore.
just execute either those 2 awk's from provided by matrixmadhan will do.
  #7 (permalink)  
Old 03-23-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by prkfriryce
Expected/desired output:

# for x in `awk 'sub($1" +"$2" +","") ' filename`; do echo $x; done
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs

the 'for loop' separates each awk line into three variables, so instead of 4 lines, there are 12:

actual output:

# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs

Code:
awk 'sub($1" +"$2" +","",$0) ' filename |
  while IFS= read -r x
  do
     printf "%s\n" "$x"
  done
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 10:48 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