Reputation: 359
I want to read text file word by word. Problem: there are some words containing "/*". Such a word causes script to return files in root directory. I tried:
for word in $(< file)
do
printf "[%s]\n" "$word"
done
And several other combinations with echo/cat/etc... For this file:
/* example file
I get following output:
[/bin]
[/cygdrive]
[/Cygwin.bat]
...
[example]
[file]
Should be easy but it's driving me nuts.
Upvotes: 1
Views: 4145
Reputation: 46853
How about this one?
while read -a a; do printf '[%s]\n' "${a[@]}"; done < file
Output:
[/*]
[example]
[file]
Upvotes: 0
Reputation: 85865
How about this solution, the double quotes around $(< file)
stop *
from being expanded and sed
is used format the output as required:
for word in "$(< file)"
do
echo "$word" | sed -E 's/(\S*)(\s)/[\1]\2\n/g'
done
Output:
[/*]
[example]
[file]
Upvotes: 1
Reputation: 5722
You need to turn off pathname expansion globbing. Run a new shell with bash -f
and try again. See http://wiki.bash-hackers.org/syntax/expansion/globs or dive into the manpage with man bash
, maybe do man bash | col -b >bash.txt
.
Upvotes: 1
Reputation: 51
this may help;
# skip blank lines and comment lines begining with hash (#)
cat $CONFIG_FILE | while read LINE
do
first_char=`echo $LINE | cut -c1-1`
if [ "${first_char}" = "#" ]
then
echo "Skip line with first_char= >>${first_char}<<"
else
:
echo "process line: $LINE" ;
fi
done
Another way is to use a case statement
Upvotes: 0