Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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:
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.
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:
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.
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.
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.
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.
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:
[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.
... 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.