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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nawk print to file in the same line 1

Status
Not open for further replies.

elcid2k3

Technical User
Jun 8, 2007
9
0
0
RO
Hi,

I print some text in a file but every print statement writes on a new line and I wish to write on the same line.

For example the code
if (index(FAULTS, 18)) print fca[1] >> "faults.txt"
if (index(FAULTS, 23)) print fca[2] >> "faults.txt"
if (index(FAULTS, 37)) print fca[3] >> "faults.txt"

outputs some result like:
fsafvsa
fsafasf
fasdfge

and I would like to be:
fsafvsa fsafasf fasdfge

How can I do this?

PS: I know is a trivial question, I am new to awk :)



 
Hi

Code:
if (index(FAULTS, 18)) print[red]f[/red] fca[1][red]" "[/red] >> "faults.txt"
if (index(FAULTS, 23)) print[red]f[/red] fca[2][red]" "[/red] >> "faults.txt"
if (index(FAULTS, 37)) print fca[3] >> "faults.txt"

[gray]# or[/gray]

[red]ORS=" "[/red]
if (index(FAULTS, 18)) print fca[1] >> "faults.txt"
if (index(FAULTS, 23)) print fca[2] >> "faults.txt"
[red]ORS="\n"[/red]
if (index(FAULTS, 37)) print fca[3] >> "faults.txt"

Feherke.
 
The usage of printf without format may be dangerous
Code:
$ [blue]cat infile[/blue]
xyz
%s
%s%s%s
ccc
$ [blue]awk '{printf $1 " "}' infile[/blue]
xyz awk: There are not enough parameters in printf statement %s .

 The input line number is 2. The file is infile.
 The source line number is 1.
$
The better way is :
Code:
$ [blue]awk '{printf("%s ",$1) }' infile[/blue]
xyz %s %s%s%s ccc $



Jean-Pierre.
 

Good point, Jean-Pierre. Worth a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top