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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract some common fields from 1 file that are presnt in another file unxusr123 UNIX for Dummies Questions & Answers 1 06-11-2008 03:33 AM
AWK - printing certain fields when field order changes in data file eric4 Shell Programming and Scripting 3 04-15-2008 07:48 PM
Sorting Files / Combing Fields / Printing ccfc1986 Shell Programming and Scripting 12 12-03-2007 02:38 PM
printing select fields in awk maverix Shell Programming and Scripting 5 06-22-2007 10:25 AM
How to add fields in a file bjorb Shell Programming and Scripting 2 10-13-2005 09:11 AM

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 10-06-2008
sais sais is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 36
need help with awk in printing the fields in a file

hi all

i get a file from a server and i do not know how many fields that file will contain.
i need to cut from the second column of the file to the last, irrespective of how many fields are there.

is there any way to make the awk command dynamic for fetching from the second to the last field in the file, some thing similar to cut commmand where giving -f2- will give fields from second to the end..
the reason why i cannot use cut is that a few of the fields have tab as a delimiter and a few have space, as awk treats both as same and fetches the field irrespective of the delimiter being tab or space, i want it to be an awk command...

any help.
  #2 (permalink)  
Old 10-06-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

Code:
awk '{sub( /^[^ \t]+[ \t]+/,""); print}' FILENAME
  #3 (permalink)  
Old 10-07-2008
sais sais is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 36
thank you very much ...... one more help ..can you please explain the command ?
  #4 (permalink)  
Old 10-07-2008
sais sais is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 36
i am getting the following error. can you please help?

> awk '{sub( /^[^ \t]+[ \t]+/,""); print}' 21CP
+ awk {sub( /^[^ \t]+[ \t]+/,""); print} 21CP
awk: syntax error near line 1
awk: illegal statement near line 1
  #5 (permalink)  
Old 10-07-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,322
Quote:
Originally Posted by sais View Post
i am getting the following error. can you please help?

> awk '{sub( /^[^ \t]+[ \t]+/,""); print}' 21CP
+ awk {sub( /^[^ \t]+[ \t]+/,""); print} 21CP
awk: syntax error near line 1
awk: illegal statement near line 1
Use nawk or /usr/xpg4/bin/awk on Solaris.

Quote:
Originally Posted by sais View Post
can you please explain the command ?
Code:
sub(/^[^ \t]+[ \t]+/,"")
The sub command replaces the first field and the tabs or spaces after the first field with nothing ("").

^[^ \t]+
-> select one or more non spaces/tabs from the begin of the line

[ \t]+ -> select one or more spaces/tabs after the characters of the begin of the line

Regards
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:44 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