Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Change the text in 2nd column in file

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-03-2012
sdosanjh sdosanjh is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 31 March 2016, 3:00 PM EDT
Posts: 143
Thanks: 50
Thanked 1 Time in 1 Post
Change the text in 2nd column in file

Legends,

I have following entries in a file.
I want to toggle the entry of 2nd column. TRUE/FALSE
i tried using
Code:
perl -p -i -e

but the problem is, if instead of "tab" space after 1st column, there is a character or multi character space then this won't work.


Code:
perl -p -i -e "s/$i.xxx.out.remoteOn           TRUE/$i.xxx.out.remoteOn           FALSE/g" /tmp/file_a

Entries in the below file is like:


Code:
abc.xxx.out.remoteOn           TRUE                               write
def.xxx.out.remoteOn           TRUE                               write
ghi.xxx.out.remoteOn           TRUE                               write
jkl.xxx.out.remoteOn           TRUE                               write

Pls help, if i can use any other editor "sed", "awk" etc.(but i am not too familiar with these)

Regards
Sponsored Links
    #2  
Old Unix and Linux 12-03-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 8 August 2016, 12:07 PM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
Try this...

only problem is with OFS..


Code:
$ cat file
abc.xxx.out.remoteOn           TRUE                               write
def.xxx.out.remoteOn           TRUE                               write
ghi.xxx.out.remoteOn           FALSE                               write
jkl.xxx.out.remoteOn           TRUE                               write

$ awk '{if($0 ~ /FALSE/){$2="TRUE"}else if($0 ~ /TRUE/){$2="FALSE"}}1' OFS="\t" file
abc.xxx.out.remoteOn    FALSE   write
def.xxx.out.remoteOn    FALSE   write
ghi.xxx.out.remoteOn    TRUE    write
jkl.xxx.out.remoteOn    FALSE   write

The Following User Says Thank You to pamu For This Useful Post:
sdosanjh (12-03-2012)
Sponsored Links
    #3  
Old Unix and Linux 12-03-2012
sdosanjh sdosanjh is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 31 March 2016, 3:00 PM EDT
Posts: 143
Thanks: 50
Thanked 1 Time in 1 Post
Quote:
Originally Posted by pamu View Post
Try this...

only problem is with OFS..


Code:
$ cat file
abc.xxx.out.remoteOn           TRUE                               write
def.xxx.out.remoteOn           TRUE                               write
ghi.xxx.out.remoteOn           FALSE                               write
jkl.xxx.out.remoteOn           TRUE                               write

$ awk '{if($0 ~ /FALSE/){$2="TRUE"}else if($0 ~ /TRUE/){$2="FALSE"}}1' OFS="\t" file
abc.xxx.out.remoteOn    FALSE   write
def.xxx.out.remoteOn    FALSE   write
ghi.xxx.out.remoteOn    TRUE    write
jkl.xxx.out.remoteOn    FALSE   write


Thanks Pamu, but it has changed the file format. after change, now only one tab space. while there are around 3 tab spaces before 3rd col and 2 tabs before 2nd col.
    #4  
Old Unix and Linux 12-03-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 8 August 2016, 12:07 PM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by sdosanjh View Post
after change, now only one tab space. while there are around 3 tab spaces before 3rd col and 2 tabs before 2nd col.
If you know the spacings then just add spacings also...
Use

Code:
awk '{if($0~/FALSE/){print $1"\t\tTRUE\t\t\t"$3}else if($0~/TRUE/){print $1"\t\tFALSE\t\t\t"$3}else{print}}' file

The Following User Says Thank You to pamu For This Useful Post:
sdosanjh (12-03-2012)
Sponsored Links
    #5  
Old Unix and Linux 12-03-2012
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 9 December 2016, 4:34 AM EST
Location: Aachen, Germany
Posts: 9,944
Thanks: 201
Thanked 2,960 Times in 2,760 Posts
This depends on the No. of <TAB>s you specified and on the fact that $4 will always contain either logical value:
Code:
awk 'BEGIN{OFS=FS="\t"}
     {$4=$4=="TRUE"?"FALSE":"TRUE"}
     1
    '  file

The Following User Says Thank You to RudiC For This Useful Post:
sdosanjh (12-03-2012)
Sponsored Links
    #6  
Old Unix and Linux 12-03-2012
sdosanjh sdosanjh is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 31 March 2016, 3:00 PM EDT
Posts: 143
Thanks: 50
Thanked 1 Time in 1 Post
Thanks Pamu and Rudic, both code worked.

---------- Post updated at 03:23 AM ---------- Previous update was at 03:07 AM ----------

Quote:
Originally Posted by pamu View Post
If you know the spacings then just add spacings also...
Use

Code:
awk '{if($0~/FALSE/){print $1"\t\tTRUE\t\t\t"$3}else if($0~/TRUE/){print $1"\t\tFALSE\t\t\t"$3}else{print}}' file

It worked, but another problem arose is:

It is a huge file and i do not want to toggle entire file entries.
I tried below for loop with your code, but that didnt work out


Code:
For i in abc def jhi
do
awk '{if($0~/FALSE/){print $1"\t\tTRUE\t\t\t"$3}else if($0~/TRUE/){print $1"\t\tFALSE\t\t\t"$3}else{print}}' file
done

Sponsored Links
    #7  
Old Unix and Linux 12-03-2012
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 9 December 2016, 4:34 AM EST
Location: Aachen, Germany
Posts: 9,944
Thanks: 201
Thanked 2,960 Times in 2,760 Posts
try
Code:
awk 'BEGIN{OFS=FS="\t"}
     /abc|def|ghi/ {$4=$4=="TRUE"?"FALSE":"TRUE"}
     1
    '  file

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Change file content 4 column to one Column using script thepurple Shell Programming and Scripting 5 03-03-2012 06:06 AM
Help i want to change the data of one file and apend it into 2nd file. iamnoone Shell Programming and Scripting 7 12-28-2011 03:55 AM
comparing column of two different files and print the column from in order of 2nd file CAch Shell Programming and Scripting 14 08-24-2011 02:45 PM
Replacing a specific column of a text file with another column evelibertine UNIX for Dummies Questions & Answers 3 06-28-2011 05:38 PM
Writing out 2nd column into one file from multiple files subhap Shell Programming and Scripting 5 10-23-2009 09:13 PM



All times are GMT -4. The time now is 05:35 AM.