0

i was looking over the web for this but came up with nothing, i found this Q&A over stackoverflow Echo tab characters in bash script but it didn't help me.

i have an index of about 9000 lines, each line has one or two letters in Chinese.

any way i would like to rewrite each line so it will be the same only with space in the end. for example:

in the original index : AB

in the new index : AB

this is the script i wrote but it didn't do the work it just put empty line after each item in the index, with no space, what am i missing???

this is the code tough:

 rm new_index.txt
 for line in $(cat index.txt)
   do
     echo "$line \t"  >> new_index.txt
   done
Community
  • 1
  • 1
0x90
  • 39,472
  • 36
  • 165
  • 245

3 Answers3

1

Try doing it with sed

sed -e 's/$/ /' index.txt > new_index.txt

You could also do it inplace i.e. update the same file

sed -e 's/$/ /' -i index.txt

With backup

sed -e 's/$/ /' -i.bak index.txt
amit_g
  • 30,880
  • 8
  • 61
  • 118
1

Not bashy, but it's what sed is for!

sed 's/$/ \t/' index.txt > new_index.txt

As for bash, you should have a '-e' for the echo to make it understand that \t is a tab character instead of a backslash and a t.

rm new_index.txt
for line in $(cat index.txt)
  do
    echo -e "$line \t" >> new_index.txt
  done
Falsenames
  • 131
  • 2
0

Try this

while IFS= read -r line ; do
    printf '%s \n' "$line"
done < index.txt > new_index.txt

This will preserve each line exactly but will append a space before each newline.

Note: the printf is not special, you could also say

echo "$line "

and get the same effect. The important thing is how you read the file.

sorpigal
  • 25,504
  • 8
  • 57
  • 75