Parsing Output of a Variable

i have a log file that contains something similar to this:

one two three four five six seven eight
nine ten eleven twelve thirteen fourteen
one two three four five six seven eight
nine ten eleven twelve thirteen fourteen
one two three four five six seven eight
nine ten eleven twelve thirteen fourteen
one two three four five six seven eight
nine ten eleven twelve thirteen fourteen
one two three four five six seven eight
nine ten eleven twelve thirteen fourteen

the file is on a remote host.

I take the last 10 lines of this file and i store it to a variable. Then this variable is passed down stream through an agent from the remote host to the central monitoring server.

VARA=$(echo `tail -10 /var/log/arm.log`)

When the lines have been sent back to the central monitoring server, it comes out looking like this:

one two three four five six seven eight nine ten eleven twelve thirteen fourteen one two three four five six seven eight nine ten eleven twelve 
thirteen fourteen one two three four five six seven eight nine ten eleven twelve thirteen fourteen one two three four five six seven eight nine ten eleven
 twelve thirteen fourteen one two three four five six seven eight
nine ten eleven twelve thirteen fourteen

My question is, how do i "disentangle" these lines, and have them return to the type of line-by-line output one would get with a tail -10?

by the way, the words/characters at the beginning and end of each line is always never the same for each line.

OS: Linux RedHat/Ubuntu/Solaris
Shell: Bash

VARA="$(tail -10 /var/log/arm.log)"

Notice the double quotes around the subshell, which prevent the shell from evaluating the variables content and the missing "echo", which would do the same, eating away the newlines.

If i may suggest something you didn't ask: don't mix subshell commands "$(...)" and backticks "``". In fact don't use backticks at all! They are only supported for historical purposes and there is no reason at all why one should use them. It is perfectly possible to nest subshells:

VARA="$(echo $(tail -10 /var/log/arm.log))"

but in your case you better skip the echo altogether and use the version stated above.

I hope this helps.

bakunin

1 Like

thanks so much. looks like this will work perfectly.

about the ticks.

how would i nest something like this:

VARA=$(for all in `echo fat skinny fine ugly`
do
echo $all
done)

Please use CODE-tags when posting code or terminal output. It makes the reading much easier and preserves indentation.

To answer your question: you want to split at word boundaries. As the shells default IFS (internal field separator - the character which is used to delimit words) is the space character already you need to do nothing, because the whole "echo" is superfluous:

VARA=$(for all in fat skinny fine ugly ; do
     echo $all
done)

Notice the difference between:

for all in fat skinny fine ugly ; do
     echo $all
done

and

for all in "fat skinny fine ugly" ; do
     echo $all
done

The difference is caused because the double quote is used exactly for this purpose: to tell the shell not to interpret the IFS character in its function but as mere normal character.

I suggest you read the man page of ksh about the IFS character and the IFS special variable.

I hope this helps.

bakunin