The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get count on different fields along the raws in a file Nayanajith Shell Programming and Scripting 7 02-09-2008 07:51 AM
combining fields in two text fields shocker Shell Programming and Scripting 3 01-16-2008 08:27 AM
How count number of fields in a record sureshg_sampat Shell Programming and Scripting 5 01-07-2008 03:30 AM
help me to count no of fields in a file babu@shell Shell Programming and Scripting 7 10-30-2006 10:36 PM
How to count the record count in an EBCDIC file. oracle8 UNIX for Dummies Questions & Answers 1 07-26-2006 04:22 PM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 12-06-2001
sskb
Guest
 

Posts: n/a
count fields

Is there a way to count the no. of fields (columns) in a file?
Actually I need to cut some fields starting from the middle to the end. How can I specify to cut till last field?

thanks in advance
Forum Sponsor
  #2  
Old 12-07-2001
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
You can use awk to do this. It's automatically stored in "NF":

ll |tail -1 |awk '{print NF}'
9

I bet you could do the whole thing in awk, but I'm not too good with awk, so maybe someone else can help with an example...
  #3  
Old 12-07-2001
Jimbo
Guest
 

Posts: n/a
file1:
aaa bbb ccc ddd
aaa bbb ccc ddd
aaa bbb ccc ddd
aaa bbb ccc ddd

If you are dealing with a fixed-column format, you can pull the columns you want with:

cut -c1-3,18-21 file1 > file2

cut command also works with columns (fields), but if the field delimiter is space, for example, multiple spaces in a row are seen as multiple field delimiters, thus making the field numbers very inconsistent. On the other hand, by default, awk considers any amount of white space as a single field delimiter. Following awk command will output fields 1 and 4 with one space between:

awk '{print $1,$4}' file1 > file2

For both solutions, file2 looks like:

aaa ddd
aaa ddd
aaa ddd
aaa ddd

awk can apply logic as to which lines to process, or process certain lines differently from others based on content, etc.
  #4  
Old 12-07-2001
flim flam flamma jamma
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
im thinking something like
Code:
(host):/home0/  cat me
1c 2 3b 4aE2
(host):/home0/ sed 's/[^ ]*$//' me
1c 2 3b
(host):/home0/

I fixed it.

Last edited by Optimus_P; 12-07-2001 at 10:28 AM.
  #5  
Old 12-07-2001
Jimbo
Guest
 

Posts: n/a
As clarification, the sed solution, as coded, will drop the last two characters of each line that ends with two characters:
first character must be space, letter or digit
second character can be anything
otherwise will leave the line unchanged.
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0