LinuxQuestions.org
Help answer threads with 0 replies.
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-11-2012, 08:54 AM   #1
indianovice
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
Post 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!
 
Old 07-11-2012, 09:21 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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 09:23 AM.
 
1 members found this post helpful.
Old 07-11-2012, 09:27 AM   #3
indianovice
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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?
 
Old 07-11-2012, 09:33 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Code:
awk 'NR>1{$3=" "}$1=$1' OFS="\t" file
 
1 members found this post helpful.
Old 07-11-2012, 10:05 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
You will need to redirect the output to a temporary file, and then replace the original file.
 
1 members found this post helpful.
Old 07-11-2012, 10:09 AM   #6
indianovice
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-11-2012, 01:12 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: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
Old 07-12-2012, 02:32 AM   #8
indianovice
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Smile

Thank you all for the answers!

Could you pls explain why $1=$1 is used?
 
Old 07-12-2012, 04:40 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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.
 
  


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
[SOLVED] Edit pattern under condition using sed or awk anath0r Programming 4 01-04-2012 04:48 PM
Modify AWK command with correct condition for selective print Perseus Programming 2 10-07-2011 11:10 PM
awk: swap fields given a condition sebelk Programming 5 10-08-2010 09:17 PM
[SOLVED] Update a line using AWK with condition NetRock Programming 11 06-21-2010 09:16 PM


All times are GMT -5. The time now is 02:26 AM.

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