0

I have a file which I need to parse, word by word, and make changes to only certain words. My bash script works in everything but retaining newline characters. I have constructed a minimum example as follows:

#!/bin/bash
# contents of myscript.sh

toks=( $* )

for tok in ${toks[*]}; do
    # make changes to $tok if need be
    printf "$tok "
done

I hope to use it as follows:

cat filename.txt | xargs myscript.sh

where filename.txt may look like

word1 word2
word3

The expected output would be the same as input, in this case, but I just get

word1 word2 word3
drjrm3
  • 4,474
  • 10
  • 53
  • 91
  • Quote your variables. Quote them. Also don't use `$*` when you want the positional arguments safely use `"$@"`. – Etan Reisner Jun 12 '15 at 19:51
  • if you've accurately expressed your need, then I take issue with your implementation. You don't really need to go word by word, right? Just use an extended regex with a word boundary match. – erik258 Jun 12 '15 at 19:51
  • I'm a bit confused about `"$@"` - I am using `toks=( "$@" )` now and getting the same result. As far as going word by word, I may not need to do that, but this is how I started it. My intent is to write a command which will take in any number of arguments and change *only* words matching a certain pattern to an integer of a specific nature. I had started it by looking at each word independently and changing it if need be. – drjrm3 Jun 12 '15 at 20:02

2 Answers2

2

Try this:

#!/bin/bash

while read -ra line; do
  for tok in "${line[@]}"; do
    # make changes to $tok if need be
    printf "%s " "$tok"
  done
  echo
done 
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

What about a regex instead of tokenizing?

$ echo -e "word1 word2
word3" | perl -pe 's/\bword[12]\b/wordX/g'
wordX wordX
word3

Granted this needs perl, but there are alternative implementations of PCRE.

erik258
  • 14,701
  • 2
  • 25
  • 31