The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
If loop is not working toanilsharma1 UNIX for Advanced & Expert Users 12 08-20-2008 01:21 PM
If then else loop not working findprakash UNIX for Dummies Questions & Answers 2 05-25-2008 01:43 AM
For loop not working...! :( bullz26 Shell Programming and Scripting 3 03-16-2008 06:28 AM
if loop is not working ranga27 Shell Programming and Scripting 3 09-16-2007 01:19 PM
Loop not working nitin Shell Programming and Scripting 2 11-07-2001 07:55 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
KME KME is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 7
Cut not working in a loop

I have a function "MyPrint" that runs great on a file (BaseData.txt) that has one line of data. If i add rows to the text file it's reading the tFile variable becomes a list of every field 2 in the file. To correct this, i tried to call the function from a loop where i read one line at a time and pass the line to the function. >>>The problem is when i try to cut the variable containing the $line0 I get an error cannot open...then it gives the proper value the first field in the line i'm trying to cut. It repeats this cannot open line for each column in the $line0 printing proper values.

I've seen other posts like this but none of the solutions have worked for me. Completely at a loss for new ideas desperate for help!

BaseData.txt has 2 rows:
hsun test1a.sas7bdat 8269135872 7.70123
hsun test1a2.sas7bdat 8269135872 7.70123



Code:
 
#!/usr/bin/ksh
 
function FindPath3 {
 find /u03/hsun -name ${1} 
}
function MyPrint {   
tfile=`cut -f2 -d ' ' ${1}`
#tfile= `echo ${1} |cut -f2 -d ' '`
echo $tfile
print `cut -f1,2,3,4 -d ' ' ${1}`, `FindPath3 ${tfile}` > /home/kemard/Test3_OP.txt
} 
 
#MyPrint /u03/kemard/BaseData.txt 
 
  while read line0
     do 
     #MyPrint ${line0}
     lineValue1=${line0}
     lineValue2=`cut -f2 -d ' ' ${line0}`
     echo "line value 1 is: ${lineValue1}"
     echo "line value 2 is: `cut -f2 -d ' ' ${lineValue1}` "
     #echo "line value 2 is: ${lineValue2}"
     #cut -f1,2,3,4 -d ' ' ${line0} > /home/kemard/Test3_OP.txt
 done < /u03/kemard/BaseData.txt

  #2 (permalink)  
Old 4 Weeks Ago
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
The syntax in this commented-out line is along the right lines but needed to preserve spaces in ${1}.
Quote:
#tfile= `echo "${1}" |cut -f2 -d ' '`
In this line (and many similar lines in the script) program "cut" is expecting ${line0} to contain a filename not a string ... hence "cannot open".
Quote:
lineValue2=`cut -f2 -d ' ' ${line0}`
I think that this will be an improvement:

Code:
lineValue2=`echo "${line0}"|cut -f2 -d ' '`

Then apply the same syntax shift to other similar lines.

By the way, neither of the two functions in the script are actually executed but we can see that most of the code is in the while loop.


You will need to preserve spaces whenever there is more than one field. Though there is little point in copying ${line0}, this line needs quotes.
lineValue1="${line0}"


What is the expected output of the script?

Last edited by methyl; 4 Weeks Ago at 12:27 PM..
  #3 (permalink)  
Old 4 Weeks Ago
KME KME is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 7
Methyl,
Thank you so much for helping. I'm still having problems but the additional quotes were very insiteful, wierd but insiteful. I was able to cut in teh loop but I was never able to send the line to the function. That is ok however because i was able to get each field from the string as if it was awk $1,$2,etc..

However, my call to the FindPath3 function is now broken and i have no idea why. It gets called but the ${1} is blank/ i've tried several different formats with the quotes with the parameter and echo statement but no luck. Any more ideas. I realize there are possibly other ways to accomplish my task, but my objective is to learn how to pass these parameters in the functions as well as achieve the end result. I'll need this ability for other code.


Code:
 
    #!/usr/bin/ksh
 
function FindPath3 {
echo "here: ${1}"
 find /u03/hsun -name ${1} 
}
function MyPrint {   
tfile=`echo "${2}"`
 echo "tfile in myprint: ${2}"
 tPath= `FindPath3 test1a2.sas7bdat`
print $1,$2,$3,$4,$tPath > /home/kemard/Test3_OP.txt
} 
 while read line0
     do 
     #echo "$line0"
     MyPrint $line0
   done < /u03/kemard/BaseData.txt

BTW, in order to get the cut to work i had to use the quotes and feed the cut with a pipe rather than pass it from the right side:

Code:
 ## lineValue3=`echo "${line0}" | cut -f2 -d ' ' `

  #4 (permalink)  
Old 3 Weeks Ago
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
Not looked deeply because I am unclear what the expected output of the script is.

There is an extra space character in this line:
Quote:
tPath= `FindPath3 test1a2.sas7bdat`
When assigning values to variables there is never a space character either side of the equals sign.


Code:
tPath=`FindPath3 test1a2.sas7bdat`

  #5 (permalink)  
Old 3 Weeks Ago
KME KME is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 7
Talking

Methyl, thank you so much i never would have considered a space - !!!!!!! My code finally runs - thanks to you.
The object of the code was to find files over 1 gig write to a file then loop through the file, sending the file names to the find function, return the file location, Print it all out to a text file. It could be done differently w/less code but this syntax was really important to me. I still don't understand 2 things
1. In the final code sending the $Line0 to the my print, this was a string not a line. I could not use cut on this. Why is it a string not a line?
2. In the findPath3 function, I played around a lot with the syndax, why did i have to use the back ticks the way i did. I originally tried the line below but it returned nothing.

Code:
echo"`find /u03/hsun -name ${1}`"


Code:
 
    #!/usr/bin/ksh
function CreateT1 {
 ls -lR ${2} | sort +2 | awk ' $5 > 1073741824 {print $3, $9, $5, $5/1073741824}' > ${1}
}
 
 
function FindPath3 {
 `echo find /u03/hsun -name ${1}`
}
function MyPrint {   
tfile=`echo "${2}"`
 tPath=`FindPath3 ${2}`
 echo "tpath: $tPath"
print $1,$2,$3,$4,$tPath >> /home/kemard/Test3_OP.txt
} 

#####Code Body######
CreateT1 /u03/kemard/BaseData.txt /u03/hsun
 while read line0
   do 
       MyPrint $line0
   done < /u03/kemard/BaseData.txt

Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0