Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed and regular expression

Status
Not open for further replies.

Disruptive

Programmer
Mar 8, 2005
40
0
0
GB
Hi I wish to alternately change some of the data in the following way. For example I wish to switch all species from their current value to their binary opposite. I.e. from 1 -> 0 and from 0 -> 1 when the LIPID number is even. Now the data is on the file such that the LIPID number is before the SPECIES number. Therefore I came up with the following sed script to attempt to do this. However this script does?t work when there is information/data between the LIPID and the SPECIES but seems to work just fine otherwise. Here is the script and the data file:



LIPID 0
SPECIES 0
5435345
LIPID 1
SPECIES 1
435345.4544
LIPID 2
SPECIES 1
22.78
LIPID 3
SPECIES 1
22.78
LIPID 4
SPECIES 1
22.78
LIPID 5
SPECIES 1
LIPID 6
SPECIES 1



sed '
/[02468]$/ {N
s/SPECIES 1/SPECIES 0/
}' Test1.txt > OP.txt;
mv OP.txt Test1.txt;


Thanks.
 
Try...
Code:
awk '$1=="LIPID"{f=$2%2}$1=="SPECIES"&&!f{$2=-1*($2-1)}1' Test1.txt > OP.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top