Using awk to reformat a line of data based on conditional value in one column

59 views Asked by At

I have a set of data formatted like this:

r 0.000 0 0 1.000 0.010

The numbers in the columns 2 and 5 vary because it is positional data (column 2) and travel-time data (column 5). However every time column 5 has a 1.000 or -1.000, it's a specific line denoting a new set of information, so I need to use awk to check for those values and edit the corresponding row.

I have tried doing various forms of

awk '{if ($5=-1.000) ($1="s" && $5=0)}' inputfile > outputfile

including only trying to change the first column from an r to an s to start with. The result should be

s 0.000 0 0 0 0.010

for every row that column 5 has a -1.000, but so far my results have just been completely blank output files. Sometimes my output replaces the entire 5th column with -1's as well, which wipes all the important data away.

EDIT: Also to clarify the data is hundreds of rows long. Here is the first 10 lines of the data to show what it consists of right now:

r 0.000 0 0 1.000 0.010 
r 0.050 0 0 0.044 0.010 
r 0.100 0 0 0.075 0.010 
r 0.150 0 0 0.108 0.010 
r 0.200 0 0 0.117 0.010 
r 0.250 0 0 0.160 0.010 
r 0.300 0 0 0.197 0.010 
r 0.350 0 0 0.214 0.010 
r 0.400 0 0 0.259 0.010 
r 0.450 0 0 0.268 0.010 

EDIT: Thank you for the assistance, I did eventually figure out what I was doing wrong. I didn't want to delete the question however.

2

There are 2 answers

0
markp-fuso On

Assumptions:

  • apply edits if the 5th column is 1.000 or -1.000
  • print all (input) lines to stdout

Sample input:

$ cat inputfile
r 0.000 0 0 1.000 0.010
r 0.000 0 0 -1.000 0.010
r 0.050 0 0 0.044 0.010
r 0.100 0 0 0.075 0.010
r 0.150 0 0 0.108 0.010
r 0.200 0 0 0.117 0.010
r 0.250 0 0 0.160 0.010
r 0.300 0 0 0.197 0.010
r 0.350 0 0 0.214 0.010
r 0.400 0 0 0.259 0.010
r 0.450 0 0 0.268 0.010

Tweaking OP's current code:

awk '
{ if ($5 == -1.000 || $5 == 1.000) {          # test 5th column
     $1="s"                                   # modify 1st and
     $5=0                                     # 5th columns
  }
}
1                                             # print current line
' inputfile

# or as a one-liner:

awk '{ if ($5 == -1.000 || $5 == 1.000) { $1="s"; $5=0 } } 1' inputfile

These both generate:

s 0.000 0 0 0 0.010
s 0.000 0 0 0 0.010
r 0.050 0 0 0.044 0.010
r 0.100 0 0 0.075 0.010
r 0.150 0 0 0.108 0.010
r 0.200 0 0 0.117 0.010
r 0.250 0 0 0.160 0.010
r 0.300 0 0 0.197 0.010
r 0.350 0 0 0.214 0.010
r 0.400 0 0 0.259 0.010
r 0.450 0 0 0.268 0.010
0
karakfa On
$ awk '$5==1 || $5==-1 {$1="s"; $5=0}1' input > output

is more idiomatic.