LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to get parts of one column and prints all columns ? (https://www.linuxquestions.org/questions/linux-software-2/how-to-get-parts-of-one-column-and-prints-all-columns-849894/)

nasra2002 12-13-2010 06:26 AM

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

colucix 12-13-2010 06:45 AM

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.

Nominal Animal 12-13-2010 10:36 PM

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


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