That works nicely!
Any idea how to make it so that it starts reading/printing on the third line?
Main Topics
Browse All TopicsI have a file with the following format:
d 28854 cpmnt open username owner 3 062 description of problem
d 28855 cpmnt open username owner 3 062 description of problem
d 28859_d cpmnt open username owner 3 062 description of problem
I need to "grab" the second token of every line, no matter how large the token is. The 2nd token is characterized by whitespace delimerters on both sides. (generally spaces, but I would like to be able to handle tabs as well).
I would like generate a new file and insert onto each line the 2nd token from this first file.
This is a fun programming excercise. I have been working on it for a little while now, but just do not know the commands wlel enough to be able to get it where i need it to be.
The faster the response on this, the more points I will add to the question, and the better grade I will offer. Please help!
Here is the relevant portion of the script that I have so far (not a big deal at all)
It simply reads in the file line by line and outputs it to the terminal
# START OF SCRIPT
exec < currentDefectsLog
while read line
do
echo $line
done
#END OF SCRIPT
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Don't know that this is the best way but you could set an accumulator and increment it
x=1
exec < currentDefectsLog
while read line
do
if [ $x -gr 1 ]
then
echo $line | awk '{ print $2 }'
fi
x=(($x + 1))
done
that will get you to start at line 2
Then to stop at the second line from the end we can do this
filelinecount=` wc - l | cat currentDefectsLog`
then inside the while we can do this
exec < currentDefectsLog
while read line
do
if [ $x -gr 1 ]
then
echo $line | awk '{ print $2 }'
fi
if [ $x -eq (($filelinecount - 2)) ]
then
exit 0
fi
x=(($x + 1))
done
put it all together and I think that will take care of it.
Let me know if you require additional help.
Paul
That is the way I check my directories.
One thing I have learned though is there must be spaces between the braces and the test cirteria:
[ ! -d directory ]
If there are no spaces, things tend not to work.
Oh and another thing, in the code I gave you above you might need to change it a bit. I think the x=(($x + 1)) should probably be x=$(x + 1). Try it both ways.
Paul
Thank you for your continuous help. I will add more points as soon as soon as a refund comes in from an unanswered question.
How would I make the script do the following:
read a file line by line where each line is a single token of alphanumeric characters, and "_" characters
**trim white spaces from line**
if line is NOT blank then
if directory /defects/$line exists then
delete /defects/$line
fi
fi
done
I tried doing this:
#---start of script
while read line
do
line= echo $line | awk '{print $0}'
if [ -n $line ]
then
if [ -d "/defects/$line" ]
then
rm /defects/$line -f -r
fi
fi
done
#--- end of script
it almost works - except one thing. if it encounters a line with just whitespace on it, it will delete the /defects directory - when I never want that deleted as it is. i want to skip all blank lines.
ill have a shorter one for you
#!/bin/bash
#start
#Usage ./scriptname <file>
#Gives you the total number of lines in the file
nrow=`wc -l $1 | awk '{print $1}'`
#Nr or rows to tail the file with all rows - 3
rowtail=$(($nrow - 3))
#Nr or rows to head the file with all rows - 2
rowhead=$(($rowtail - 2))
#Output the right fields
tail -n $rowtail $1 | head -n $rowhead | awk '{ print $2 }'
#end
/Rob
Business Accounts
Answer for Membership
by: PaulS_IIIPosted on 2004-05-04 at 08:51:20ID: 10987933
try:
echo $line | awk '{ print $2 }'
That should work for ya
Paul