Hello I am trying to pull specific columns from an output file that contains spaces and make it a variable.. Here is a piece of the output file and my command:
Host1 UNIX /vol/volume/my stuff
When I pull in the data into my variable the word stuff is left off - I need this as part of my variable
--
Note: by using read like this, SVM will contain the first field ( "host1" ), CIFS_SHARE will contain the second field ( "UNIX" ) and PATH_NAME will contain the third field and the rest of the line ( "/vol/volume/my stuff" ). By putting single quotes around the last variable, its content will be a single field in the resulting command...
Is it possible for you to use some other field delimiter in the input ? So for example, using commas between fields rather than spaces:
Host1,UNIX,/vol/volume/my stuff
This way, you'd be able to accommodate paths with spaces (though not with commas, naturally - you'd then have the same problem again). Or you could use tab characters rather than spaces, since it should be impossible for those to occur in a path under most conceivable conditions.
That'd be my recommendation, anyway: rather than finding creative ways to handle this one special case, use something other than spaces that will never occur in a valid path to separate your input fields so that paths with any number of spaces will be fine.
Thank you all - I can't modify the input file.. As for the 1st reply that seemed to work - going to add that to my script now and see.. Will keep everyone posted and thank you again!
---------- Post updated at 03:20 PM ---------- Previous update was at 03:04 PM ----------
Thank you again - the only 2 things I have left on this script is how do omit a blank line at the end of the input file and I also have this as the header that I want to omit as well
{
read # skip header line
while read SVM CIFS_SHARE PATH_NAME
do
if [ -n "$SVM" ]; then # if $SVM is not empty, i.e. the line is not a blank line...
echo "vserver cifs share create -vserver ${SVM} -share-name ${CIFS_SHARE} -path '${PATH_NAME}'"
fi
done
} < $OUTFILE2 > $SCRIPT
Which results did you get when deploying Scrutinizer's proposal? Your attempt seems to deviate slightly. Did you fully understand what you were doing when applying the modifications? And, is the dashes line the first line in the input file or the second, being sth. like an "underscore" line?
My apologies about the code tags - will make sure I do that moving forward.. Thank you..
---------- Post updated at 01:48 PM ---------- Previous update was at 01:39 PM ----------
Thank you for replying..
Actually not really scripting isn't my strong suite - continuously trying to learn and understand..
As for is it dashes or underscore, they are from the first line of the input file which were headers.. In a previous step that populates the OUTFILE2 I exclude some items but it left the dashes or underscores of the header..
It might well be that my arithmetics is a bit weird, but I see the dashes in line 2. This is not what Scrutinizer allowed for - his code will eliminate vserver share-name path but NOT the dashes. And, the "blank line" might NOT be empty if the code does not eliminate it.
So - what do you want to do - suppress both lines? Print line1, suppress line2? What's in the last, "blank" line?
The 1st line is the first line in the input file.. The blank line is the last line of the input file - has about 200 lines total.. I remove the words from the 1st line but the dashes or underscores are still there.. The blank line is also there as well..