1828483 Members
2730 Online
109978 Solutions
New Discussion

Script Output

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Script Output

Hi All,

Can you help below Please? Using sed command, How to convert the input as "adhn4807, adhn5326, adhn5327, adhn5328, adhn5333," to the output below?

adhn4807
adhn5326
adhn5327
adhn5328
adhn5333

Thanks alot in advance.
Regards,
Negara

Santos
8 REPLIES 8
R.K. #
Honored Contributor

Re: Script Output

Hi Dewa,

I am not sure about sed, but following script runs well according to you:

#################
#!/usr/bin/sh
VAR="adhn4807, adhn5326, adhn5327, adhn5328, adhn5333,"
for A in `echo $VAR | awk -F"," '{print $1,$2,$3,$4,$5}'`
do
echo $A
done
#################

Hope this helps..
Don't fix what ain't broke
Dewa Negara_4
Regular Advisor

Re: Script Output

Thanks alot. But here there are so many servers in the list...adhn4807, adhn5326, adhn5327, adhn5328, adhn5333,...,....,.....,etc (many more)

Need to convert the output to:
adhn4807
adhn5326
adhn5327
adhn5328
adhn5333
...
...
...
etc (many more)


Regards,
Negara
Santos
R.K. #
Honored Contributor

Re: Script Output

Hi Again,

You can put the server names in a file "ex1".
Then run following command:

# awk -F", " '{ for (i = 1; i <= NF; i++) print $i}' ex1

adhn4807
adhn5326
adhn5327
adhn5328
adhn5333


Where:
#more ex1
adhn4807, adhn5326, adhn5327, adhn5328, adhn5333

Hope this will be of some help..
Don't fix what ain't broke
Jean-Luc Oudart
Honored Contributor
Solution

Re: Script Output

if this is in a file :

cat | tr "\," "\n"
or
tr "\," "\n" <

Regards
Jean-Luc
fiat lux
James R. Ferguson
Acclaimed Contributor

Re: Script Output

Hi Dewa:

Your data shows that there are whitespaces following the commas. To eliminate these and produce the output you showed, use a one-process Perl instead of pipes with 'tr' and/or 'sed':

# perl -pe 's/,\s*/\n/g' file

(or)

# X="adhn4807, adhn5326, adhn5327, adhn5328, adhn5333"
# echo ${X}|perl -pe 's/,\s*/\n/g'

This code reads your file globally substituting a newline character for any comma with or without an optional whitespace (\s) following it.

If you are using a GNU 'sed' then this would work too:

# sed -e "s/,[ ]*/\n"/g file

Unfortunately, the standard HP-UX 'sed' doesn't support this syntax.

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Script Output

You have spaces after each ","?
for word in $(sed 's/,//g' file); do
print $word
done

With vi you can change comma to newlines:
:%s/, /^V/g
(That's a control-V.)
Then a cleanup of comma without a space.
Dennis Handly
Acclaimed Contributor

Re: Script Output

Oops:
With vi you can change comma to newlines:
:%s/, /^V^M/g
(That's a control-V followed by a enter.)
Then a cleanup of comma without a space.
Dewa Negara_4
Regular Advisor

Re: Script Output

Hi All,

Thanks alot for your help. Got the answer. Appreciate your help.

Best Regards,
Negara
Santos