Command to print columns with space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command to print columns with space
# 1  
Old 03-06-2017
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:

Code:
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

Code:
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:

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

but getting

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

here is what I tried so far and no luck:
Code:
# 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!

Last edited by Scrutinizer; 03-06-2017 at 03:37 PM.. Reason: code tags; removed spurious formatting
# 2  
Old 03-06-2017
Try something like:
Code:
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...

Last edited by Scrutinizer; 03-06-2017 at 04:12 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-06-2017
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.
# 4  
Old 03-06-2017
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..

Code:
 vserver cifs share create -vserver ------------- -share-name ---------- -path ---- 
  
 -share-name -path vserver cifs share create -vserver

Thank you all again!
Moderator's Comments:
Mod Comment Please use CODE tags (not FONT and SIZE tags) when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 03-06-2017 at 06:25 PM.. Reason: Delete FONT and SIZE tags; add CODE tags.
# 5  
Old 03-06-2017
Try something like this:
Code:
{
  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


Last edited by Scrutinizer; 03-06-2017 at 04:36 PM..
# 6  
Old 03-07-2017
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:

Code:
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!



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-07-2017 at 02:04 PM.. Reason: Added CODE tags.
# 7  
Old 03-07-2017
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Romove columns and replace a space with a character

Hi, I have a file containing this: testvol1 unix enabled testvol2 unix enabled testvol3 unix enabled testvol3 qtree1 unix enabled testvol3 qtree2 unix enabled testvol4 unix enabled testvol4 qtree1 unix enabled And I want an output of this: testvol1... (4 Replies)
Discussion started by: niap21
4 Replies

3. Shell Programming and Scripting

Columns to rows with space

Hi, My input is in the following way a b c d e f I would like to have it printed as a b c d e f Any awk scripts are appreciated. (1 Reply)
Discussion started by: jacobs.smith
1 Replies

4. UNIX for Dummies Questions & Answers

help needed: remove space between certain columns

dear all, i have a data looks like this (i have 3000 columns): rs123 A T T G C C C C C C A A A A A A A A A A A ... rs154 T T G C C C C C A A A A A A A A A A T T G ... rs126 A C C C C C A A A A A A A A A A A A T T G ... I want to remove all the space after the 2nd column and make the... (2 Replies)
Discussion started by: forevertl
2 Replies

5. Shell Programming and Scripting

awk command to print multiple columns

Hello Team, I have written following command which is giving output is as shown below. bash-3.00$ grep -i startup catalina.out | tail +2 | sed -n 1p | awk -F" " '{ for (x=1; x<=5; x++) { printf"%s\n", $x } }' Dec 19, 2010 3:28:39 PM bash-3.00$ I would like to modify above command to... (2 Replies)
Discussion started by: coolguyamy
2 Replies

6. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

8. Shell Programming and Scripting

help with delimiting columns with a space

Hi all, i am writting a script to fix some problems we have with data that we need that contains 1000s of records. I have a text file with 3 columns of data. The problem is there should be a space between the end of the first column and the start of the second column. The majority of the data is... (7 Replies)
Discussion started by: borderblaster
7 Replies

9. HP-UX

How to add space in between columns in VI editor

Hi, I am trying to add space after 3rd and 6th column for editing a file in special format. How should I achieve in VI?. So, is it possible to do in it in sed ? thanks vipa (1 Reply)
Discussion started by: vipas
1 Replies

10. Shell Programming and Scripting

Trying to space columns in a report

I have a program that calculates shipping cost for an item and prints the information to a file. I cannot seem to get the lines to print with equal spacing in columns. I need data like this to be aligned into columns: Optical Mouse 5 A $25 Compaq Presario 3... (2 Replies)
Discussion started by: turbulence
2 Replies
Login or Register to Ask a Question