Unix: Unable to add "\\\ n" even on end of line
I am trying to convert pure column data to tables in tex. I cannot have "\\ n" at each end of the line. Please see the command at the end.
Data
$ . ./bin/addTableTexTags.sh < .data_3
10.31 & 8.50 & 7.40
10.34 & 8.53 & 7.81
8.22 & 8.62 & 7.78
10.16 & 8.53 & 7.44
10.41 & 8.38 & 7.63
10.38 & 8.57 & 8.03
10.13 & 8.66 & 7.41
8.50 & 8.60 & 7.15
10.41 & 8.63 & 7.21
8.53 & 8.53 & 7.12
$ cat .data_3
10.31 8.50 7.40
10.34 8.53 7.81
8.22 8.62 7.78
10.16 8.53 7.44
10.41 8.38 7.63
10.38 8.57 8.03
10.13 8.66 7.41
8.50 8.60 7.15
10.41 8.63 7.21
8.53 8.53 7.12
addTableTexTags.sh
#!/bin/bash
sed -e "s@[[:space:]]@\t\&\t@g" -e "s@[[:space:]]*&*[[:space:]]*\$@\t \\ \\\\n@g"
// Tried Escaping "\\" with "/" here and there
// but cannot get a line ending with "\\ \n".
a source to share
here's an easier way without too many regexes. Just set the Output Field Separator (OFS).
$ awk '{$1=$1}1' OFS=" & " file
10.31 & 8.50 & 7.40
10.34 & 8.53 & 7.81
8.22 & 8.62 & 7.78
10.16 & 8.53 & 7.44
10.41 & 8.38 & 7.63
10.38 & 8.57 & 8.03
10.13 & 8.66 & 7.41
8.50 & 8.60 & 7.15
10.41 & 8.63 & 7.21
8.53 & 8.53 & 7.12
the equivalent of this: awk '{$1=$1}1' OFS=" & " .data | sed -e 's@$@\\\\@g'
which you have.
awk '{$1=$1;$0=$0"\\\\"}1' OFS=" & " file
when you are using awk there is no need to use sed.
a source to share
If you want each line to end with a pair of backslashes, a space, another backslash and "n" (followed by a newline), then:
sed 's/$/\\\\ \\n/'
If this is not what you want, please explain what you need.
Single quotes stop backslashes from messing around. With double quotes, this becomes:
sed "s/\$/\\\\\\\\ \\\\n/"
Yes - 8 gaps in a row! Fun, isn't it?
Adapting this to your script is hard for you. The first term uses " \t
", but it is unclear if it should be a tab or an actual backslash and the letter "t".
sed -e 's@[[:space:]]@\t\&\t@g' \
-e 's@[[:space:]]*&[[:space:]]*$@\\\\ \\n@g'
If you want actual output tabs, I usually print them as tabs (making sure I don't have "vim" set for expandtabs at the time). If I have to be extended " \t
", I usually use Perl because I know its rules, and different shells have different views on the rules. Note that I dropped the "*" right after the "&". in the last match; I think you wanted "&". added by the previous rule.
a source to share