Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding tab to system()

Status
Not open for further replies.

blarneyme

MIS
Jun 22, 2009
160
US
Need to add a tab to a string inside system().

For example:
Code:
system("echo x.x.x.x\t > file.txt");

But the \t is only adding a space to file.txt.

I've also tried setting '$t = '\t'; but that also only prints a space:
Code:
system("echo x.x.x.x${t} > file.txt");
 
Hi,
If I try the following
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$cmd[/blue]=[red]"[/red][purple]echo x.x.x.x[purple][b]\t[/b][/purple] > file.txt[/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\$[/b][/purple]cmd = [blue]$cmd[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]# execute command[/i][/gray]
[black][b]my[/b][/black] [blue]$rc[/blue]=[url=http://perldoc.perl.org/functions/system.html][black][b]system[/b][/black][/url][red]([/red][blue]$cmd[/blue][red])[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\$[/b][/purple]rc = [blue]$rc[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]
then after viewing the created file file.txt in HEX I see
Code:
[COLOR=#ff00ff]0000000[/color][COLOR=#008080]:[/color] 782e 782e 782e 7809 200d 0a         [COLOR=#804040][b]     x[/b][/color].[COLOR=#804040][b]x[/b][/color].[COLOR=#804040][b]x[/b][/color].[COLOR=#804040][b]x[/b][/color].[COLOR=#804040][b] [/b][/color]..
Also you see \t i.e. 09H (0x09) the horizontal tab character is in the file. but followed by space i.e. 20H (0x20) - see the ASCII Chart
 
Hi

If that runs on Unix or Unix-like system, I would be concerned by the shell issue part - the [tt]\t[/tt] is enumerated in [tt]IFS[/tt] so will be treated as parameter separator when the word splitting is performed.
Code:
[b]my[/b] [navy]$cmd[/navy][teal]=[/teal][green][i]"echo [highlight]'[/highlight]x.x.x.x\t[highlight]'[/highlight] > file.txt"[/i][/green][teal];[/teal]

[gray]# or[/gray]

[b]my[/b] [navy]$cmd[/navy][teal]=[/teal][green][i]"echo [highlight]\"[/highlight]x.x.x.x\t[highlight]\"[/highlight] > file.txt"[/i][/green][teal];[/teal]
mikrom, your code gives me this file :
Code:
[blue]master #[/blue] od -xc file.txt 
0000000    2e78    2e78    2e78    0a78
          x   .   x   .   x   .   x  \n
0000010
With my suggestion the file becomes this :
Code:
[blue]master #[/blue] od -xc file.txt 
0000000    2e78    2e78    2e78    0978    000a
          x   .   x   .   x   .   x  \t  \n
0000011
And I think the OP wants the later.

By the way, all this happens on Linux with [tt]perl[/tt] 5.10.0 and [tt]bash[/tt] 4.0.28.

Feherke.
 
Btw, I tried it on Windows, but I wonder why your hex view shows 2e78 and my shows 782e
'x' is hex 78
and
'.' is hex 2e
so 'x.' should be in hex 782e

I tried the od in MSYS on Windows and I got this
Code:
$ od -xc file.txt
0000000 2e78 2e78 2e78 0978 0d20 000a
          x   .   x   .   x   .   x  \t      \r  \n  \0
0000013
I wonder why the od command swaps the bytes, but it's OT in this thread :)
 
In one case it is being interpreted as a 16-bit word on a little-endian system... i.e. the last significant byte is printed first. To avoid confusing myself I usually use od -t x1c, which should look the same on any platform.

Code:
$ echo x.x.x.x | od -t x1c
0000000 78 2e 78 2e 78 2e 78 0a
          x   .   x   .   x   .   x  \n
0000010

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top