Command to print columns with space

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

cat $OUTFILE2 | while read SHARES

do
SHARE_NAMES=$SHARES
SVM=`echo $SHARE_NAMES | awk '{print $1}'`
CIFS_SHARE=`echo $SHARE_NAMES | awk '{print $2}'`
PATH_NAME=`echo $SHARE_NAMES | awk '{print $3}'`

echo "vserver cifs share create -vserver" $SVM "-share-name" $CIFS_SHARE "-path" $PATH_NAME >> $SCRIPT
done

This is what the output needs to look like:

vserver cifs share create -vserver Host1 -share-name UNIX -path /vol/volume/my stuff

but getting

vserver cifs share create -vserver Host1 -share-name UNIX -path /vol/volume/my

here is what I tried so far and no luck:

# PATH_NAME=`echo $SHARE_NAMES | awk '{print $3}'`
# PATH_NAME=`echo $SHARE_NAMES | awk -F"\t" '{print $3}'`
# PATH_NAME=`echo $SHARE_NAMES | awk -F'\t' '{print $3}'`
# PATH_NAME=`echo $SHARE_NAMES | awk -F" {2,}" '{print $3}'`
# PATH_NAME=`echo $SHARE_NAMES | awk " -F \" '{print $3}'`

Any suggestions would be greatly appreciated!

Try something like:

while read SVM CIFS_SHARE PATH_NAME
do
  echo "vserver cifs share create -vserver ${SVM} -share-name ${CIFS_SHARE} -path '${PATH_NAME}'" 
done < $OUTFILE2 > $SCRIPT

--
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...

1 Like

Hi,

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

[FONT=r\_ansi][SIZE=2][FONT=r_ansi][SIZE=2] ------------- ---------- ----

I run my script it pulls both that line and the blank line in..

 vserver cifs share create -vserver ------------- -share-name ---------- -path ---- 
  
 -share-name -path vserver cifs share create -vserver

Thank you all again!

Try something like this:

{
  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

Thank you again for replying.. I may be missing a step because it's still showing the dashes and the blank line in my output..

Here is what I have now:

cat $OUTFILE2 |while read SVM CIFS_SHARE PATH_NAME
 do
 if [ -n "$SVM" ]; then
 echo $DRRSH "vserver cifs share create -vserver ${SVM} -share-name ${CIFS_SHARE} -path $PATH_NAME" >> $DRSCRIPT
 fi
 done

Thank you again for all your help - this was a big help for me!

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..

General text:

Here is the output from the script:
Dash's

vserver cifs share create -vserver ------------- -share-name ---------- -path ---- 

Blank line:

vserver cifs share create -vserver 

Input file looks something like this:

vserver share-name path 
 ------------- ---------- ---- 
 SVM_A   admin$ / 

I am able to delete the first line by omitting share-name but the dashes or underscore still appear..

My apologies - I didn't consider those lines code because they are just general text.. I will make it code moving forward..

So there are two header lines. In that case you could modify my suggestion like this:

{
  read; read                     # skip header lines

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..

Thanks