LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-13-2010, 05:26 AM   #1
nasra2002
LQ Newbie
 
Registered: May 2010
Posts: 4

Rep: Reputation: 0
Unhappy How to get parts of one column and prints all columns ?


Dear All,
I want your help. I used awk and cut command but still I did not get the output I want...

I have 6 columns in the file, and the 6th column is too long. what I want is to print all columns form 1-5 and a part of 6th column.

Example:
aa bb cc dd ee ff33ff

from the above example I want only to get the 1-5 columns and the numbers from 6th column so the output shoud be as:

aa bb cc dd ee 33

please waiting for your help .




Thanks in advance
 
Old 12-13-2010, 05:45 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
What have you tried so far? Maybe it's better to review your code, so that you can learn from your errors. Here are some hints: 1. usually you don't need cut together with awk, since the latter can manage fields as well, 2. to remove all non-digit characters from a string you can use the gsub function in awk, as in:
Code:
gsub(/[^[:digit:]]/,"",$NF)
Hope this helps.
 
Old 12-13-2010, 09:36 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Please refer to the GNU Awk User's Guide.
The initial chapters are short and easy to read, but describe the methodology behind awk scripts.
Understanding those first makes it much, much easier to write and understand awk scripts.

Awk scripts are composed of actions, which are applied to records.
By default, each line is a separate record.
Each action is a snippet of code within braces, optionally preceded by a pattern. If there is a pattern, the action will be applied only to matching records.

To remove all non-digits from the sixth field, you can use e.g.
Code:
gsub(/[^0-9]+/, "", $6)
within an action. On the other hand, you can ignore leading characters, and convert the first numeric sequence to a number, using e.g.
Code:
gsub(/^[^+\-.0-9]+/, "", $6)
$6 = 1.0 * $6
where the second line converts the rest of the field to a number, via awk's automatic type conversion. This way, "aa-1.2e6bbc" will be converted to "-1.2e6" == -1200000.

Also, if you check the Guide, you'll see that gsub() modifies the string in place. This is typical for awk, and may be surprising.

To print the modified record in an action,
Code:
print
suffices; it prints the entire record if no arguments are given.
If you wished to reorder the fields, or print only some of them, you can use e.g.
Code:
print $6, $5, $4, $3, $2, $1
Note that the comma is a parameter separator, and is not printed.
Leaving it out would concatenate the fields without any separators.

To do the above for each input record, put it in an action. The end result will be something like
Code:
gawk '{ gsub(/[^0-9]+/, "", $6); print }'
Cheers,
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 01:33 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting certain parts of a list of columns in BASH mikejreading Linux - Newbie 6 05-07-2009 04:48 AM
Problem with C.Itoh Ci5000 Printer - No Driver - Prints 80 columns tcd777 Linux - Hardware 0 09-05-2008 09:08 PM
how to append columns form a column file in another file adam_blackice Programming 4 09-17-2007 11:33 PM
CUPS Printer Problem - prints, but prints gibberish Sparrowhawk Debian 2 06-19-2006 07:32 PM
cups prints poorly, while lpr from command line prints well spindles Linux - Software 2 04-01-2006 04:02 AM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration