LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-04-2012, 07:26 AM   #1
ksvinaykumar
LQ Newbie
 
Registered: Apr 2012
Posts: 9

Rep: Reputation: Disabled
How to manipulate a range of columns in a file using awk


I have a file with 10 columns (example)

If i want to do the same manipulation for the columns from 3-10 using awk, how i could do it. I know it for one column:

substr($1,1,5)

I just want to know is there any command that i dont have to type for each column but in one single substr command for a range of columns.

Thanks and regards, Raje
 
Old 04-04-2012, 08:04 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Nope. The substr function manages one field/string at a time. You may want a loop:
Code:
for ( i = 3; i <= 10; i++ )
  do something with substr($i,1,5)
Hope this helps.
 
1 members found this post helpful.
Old 04-04-2012, 09:15 AM   #3
ksvinaykumar
LQ Newbie
 
Registered: Apr 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
I am sorry,

what is the exact syntax:

cat file | awk '(for ( i = 3; i <= 10; i++ )
{print substr($i,1,5)}'

This is not working this way.

pls send the correct syntax
 
Old 04-04-2012, 09:34 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,483

Rep: Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864
cat is not required and if no expression is to be evaluated then all actions must be inside the curly braces:
Code:
awk '{for ( i = 3; i <= 10; i++ )print substr($i,1,5)}' file
 
1 members found this post helpful.
Old 04-05-2012, 05:23 AM   #5
ksvinaykumar
LQ Newbie
 
Registered: Apr 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Hi,

Thank u. that worked.
awk '{for ( i = 3; i <= 10; i++ )print substr($i,1,5)}' file

But it prints each result in the next row, i mean with a newline. How can i print the result in the tabwise format.

Thanks in advance.
 
Old 04-05-2012, 08:02 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can either change the Output Record Separator (check the internal variable ORS):
Code:
awk 'BEGIN{ORS="\t"}{for ( i = 3; i <= 10; i++ ) print substr($i,1,5)}' file
or use the printf statement to explicitly set the format of the output:
Code:
awk '{for ( i = 3; i <= 10; i++ ) printf "%s\t", substr($i,1,5)}' file
To avoid the extra TAB at the end (or if you have multiple lines to print out):
Code:
awk '{for ( i = 3; i <= 9; i++ ) printf "%s\t", substr($i,1,5); print substr($i,1,5)}' file
Notice that after exiting the for loop the variable i has the value 10, so that you can print the last desired field. Otherwise use $10 instead of $i in the last print statement. Hope this helps.
 
1 members found this post helpful.
Old 04-05-2012, 02:42 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
 
Old 04-10-2012, 05:44 AM   #8
ksvinaykumar
LQ Newbie
 
Registered: Apr 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
All the above 3 codes prints the same output. Only the first record started to print in 1st column. From the second record on, it starts from the second column. One extra tab is printed in the first column of the second record.

Thanks and regards, Raje.
 
Old 04-10-2012, 09:11 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,715

Rep: Reputation: 4136Reputation: 4136Reputation: 4136Reputation: 4136Reputation: 4136Reputation: 4136Reputation: 4136Reputation: 4136Reputation: 4136Reputation: 4136Reputation: 4136
Quote:
Originally Posted by ksvinaykumar View Post
All the above 3 codes prints the same output. Only the first record started to print in 1st column. From the second record on, it starts from the second column. One extra tab is printed in the first column of the second record.

Thanks and regards, Raje.
Well, since you've been handed the right commands, why don't you start doing things on your own, and figure out how to make it work like you want it?? Seriously, that's the best way to actually LEARN, rather than being spoon-fed an answer.
 
  


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
BASH or AWK: extract columns in multiple files and combine to a single file cristalp Programming 2 03-15-2012 11:55 AM
awk - rearrange column data in csv file to match columns wolverene13 Programming 9 12-21-2011 04:55 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM
awk: how to read dan manipulate data in multi files from a file mauludi Linux - Newbie 4 05-27-2011 01:52 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM


All times are GMT -5. The time now is 09:36 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration