Hi,
I want to search a pattern in a text file and remove another pattern in that file.
my text file look like this
0.000000 1.970000 F 303 -
1.970000 2.080000 VH VH +
2.080000 2.250000 VH VH +
2.250000 2.330000 VH L -
2.330000 2.360000 F H +
2.360000 2.410000 L VL -
2.410000 2.450000 F 312 +
I want to search the line containing 'F' and remove signs of that line(+ or -)
Yoda
July 25, 2013, 12:40am
2
awk '$3=="F"{gsub(/[+-]/,X)}1' file
rveri
July 25, 2013, 1:21am
3
sreejith,
check it out..
$ awk '$3~/F/&&(($NF~/-/)||($NF~/\+/)){$NF="";print} ' file
0.000000 1.970000 F 303
2.330000 2.360000 F H
2.410000 2.450000 F 312
Jotne
July 25, 2013, 3:43am
4
@reveri
He does not say that he like to remove all other line, just remove -/+ of lines with F.
Also he does not specify what part of the line does contain the F, so it does not need to be at $3
awk '/F/{NF-=1}1' file
If its at $3 and F may be in combination with other letters, use:
awk '$3~"F"{NF-=1}1' file
rveri
July 25, 2013, 4:13pm
5
Jotne,
Thanks , understood the reqiurement now.
I tried your codes, looks like not working for me: not removing signs ( + - ) .os:hp-ux
$ awk '/F/{NF-=1}1' file
0.000000 1.970000 F 303 -
1.970000 2.080000 VH VH +
2.080000 2.250000 VH VH +
2.250000 2.330000 VH L -
2.330000 2.360000 F H +
2.360000 2.410000 L VL -
2.410000 2.450000 F 312 +
$ awk '$3~"F"{NF-=1}1' file
0.000000 1.970000 F 303 -
1.970000 2.080000 VH VH +
2.080000 2.250000 VH VH +
2.250000 2.330000 VH L -
2.330000 2.360000 F H +
2.360000 2.410000 L VL -
2.410000 2.450000 F 312 +
Jotne
July 26, 2013, 12:27am
6
Try this:
awk '/F/{NF=NF-1}1' file
or Yodas suggestion
Yoda
July 26, 2013, 12:31am
7
In HP-UX, you have to change NF
and also force record reconstruction to get desired o/p:
awk '/F/{NF-=1;$1=$1}1' file