Operating System - HP-UX
1838642 Members
2175 Online
110128 Solutions
New Discussion

how can I separate eacho one of fields in the line and put them in the file as lines

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

how can I separate eacho one of fields in the line and put them in the file as lines

I have a variable or a line in the file, and it consists of a number of fields.
Now, I want to put each one of fileds in a file, and as lines.

Also, how do I change all Caps to low cases?


Can sombody please help me with this korn shell scripting?

Thanks!
none
4 REPLIES 4
Redhat
Trusted Contributor

Re: how can I separate eacho one of fields in the line and put them in the file as lines

you can use some things like this:-

cat file |while read line
First=echo $line |awk '{print $1}'
Second=echo $line |awk '{print $2}'
etc

for converting CAPS to LOW use

tr '[A-Z]' '[a-z]'
see man tr for more info
spex
Honored Contributor

Re: how can I separate eacho one of fields in the line and put them in the file as lines

Hello,

> Now, I want to put each one of fileds in a
> file, and as lines.

$ echo ${myvar} | awk '{print $1}' >> field1

> Also, how do I change all Caps to low cases?

There are many ways to do this, but the best way is through a shell built-in:

$ typeset -l mylcvar=${myvar} && echo ${mylcvar}

Other options:

$ echo ${myvar} | tr '[:upper:]' '[:lower:]'
$ echo ${myvar} | dd conv=lcase 2>/dev/null

http://docs.hp.com/en/B2355-90046/

PCS
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: how can I separate eacho one of fields in the line and put them in the file as lines

Let's see you want a Korn shell solution? Ok, I will do it in ksh:

------------------------------------------
#!/usr/bin/ksh

typeset STAT=0
awk '{ for (i = 1; i <= NF; ++i) print tolower($i); }'
STAT=${?}
exit ${STAT}
--------------------------------------------

Now use it like this:
my.sh < infile > outfile
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: how can I separate eacho one of fields in the line and put them in the file as lines

# awk '{for(i=1;i<=NF;i++) print tolower($i) >"out"}' infile