# need help with a quick and dirty bash script

**URL:** <https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925>\
**Category:** Factual Questions\
**Created:** [July 30, 2013, 6:08pm UTC](https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925 "2013-07-30T18:08:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drewtwo99](https://avatars.discourse-cdn.com/v4/letter/d/d07c76/32.png) [@drewtwo99](https://boards.straightdope.com/u/drewtwo99)\
**Post date:** [July 30, 2013, 6:08pm UTC](https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925/1 "2013-07-30T18:08:50Z")

</div>

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

```auto

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?

---

<div class="post-metadata">

**Author:** ![septimus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/septimus/32/410_2.png) [@septimus](https://boards.straightdope.com/u/septimus)\
**Post date:** [July 30, 2013, 7:19pm UTC](https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925/2 "2013-07-30T19:19:12Z")

</div>

nm. my script doesn’t work ☹

---

<div class="post-metadata">

**Author:** ![drewtwo99](https://avatars.discourse-cdn.com/v4/letter/d/d07c76/32.png) [@drewtwo99](https://boards.straightdope.com/u/drewtwo99)\
**Post date:** [July 30, 2013, 7:38pm UTC](https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925/3 "2013-07-30T19:38:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Digital\_is\_the\_new\_Analog](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@Digital\_is\_the\_new\_Analog](https://boards.straightdope.com/u/Digital_is_the_new_Analog)\
**Post date:** [July 30, 2013, 7:57pm UTC](https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925/4 "2013-07-30T19:57:34Z")

</div>

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…

```auto

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

```

---

<div class="post-metadata">

**Author:** ![drewtwo99](https://avatars.discourse-cdn.com/v4/letter/d/d07c76/32.png) [@drewtwo99](https://boards.straightdope.com/u/drewtwo99)\
**Post date:** [July 30, 2013, 8:05pm UTC](https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925/5 "2013-07-30T20:05:52Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![arseNal](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@arseNal](https://boards.straightdope.com/u/arseNal)\
**Post date:** [July 30, 2013, 8:12pm UTC](https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925/6 "2013-07-30T20:12:30Z")

</div>

NF, not $NF 😉

---

<div class="post-metadata">

**Author:** ![drewtwo99](https://avatars.discourse-cdn.com/v4/letter/d/d07c76/32.png) [@drewtwo99](https://boards.straightdope.com/u/drewtwo99)\
**Post date:** [July 30, 2013, 8:20pm UTC](https://boards.straightdope.com/t/need-help-with-a-quick-and-dirty-bash-script/664925/7 "2013-07-30T20:20:35Z")

</div>

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