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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-11-2012, 07:54 AM
|
#1
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Rep: 
|
awk with if condition
Hi,
I have a file which is like
A B C D
1 2 2 3
1 4 6 3
3 3 3 3
(The delimiter is tab)
Now I want to replace the 3rd column with space but I cant use anything like cat file.txt |awk '{$3=" "};1' because I want the 'C' to be still there. ie the output should be have a blank 3rd column with C as its heading(only the numbers should be removed)
Please help!
|
|
|
07-11-2012, 08:21 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
just use the if statement, as you said:
awk '{if (NR==1) {print} else { print $1 " " $2 " " $4 }}'
Last edited by acid_kewpie; 07-11-2012 at 08:23 AM.
|
|
1 members found this post helpful.
|
07-11-2012, 08:27 AM
|
#3
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep: 
|
Hi,
Thanks ! It worked. But I want the change to be reflected in the original file, and not just print the output. How can I do that?
Also,the output was like:
A B C D
1 2 3
1 4 3
3 3 3
Why is it so?
|
|
|
07-11-2012, 08:33 AM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
Code:
awk 'NR>1{$3=" "}$1=$1' OFS="\t" file
|
|
1 members found this post helpful.
|
07-11-2012, 09:05 AM
|
#5
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You will need to redirect the output to a temporary file, and then replace the original file.
|
|
1 members found this post helpful.
|
07-11-2012, 09:09 AM
|
#6
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep: 
|
awk 'NR>1{$3=" "}$1=$1' OFS="\t" aaa.txt
A B C D
1 2 3
1 4 3
3 3 3
cat aaa.txt
A B C D
1 2 2 3
1 4 6 3
3 3 3 3
But I want aaa.txt to be changed to
A B C D
1 2 3
1 4 3
3 3 3
|
|
|
07-11-2012, 12:12 PM
|
#7
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Please use *** [code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.
The majority of command line tools, including awk, only print to stdout and do not alter the input file. And you can't just redirect the output back into the same input file, as the redirection operator will overwrite it before it even gets read. For the most part you need to output to a temporary file or variable first, and then replace the original.
Code:
awk 'NR>1{$3=" "}$1=$1' OFS="\t" aaa.txt >tempfile
mv -f tempfile aaa.txt
|
|
1 members found this post helpful.
|
07-12-2012, 01:32 AM
|
#8
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep: 
|
Thank you all for the answers!
Could you pls explain why $1=$1 is used?
|
|
|
07-12-2012, 03:40 AM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
It could be any valid field number, 1 is just easy, and it will restructure the line with OFS between each field. Change it to 1 (one) and see the difference.
|
|
|
All times are GMT -5. The time now is 05:07 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|