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

replacing whitespaces with carriage returns

Status
Not open for further replies.

A5k

Programmer
Jul 29, 2001
54
CA
Hi all,
I'm a newbie to scripting within unix. I'm using bash and ksh for my scripts. I want to know how to turn a file with a list of variable number of users so that it is columnar.

$ users>>file
user1 user2 user3 user4
$ script file
user1
user2
user3
user4

Thanks,
dAve
 
Dave:

Here's an awk script that prints out the Number of Fields, one
per line:

awk ' { for(i=1; i<=NF; i++)
print $i } ' < file > file2

Also, here's a translate command, tr, example I ripped off from the tr SCO Open
Server V man page and modified slightly:

# The following example creates a list of all the words in file, one per line
# in file2, where a word is taken to be a maximal string of alphabetics. The
# strings are quoted to protect the special characters from interpretation by
# the shell; 012 is the ASCII code for newline:

tr -cs &quot;A-Za-z0-9&quot; &quot;[\012*]&quot; <file >file2 # added numerics


Regards,

Ed
Schaefer
 
To make the file from list to columnar in a script try out

ex file <<!
%j
w
q
!

or to get columns in stdout try out

sed '
:a
$!{
N
ba
}
s/\n/ /g
' file

Cheers,
ND [smile]
 
Thanks!! The sample scripts have given me another little insight into the fun things you can do within scripting.

Thanks again,
dAve
 
Hi,
I tried your awk script but the lines in my file were too long.

I ran it through a2p and came out with the following perl script and it works Ok with long lines.

Thanks for help. here is the perl script.

#!/usr/local/bin/perl

$[ = 1; # set array base to 1

while (<>) {
chop; # strip record separator
@Fld = split(' ', $_, 9999);
for ($X = 1; $X < $#Fld; $X++) {
printf &quot;%s\n&quot;, $Fld[$X];
}
}
 
awk 'BEGIN{OFS=&quot;\n&quot;} $1=$1' file

OR

cat file | tr ' ' '\n'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top