What is tcsh equivalent of bash $'\t' - tcsh

I'm trying to use column -t -s $'\t' in tcsh (which works in bash) but I can't find a way to make it work
echo "selected version\tnew version\nbbb\tccc\n" | column -t -s $'\t'
Illegal variable name
So what's the equivalent in tcsh?

tcsh doesn't have a mechanism for including non-printing characters in string literals, other than including them directly.
In bash, you can do this (changing echo to printf because its behavior is more consistent):
$ printf "selected version\tnew version\nbbb\tccc\n" | column -t -s $'\t'
selected version new version
bbb ccc
In tcsh, you can replace the $'\t' with a literal tab character, entered by typing Ctrl-VTab -- but when you read the code it's indistinguishable from a sequence of spaces.
Here's one solution, using the printf command to convert \t to a tab character:
% printf "selected version\tnew version\nbbb\tccc\n" | column -t -s "`printf '\t'`"
selected version new version
bbb ccc
You can also store the tab character in a variable; you can use either double quotes or the :t suffix to retain the literal value when you use it:
% set tab = "`printf '\t'`"
% printf "selected version\tnew version\nbbb\tccc\n" | column -t -s "$tab"
selected version new version
bbb ccc
% printf "selected version\tnew version\nbbb\tccc\n" | column -t -s $tab:q
selected version new version
bbb ccc

in general, you just ust "\t" for a tab. However, column seems to intepret this literally as a backslash and a letter t. To get around this, you can explicitly enter a tab character by pressing cntl-v and then tab (surrounded by quotes).
Note however, in the case of your specific example, you do not need the -s argument at all, since column separates by whitespace by default.

Related

Combining Grep and Paste command in bash

very sorry to ask a stupid question but I'm getting crazy with this thing.
So, I'm in bash and I have some files:
ls
a.bed
b.bed
c.bed
all I want to do is create a variable that have all the 3 of them separated with a comma, this is the output I search for:
a.bed, b.bed, c.bed
What I'm using for now (but have spaces instead of commas is):
beds=$(ls|grep .bed)
which have
a.bed b.bed c.bed
Thank you so much
I would use printf and its -v option, followed by a use of parameter expansion.
$ printf -v beds '%s, ' *.bed
$ beds=${beds%, }
The first line produces a.bed, b.bed, c.bed, . The second line trims the trailing , .
If you only need a single-character separator, an alternative is to use an array with IFS:
$ beds=$(a=(*.bed); IFS=,; echo "${a[*]}")
You can do it with ls 'x' and 'm' options alone:
beds=$(ls -xm *.bed)
echo $beds
a.bed, b.bed, c.bed
Here's one that is a bit wacky:
beds=$( tr \ , <<< $(ls *.bed))
In the example above, we get rid of the newlines in the ls output simply by executing it with $(). Then we use the resulting string as input to tr which replaces all spaces with commas.
My favorite is using the built in -xm parameters in ls, but this particular answer can apply to other executables that do not provide the rich set of output formats that ls does.
Overkill for this specific case but just as an FYI you could do:
$ bedsArr=( *.bed )
$ bedsStr=$( printf '%s, ' "${bedsArr[#]:0:$((${#bedsArr[#]} - 1))}"; printf "%s\n" "${bedsArr[#]: -1:1}" )
$ printf '%s\n' "$bedsStr"
a.bed, b.bed, c.bed

xargs adds whitespace after echo statement when outputting on to new line

using xargs and echo to output result of samtools to new line in output.txt file
samtools view $SAMPLE\.bam | cut -f3 | uniq -c | sort -n | \
xargs -r0 -n1 echo -e "Summarise mapping...\n" >> ../output.txt
This adds the result on a new line after the echo but also adds a space before the result on the first new line, how can i stop this?
It's not xargs which is adding the space. It's the echo command:
The echo utility arguments shall be separated by single <space> characters and a <newline> character shall follow the last argument. (Text from Posix standard; emphasis added.)
If you want more control, use printf:
...
xargs -r0 -n1 printf "Summarise mapping...\n%s\n" >> ../output.txt
Unlike printf does not automatically add a newline at the end, so it needs to be included in the format.
Note that printf automatically interprets escape sequence like \n in the format string (but not in interpolated arguments). As an additional bonus for using printf, you could leave out the -n1 option since printf automatically repeats the format until all arguments are consumed.

bash: setting tab field separator with variable

Some bash tools such as sort, join, cut (all coreutils?) require field separator to be passed in a somewhat peculiar way for tabs: sort -t $'\t' .... There are many questions here that address this behavior.
My problem is I am trying to pass the field separator as a variable, such as:
SEP="\t"
sort -t $SEP ...
With normal characters, that works, but not with tabs. I tried a few variations, but none of them work. How can this be done?
Declare it using ANSI-C quoting:
sep=$'\t'
And call it as "$sep", quotes are important to preserve the literal meaning:
sort -t "$sep" file.txt
Example:
$ cat file.txt
foo bar
spam egg
abc def
$ sep=$'\t'
$ sort -t $sep file.txt
sort: multi-character tab ‘file.txt’
$ sort -t "$sep" file.txt
abc def
foo bar
spam egg
Also note that, to get rid of the ambiguity with the environment variables i have used lowercase characters for the variable name, unless you have a very good reason you should do so too.
Use the keys [CONTROL]+[V] before pressing [TAB] to introduce the tab char.
echo "a b c" |cut -d" " -f2
b
Be careful if you copy paste the code, as the tabs may be lost, as they are lost in fact, in this post :-)

count number of tab characters in linux

I want to count the numbers of hard tab characters in my documents in unix shell.
How can I do it?
I tried something like
grep -c \t foo
but it gives counts of t in file foo.
Use tr to discard everything except tabs, and then count:
< input-file tr -dc \\t | wc -c
Bash uses a $'...' notation for specifying special characters:
grep -c $'\t' foo
Use a perl regex (-P option) to grep tab characters.
So, to count the number of tab characters in a file:
grep -o -P '\t' foo | wc -l
You can insert a literal TAB character between the quotes with Ctrl+V+TAB.
In general you can insert any character at all by prefixing it with Ctrl+V; even control characters such as Enter or Ctrl+C that the shell would otherwise interpret.
You can use awk in a tricky way: use tab as the record separator, then the number of tab characters is the total number of records minus 1:
ntabs=$(awk 'BEGIN {RS="\t"} END {print NR-1}' foo)
My first thought was to use sed to strip out all non-tab characters, then use wc to count the number of characters left.
< foo.txt sed 's/[^\t]//g' | wc -c
However, this also counts newlines, which sed won't touch because it is line-based. So, let's use tr to translate all the newlines into spaces, so it is one line for sed.
< foo.txt tr '\n' ' ' | sed 's/[^\t]//g' | wc -c
Depending on your shell and implementation of sed, you may have to use a literal tab instead of \t, however, with Bash and GNU sed, the above works.

sort multiple tabs but ignoring spaces

I have a data file like this (\t represents tabs):
short line\t \t \t \t \t 3
very long line with lots of text\t\t 2
How could I sort it by the second column using sort? In other words I want to set the delimiter to be multiple tabs, but not spaces.
It seems that the field separator for sort must be a single character, so this command:
sort -t $'\t' -k2 file
will not handle multiple tabs as a single separator: it will sort the empty 2nd field for both lines.
This command will successfully find the the second field, but it modifies the text:
tr -s '\t' < file | sort -t $'\t' -k2
Note that tr interprets the 2-character string "\t" as a tab character, while sed -t does not. Just a foible of how different commands are implemented.
sort -k2 -t' ' test.txt
worked out of the box for me. Enter the TAB inside ' ' as C-vTab in bash
Setting the field delimiter to something else is accomplished with the -t parameter. But passing a tab character can be tricky, so the solution may look like:
sort -t "$(echo -e '\t')" -k 2 file.txt

Resources