Reputation: 4718
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
Upvotes: 0
Views: 41
Reputation: 88756
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
Upvotes: 2
Reputation: 16304
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.
Upvotes: 0