- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: exporting variables from awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2008 06:41 PM
тАО03-02-2008 06:41 PM
I'm trying to split a string in an unknown number of strings separated by spaces.
I'm trying with awk, but i can't export variables back to shell.
this is what i have, it doesn't work:
str='one two three four five six'
i=0
awk -v string="$str" 'BEGIN { split(string,a); for(i in a) print a[i]; }' | \
while read name
do
filename[$i]=$name
echo ${filename[$i]}
let i=$i+1
done
I know i can append awk output in a file and then do
cat filename | \
while read name
....
but i don't want to use external files.
i also know about variable=`awk statement`
but this lastone can't return me more than one variable.
any idea?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2008 08:05 PM
тАО03-02-2008 08:05 PM
Re: exporting variables from awk
>it doesn't work:
You don't explain why it fails and what you want to do?
>I know i can append awk output in a file and then do
This is not much different than the pipe except you don't need a name.
>i also know about variable=$(awk statement)
but this last one can't return me more than one variable.
It returns you a string. Which you can fiddle with.
The standard trick is to return a command and use eval on it:
eval $(echo export x=1 b=2)
>str='one two three four five six'
You could use:
set -A filename $str
Or you could do:
for i in $str; do
echo $i
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2008 10:55 PM
тАО03-02-2008 10:55 PM
SolutionTo me, I think perl is your answer here but if you don't like it perhaps it would be simpler to change the "while" to a "for" loop and cycle through the output like this.
for $result in `awk -v string="$str" 'BEGIN { split(string,a); for(i in a) print a[i]; }'`
do
filename[$i]=$result
etc etc etc
done
You might want to look at the venerable "expr" with a nice regular expression command instead of awk for something like this too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-03-2008 02:38 AM
тАО03-03-2008 02:38 AM
Re: exporting variables from awk
why the "awk" awkwardness?
The Posix shell can handle this
$ myvirtues="lust gluttony greed sloth wrath envy\npride"
$ set -A sins $(echo $myvirtues)
$ echo ${#sins[*]}
7
$ echo ${sins[*]}
lust gluttony greed sloth wrath envy pride
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-03-2008 05:18 AM
тАО03-03-2008 05:18 AM
Re: exporting variables from awk
Adding a bit to Ralph's virtues:
# myvirtues=" myvirtues="lust gluttony greed sloth wrath envy\npride\nimpatience\nlaziness\nhubris"
# echo ${myvirtures}|xargs
lust gluttony greed sloth wrath envy pride impatience laziness hubris
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-03-2008 05:45 PM
тАО03-03-2008 05:45 PM
Re: exporting variables from awk
that works great on ksh, but unfourtunly i'm working on bash.
i'll explain what i need
what i have to do is to look for a file and do something with it.
points are:
-I've to search in many directories
-I'll probably found not one but two, three or four files, with the same name but diferent extension. and perhaps some completly similar files in diferent directories
-I have to chose only one of those, which one? the one has not null lenght (wc -l) and the one has the higher value in a list of extension priorities
so, my idea was to loop the dirs, and make finds with the only file mask for any dir. then get the find result in a variable, split it, and make stuff.
but perhaps that's not the best idea.
time ago "somebody" used to do it like this:
file_1=`echo $fullname | cut -d' ' -f1`
file_2=`echo $fullname | cut -d' ' -f2`
but he didn't concern into the posibility have more than two filenames in the string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-03-2008 06:20 PM
тАО03-03-2008 06:20 PM
Re: exporting variables from awk
(Scripts should be in sh or ksh.)
>file_1=`echo $fullname | cut -d' ' -f1`
>but he didn't concern into the possibility have more than two filenames in the string.
If they are whitespace separated then "for" or arrays should automatically separate them.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-04-2008 08:41 AM
тАО03-04-2008 08:41 AM
Re: exporting variables from awk
is your bash run on Linux or HP-UX?
Btw, bash can equally well handle arrays
like hpux-sh or ksh.
It only has a slightly deviating syntax
(a peek at man bash always helps ;-)
Because we haven't installed Bash on our HP-UX boxes (simply no need for it there)
I am running this on Linux.
$ uname
Linux
$ echo $BASH_VERSION
3.1.17(1)-release
To stick with James' extended set of virtues
(btw. I would have thought that laziness was covered by sloth, and hubris by pride?)
$ myvirtues=" myvirtues="lust gluttony greed sloth wrath envy\npride\nimpatience\nlaziness\nhubris"
as I'm doing this on Linux I need to cater the escape option along with the echo command (omit on hpux)
$ sins=($(echo -e "$myvirtues"))
$ echo ${#sins[*]}
10
$ echo ${sins[*]}
lust gluttony greed sloth wrath envy pride impatience laziness hubris
But this doesn't help you much, I suppose?
Apropos, the GNU find available on Linux
has a nifty -print0 option,
which separates the output records by \0
so that find can also cope with (Unix)unusual file names that contain whitespace etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-04-2008 09:09 AM
тАО03-04-2008 09:09 AM
Re: exporting variables from awk
And I would add that the use of 'xargs' applies to the Bash environment too:
$ virtues=" laziness\timpatience hubris\nare\nvirtures"
$ echo "${virtues}"
laziness impatience hubris
are
virtures
$ values=$(echo "${virtues}"|xargs)
$ echo ${values}
laziness impatience hubris are virtures
By the way, I teasingly used "hubris ~ pride" and "laziness ~ sloth" as Ralph pointed out, since these are Perl virtues he appreciates too :-)
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-04-2008 10:17 AM
тАО03-04-2008 10:17 AM
Re: exporting variables from awk
> Ralph: Apropos, the GNU find available on Linux has a nifty -print0 option, which separates the output records by \0 so that find can also cope with (Unix)unusual file names that contain whitespace etc.
HP-UX introduced this to 11.23 with patch: PHCO_32269.
The 'find' command for 11.31 supports this option, too.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2008 05:00 AM
тАО03-06-2008 05:00 AM
Re: exporting variables from awk
i'm trying it on linux, but it must work on HP-UX at work.
Yes, i have learned to handle arrays in bash. well, i'm learning it right now, trying these stuff
matter is that i have resolved in a simple way:
would it work on HP-UX?
for dir in $directories
do
echo "Searching in $dir" >> $log
find $dir -name "$filemask*" 1> fullnames 2> /dev/null
#cat fullnames
if [ -n `cat fullnames` ]; then
break;
fi;
done
i=0
flag=0
echo
while read fullname
do
echo "Checking for posible duplicated file" >> $log
for (( e=0 ; e <= i ; e++ ))
do
echo "checking file $i : $fullname iteration number: $e" >> $log
if [ "$fullname" = "${fullnames_array[$e]}" ];
then
echo "$fullname number $i equals another, it's Discarded" >> $log
flag=1
else
echo "$fullname ins unique!" >> $log
fi
done
if (( flag == 0 ))
then
fullnames_array[$i]=$fullname
i=$(( i + 1 ))
#echo ${fullnames_array[*]}
#echo "print $fullname"
fi
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-07-2008 01:31 AM
тАО03-07-2008 01:31 AM
Re: exporting variables from awk
This works in ksh. You have to replace that funny for loop by a while:
for (( e=0 ; e <= i ; e++ ))
for dir in $directories; do
echo "Searching in $dir" >> $log
find $dir -name "$filemask*" 1> fullnames 2> /dev/null
#cat fullnames
if [ -n "$(< fullnames)" ]; then
break;
fi
done
i=0
flag=0
echo
while read fullname; do
echo "Checking for possible duplicated file" >> $log
(( e = 0 ))
while (( e <= i )); do
echo "checking file $i : $fullname iteration number: $e" >> $log
if [ "$fullname" = "${fullnames_array[$e]}" ]; then
echo "$fullname number $i equals another, it's Discarded" >> $log
flag=1
else
echo "$fullname is unique!" >> $log
fi
(( e += 1 ))
done
if (( flag == 0 )); then
fullnames_array[$i]=$fullname
(( i += 1 ))
#echo ${fullnames_array[*]}
#echo "print $fullname"
fi
done < fullnames
echo "found files: ${fullnames_array[*]}" >> $log
I'm not sure why you want to check for a duplicated file, find won't return those.
And you can always use "sort -u" to get rid of dups.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2008 06:54 PM
тАО03-09-2008 06:54 PM
Re: exporting variables from awk
it looks better with a while, but what looks really better for me is "$(< fullnames)" insteed the horrible `cat $fullnames`.
in a deeper way, what are the differences between both?
you're right, find won't return me duplicated...
but i'll cut string routes before doing comparison
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2008 08:47 PM
тАО03-09-2008 08:47 PM
Re: exporting variables from awk
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2008 09:10 PM
тАО03-09-2008 09:10 PM
Re: exporting variables from awk
The only problem with that while is that a continue won't work, it will skip the increment.
>what are the differences between both?
Besides what Hein said, it helps you in a catless style of scripting. You really only need cat(1) in a few cases, multiple inputs, 2 GB limitations or copying a file to stdout/stderr.