need help with a quick and dirty bash script

I’m trying to knock out a really quick and dirty bash script which will perform a very simple task, but I’m not familiar enough with sed or awk syntax to manage it.

Here’s what I need the script to do:

script will search for all files of the type .sps in the folder it’s located and perform the following:

Read in a line and modify it as follows



SAMPLE LINE BEFORE EDIT

216992841            22481A2       8.0                     303429.4 2883339.0       201 530 4

SAMPLE LINE AFTER EDIT

216992841            22481A2       8.0                   0 303429.4 2883339.0    0.0201 530 4


Go through each line in each .sps file and perform the above modification (inserting the 0’s and 0.0’s in the appropriate places)

Ideally, I would like it to just edit the existing .sps files and save the “new” one with the same file name.

Please note that the first 0 will be in its own column, but the 0.0 needs to be prepended to that other value near the end. Also note that ALL lines follow this same basic pattern as given in the sample. The only exception is the last few values “201 530 4” might be a single value like “199345592.” The spaces are essentially just 0’s in the “201 530 4” sample given.

This is something I can do in vi in a few seconds, of course, but I have to do it so often it would be nice to just have a script do it for me.

So, all you awk/sed masters, can you help me out?

nm. my script doesn’t work :frowning:

Ok well I’ve got a good start but I have a bit of a problem. Here’s what I have (ignoring the for loop for now, and just focusing on one text file):

awk ‘{print $1,$2,$3," 0 “$4, $5,” 0.0"$6,$7,$8}’ text.sps

This almost works perfectly, except for when there aren’t 7 or 8 tokens or whatever you want to call them. It screws up a bit when the variables only go up to $6 or $7 which happens on many lines. For the lines that do go up to $8, it works nicely.

How can I fix it?

It’s been a long time since I did awk or sed. And I’m sick today, so I’m not really focused.

A quick and dirty way is to use the NF variable…


awk '{
  if (NF == 7) {
    print relevant print statements
  }
  else if (NF == 6) {
    print other relevant stuff
  }
}'

Unfortunately $NF does not store the number of tokens read in a line, but the actual value of the last token. So that won’t work for me :frowning:

NF, not $NF :wink:

Oh ok. I’ve got it working a slightly different way anyhow. Thank you for the tips!