LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 07-28-2011, 07:09 AM   #1
Rode
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Rep: Reputation: 0
Problem in using AWK to modify column


Hello everyone,
I want to use awk to modify file like this
origin:
A[]A[][]A[]A
modified:
A[]a[][]A[]A

but when I use
awk '{$2="a"; print $0}' inputfile
the output is
A[]a[]A[]A
where the [] means space. This is not what i want. I guess that is because the OFS is one space by default, but i really don't know how to solve this. Any help is highly appreciated.

Last edited by Rode; 07-28-2011 at 07:11 AM.
 
Old 07-28-2011, 07:16 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

As you probably have seen by now, multiple spaces and tabs are reduced to one. Please put [code] ...[/code] tags around your code to preserve multiple tabs/spaces.

Although we don't know what the input looks like, would this help:
Code:
awk 'BEGIN {OFS="\t"} { print $1, tolower($2), $3, $4 }'
The bold part is the important part. It sets the OFS (a tab in this case).

Hope this helps.

EDIT: Just noticed you edited while I was replying.

Have a look at this:
Code:
awk '{ print $1, " " tolower($2), $3, $4 }'
This way you place an extra space in the output.

Last edited by druuna; 07-28-2011 at 07:19 AM.
 
Old 07-28-2011, 08:47 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

if you are not limited to awk I can supply a more general solution:
Code:
$ cat file
A A  A A
B B  B B
Hello THErE  World !!
$ sed -r 's/([^[:blank:]]+[[:blank:]]+)([^[:blank:]]+)/\1\L\2/' file
A a  A A
B b  B B
Hello there  World !!
The bold part will turn any second word into lower-case.
 
1 members found this post helpful.
Old 07-28-2011, 08:03 PM   #4
Rode
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Hi,

As you probably have seen by now, multiple spaces and tabs are reduced to one. Please put [code] ...[/code] tags around your code to preserve multiple tabs/spaces.

Although we don't know what the input looks like, would this help:
Code:
awk 'BEGIN {OFS="\t"} { print $1, tolower($2), $3, $4 }'
The bold part is the important part. It sets the OFS (a tab in this case).

Hope this helps.

EDIT: Just noticed you edited while I was replying.

Have a look at this:
Code:
awk '{ print $1, " " tolower($2), $3, $4 }'
This way you place an extra space in the output.
Hi, thank you for your quick reply. But I don't think this will help, since this is a quite simple case, the actual problem is long lines with multiple spaces as field seperator.
 
Old 07-28-2011, 08:14 PM   #5
Rode
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Hi,

if you are not limited to awk I can supply a more general solution:
Code:
$ cat file
A A  A A
B B  B B
Hello THErE  World !!
$ sed -r 's/([^[:blank:]]+[[:blank:]]+)([^[:blank:]]+)/\1\L\2/' file
A a  A A
B b  B B
Hello there  World !!
The bold part will turn any second word into lower-case.
Hi, I once turned to sed but found it not such easy to use as awk when modifying columns. I suppose that because I am new to sed since you post. Would you please explain the code a little bit?

Actually, as is said in the reply to druuna, this is a simple case, any code that explicitly refers to every column is not realistic in my case on hand.
 
Old 07-28-2011, 08:22 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,962
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
So what's the actual requirement? Will columns w/ content always
only have one none-whitespace character? Are they fixed width?


Cheers,
Tink
 
Old 07-28-2011, 08:30 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by Rode View Post
Actually, as is said in the reply to druuna, this is a simple case, any code that explicitly refers to every column is not realistic in my case on hand.
Well, maybe you should provide a more representative example then before we develop solutions that just won't do for the actual data.

What the sed above does is it turns the second column to lowercase. It does not matter how many spaces there are nor how many columns the file contains. If you want to also specify another column than the second, e.g. by a parameter, this can probably be done, too. But as I said, you will have to provide more representative sample data.
 
Old 07-28-2011, 08:57 PM   #8
Rode
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Well, maybe you should provide a more representative example then before we develop solutions that just won't do for the actual data.

What the sed above does is it turns the second column to lowercase. It does not matter how many spaces there are nor how many columns the file contains. If you want to also specify another column than the second, e.g. by a parameter, this can probably be done, too. But as I said, you will have to provide more representative sample data.
I am sorry for the ambiguities it causes, what I meant is to give a simplified case which avoids trivial information. Whatever, since you ask, I show part of a line of the file to be modified here.
Code:
STRT..=  A MSGL.=  0 EXPAN.= S FCD..=  Y FUNC..= SBA
I want to change the first A into B and the third S into M, seperately or at once. I really appreciate that you can explain the regular expressions in your code more so I can do it myself and don't have to bother people for repeated help next time.

Last edited by Rode; 07-28-2011 at 08:59 PM.
 
Old 07-28-2011, 10:30 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
So just to confirm ... whatever this data is for it requires that the format of the spacing be unaltered?

As for the regular expression in crts' code, [[:blank:]] is a character list and the round brackets save back references
to whatever is matched into an increasing number count starting at 1, so:
Code:
([^[:blank:]]+[[:blank:]]+) = \1
([^[:blank:]]+) = \2
And obviously \L to set the following information to lower
 
1 members found this post helpful.
Old 07-29-2011, 07:22 AM   #10
Rode
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
[QUOTE=grail;4428151]So just to confirm ... whatever this data is for it requires that the format of the spacing be unaltered?

You have got my idea. Generally, the requirement can be conluded like that, I want to modify the certain field of a line with all other format untouched. Actually, what I am dealing with is the formatted input file of a fortran program.
 
Old 07-29-2011, 07:47 AM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by Rode View Post
... I show part of a line of the file to be modified here.
Code:
STRT..=  A MSGL.=  0 EXPAN.= S FCD..=  Y FUNC..= SBA
I want to change the first A into B and the third S into M, seperately or at once. I really appreciate that you can explain the regular expressions in your code more so I can do it myself and don't have to bother people for repeated help next time.
So it has nothing to do with changing to lowercase at all. Somehow I have a feeling that if we provide you with another solution it will still not fit your needs. Do all other lines look like the one you provided? Will it always be the second and tenth column that needs editing? Will it always be exactly two columns that need editing? Will those columns always follow the columns 'STRT..=' and 'FUNC..='? Will the replacement always be the same?
For your new example:
Code:
sed -r 's/(STRT..=[[:blank:]]*)A(.*FUNC..=[[:blank:]]*)S/\1B\2M/' file
It is possible to use variables for the search/replacement patterns and make it more general and dynamic.

Last edited by crts; 07-29-2011 at 07:49 AM.
 
1 members found this post helpful.
Old 07-30-2011, 09:13 PM   #12
Rode
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
So it has nothing to do with changing to lowercase at all. Somehow I have a feeling that if we provide you with another solution it will still not fit your needs. Do all other lines look like the one you provided? Will it always be the second and tenth column that needs editing? Will it always be exactly two columns that need editing? Will those columns always follow the columns 'STRT..=' and 'FUNC..='? Will the replacement always be the same?
For your new example:
Code:
sed -r 's/(STRT..=[[:blank:]]*)A(.*FUNC..=[[:blank:]]*)S/\1B\2M/' file
It is possible to use variables for the search/replacement patterns and make it more general and dynamic.
Thank you very much. This code is quite enough. I learn more about sed right now. What I'm editing is a formatted input file of a fortran program, the column is not limited some certain ones. Anyway, I guess I can work out other solutions now.

Thank you all.

Btw, This is a quite great forum, I like it.
 
  


Reply

Tags
awk, column


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
[SOLVED] Adding a column with awk Tauro Linux - Newbie 2 07-27-2011 07:41 AM
[SOLVED] Not able to extract last column from awk vinaytp Linux - Newbie 4 05-20-2011 04:27 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
How to modify a some particular elements of a column? mauludi Linux - Newbie 6 02-22-2010 02:12 AM
how to modify a column type in postgrest Bheki Linux - General 3 03-01-2004 03:37 AM


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

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