![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |