Correctly interpret newline characters

81 views Asked by At

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
2

There are 2 answers

1
Cyrus On BEST ANSWER

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 
0
erik258 On

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.