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

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.

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:

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

Try this...

only problem is with OFS..

$ 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
1 Like

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.

If you know the spacings then just add spacings also...
Use

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
1 Like

This depends on the No. of <TAB>s you specified and on the fact that $4 will always contain either logical value:

awk 'BEGIN{OFS=FS="\t"}
     {$4=$4=="TRUE"?"FALSE":"TRUE"}
     1
    '  file
1 Like

Thanks Pamu and Rudic, both code worked.

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

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

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

try

awk 'BEGIN{OFS=FS="\t"}
     /abc|def|ghi/ {$4=$4=="TRUE"?"FALSE":"TRUE"}
     1
    '  file