awk to print first row with forth column and last row with fifth column in each file | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

awk to print first row with forth column and last row with fifth column in each file

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-09-2013
cdfd123 cdfd123 is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 20 July 2013, 1:08 PM EDT
Posts: 133
Thanks: 21
Thanked 0 Times in 0 Posts
awk to print first row with forth column and last row with fifth column in each file

file with this content
Quote:
PPP11 OFS HIGH 1200 1401 + 0 KNOW
PPP22 OFS HIGH 1433 1588 - 2 KNOW
Quote:
u want to print first row with fourth column and second row with fifth column; the code is below

Code:
awk 'NR==1 {print $4} && NR==2 {print $5}' file

The error is shown with syntax error; what can be done

Quote:
And another question is that if there is to print first row with fourth column and last row with fifth column; in each file; how the solution with awk will be?
Sponsored Links
    #2  
Old 02-09-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 July 2014, 5:38 AM EDT
Location: San Jose, CA, USA
Posts: 4,117
Thanks: 160
Thanked 1,406 Times in 1,193 Posts
Quote:
Originally Posted by cdfd123 View Post
file with this content


Code:
PPP11 OFS HIGH 1200 1401 + 0 KNOW
PPP22 OFS HIGH 1433 1588 - 2 KNOW

u want to print first row with fourth column and second row with fifth column; the code is below

Code:
awk 'NR==1 {print $4} && NR==2 {print $5}' file

The error is shown with syntax error; what can be done

And another question is that if there is to print first row with fourth column and last row with fifth column; in each file; how the solution with awk will be?
First, please use CODE tags (not QUOTE tags) to show contents of files.

Second, we seem to have a language barrier. I do not understand what you want. Are you asking for a script that uses awk to read two lines from a file and write just column 4 from the 1st row and column 5 from the 2nd row? If this is what you want, you can get it using something like:

Code:
awk 'NR==1 {print $4}
NR==2 {print $5}' file

Are you asking for a script that reads a file and prints the 1st line that contains 4 or more columns and the 2nd line that contains 5 or more columns?

Please show us the output you want from the sample input you provide (in CODE tags) so we more clearly understand what you're trying to do.
The Following User Says Thank You to Don Cragun For This Useful Post:
cdfd123 (02-09-2013)
Sponsored Links
    #3  
Old 02-09-2013
user8 user8 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 24 April 2013, 10:34 AM EDT
Posts: 36
Thanks: 0
Thanked 19 Times in 14 Posts
About how to print: "last row with fifth column; in each file":

Code:
END{ print $5 }

or in case you are using gawk 4.x:

Code:
ENDFILE{ print $5 }

The Following 2 Users Say Thank You to user8 For This Useful Post:
admax (02-09-2013), cdfd123 (02-09-2013)
    #4  
Old 02-09-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 July 2014, 3:37 PM EDT
Location: Amsterdam
Posts: 9,278
Thanks: 260
Thanked 2,302 Times in 2,065 Posts
@user8: not every awk retains the fields in the END section. To get around this:

Code:
{s=$5} END{ print s }

The Following User Says Thank You to Scrutinizer For This Useful Post:
cdfd123 (02-11-2013)
Sponsored Links
    #5  
Old 02-09-2013
user8 user8 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 24 April 2013, 10:34 AM EDT
Posts: 36
Thanks: 0
Thanked 19 Times in 14 Posts
Quote:
Originally Posted by Scrutinizer View Post
@user8: not every awk retains the fields in the END section.
You are right. I should have mentioned this.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print unique names in each row of a specific column using awk quincyjones Shell Programming and Scripting 4 12-12-2012 12:23 PM
Subtracting each row from the first row in a single column file using awk ks_reddy Shell Programming and Scripting 11 04-04-2012 03:31 PM
AWK Script - Print a column - within a Row Range panapty Shell Programming and Scripting 6 09-28-2011 08:54 AM
Moving data from a specified column/row to another column/row jl487 Shell Programming and Scripting 2 06-05-2010 12:07 AM
Changing the column for a row in a text file and adding another row aYankeeFan Shell Programming and Scripting 9 05-02-2005 09:42 PM



All times are GMT -4. The time now is 05:42 AM.