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
pamu
December 3, 2012, 4:58am
2
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
pamu:
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
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.
pamu
December 3, 2012, 5:20am
4
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
RudiC
December 3, 2012, 5:30am
5
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
RudiC
December 3, 2012, 7:05am
7
try
awk 'BEGIN{OFS=FS="\t"}
/abc|def|ghi/ {$4=$4=="TRUE"?"FALSE":"TRUE"}
1
' file