0
printf "%s\t" Nt n{1..1600} >file_a
printf "%s\t" R cha 2 987 > file_aa
awk '{print}' file_a file_aa > newfile

to creat a file in which the header looks like

Nt /t n1 /t n2 /t n3 ....

the problem is that when lookin at how this prints it turns out that in the last n it is

\t   n   1   5   9   8  \t   n   1   5   9   9  \t   n   1   6
0   0  \t  \n

so there is something after nt 1600 - how can I lose this last \t in awk? Or somewhere?

I was talking about the last \t in that \t n 1 5 9 8 \t n 1 5 9 9 \t n 1 6 0 0 \t \n here - I need the newline character - both answers did not solve that.

I do need

printf "%s\t" Nt n{1..1600} >file_a
printf "%s\t" R cha 2 987 > file_aa

There has to be a newline.

3
  • What is the 0002120 and 0002130 at the beginning of the lines? – A.B. Jun 29 '15 at 11:13
  • 1
    @A.B. output of od -c. od adds those to help track byte counts. – muru Jun 29 '15 at 11:45
  • 2
    Or alternatively, you could visit the answers to your previous questions and notice that all of the answers at askubuntu.com/questions/640854 gave you ways to generate this stuff without the trailing tab character in the first place. Several people on several of your questions have now asked you to look at the answers. – JdeBP Jun 30 '15 at 5:15
2

Assuming you are talking about \n, if you don't want that newline to appear, use cat to join those files instead of awk:

cat file_a file_aa > newfile

The difference:

$ awk 1  <(printf "%d\t" {1..2} ) <(printf "%d\t" {1..2} )
1   2   
1   2   
$ cat <(printf "%d\t" {1..2} ) <(printf "%d\t" {1..2} )
1   2   1   2   

Since you want to delete the trailing \t, try:

sed -i 's/\t*$//' file_a
awk '{print}' file_a file_aa
  • \t*$ is any number of tabs at the end of the line $.

The effect:

$ awk 1 <(printf "%d\t" {1..2} | sed 's/\t*$//') <(printf "%d\t" {1..2} ) | od -c              
0000000   1  \t   2  \n   1  \t   2  \t  \n
0000011
2
  • no I was taking about the last /t in that swe \t n 1 5 9 8 \t n 1 5 9 9 \t n 1 6 0 0 \t \n here - i need the newline character – user3069326 Jun 29 '15 at 11:21
  • @user3069326 see update. – muru Jun 29 '15 at 11:45
1

If you need a newline, just add one

{ printf "%s\t" Nt n{1..1600}; echo; } > file_a
{ printf "%s\t" R cha 2 987;   echo; } > file_aa
sed 's/\t$//'  file_a file_aa          > newfile

Or, without the temp files:

{ 
    printf "%s\t" Nt n{1..1600}; echo
    printf "%s\t" R cha 2 987;   echo
} | sed 's/\t$//' > newfile

Or, without having to explicitly remove the trailing tabs:

(                    # in a subshell, so IFS is not modified in the current shell
    IFS=$'\t'
    head1=( Nt n{1..1600} ); echo "${head1[*]}"
    head2=( R cha 2 987 );   echo "${head2[*]}"
) > newfile
1

The tab is there because you are adding it:

printf "%s\t" 

That will print every string you give it followed by a tab. So, the last string will have a tab after it as well. To avoid that, you could either remove it after creating it using sed or similar tools (see @muru'sanswer), or you can use a different tool to create it. For example, perl:

perl -le 'print "NT\tn", join("\tn",(1..1600)), "\nR\tcha\t2\t987\n"' > newfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.