Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assigning a field value to a variable

Status
Not open for further replies.

colrad

Technical User
Mar 17, 2002
2
GB
I have a log file which stores information on files that have been put into a "recycle-bin".
When files are placed in the bin they are assigned a number as there name. So the info stored is number, path, original name.
I want to be able to use the info in the log file to restore or delete these files.
I have been trying to assign field values to variables without success.
Any help appreciated

colrad
 
I not sure I fully understand what your after, but I'll have a go

a=`ls -al|awk '{print $1}'`
b=`ls -al|awk '{print $2}'`
c=.... and so on.

then just do

rm $c

or the like.

Please give me more info if I've got hold of the wrong end of the stick.

Mike --
| Mike Nixon
| Unix Admin
| ----------------------------
 
Yes, awk should work.
Awk defaults to blanks spaces or tabs
as field seperators.

You can also specify field seperators in awk
depending on the format of the file you are reading.

For example:

echo "Name Phone Number Address"|awk '{print $2}'
would return
Phone

echo "Name:phone Number:Address"|awk -F: '{print $2}'

would return
Phone Number

Robert



Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Hi colrad,

The followinfg script is an example of what you can do :

# Recycle_Bin = Recycle-bin directory
# LogFile = The log file containing informations
exec 3<&0 # Save stdin
exec 0<$LogFile # Log file is now stdin
while read Number Path Original
do
echo &quot;&quot;
echo &quot;recycle-bin $Number was $Path/$Original&quot;
echo &quot;(R)estore (D)elete (K)eep [Def:K] ? &quot;
read Action <&3
case ${Action:-K} in
[Rr]*) mv $RecycleBin/$Number $Path/$Original ;;
[Dd]*) rm $RecycleBin/$Number ;;
[Kk]*) echo &quot;Keep ...&quot; ;;
*) echo &quot;Invalid choice: <$Action>. No action.&quot; ;;
esac
done


If the field separator in your logfile isn't space or tab, you must set the variable IFS with the separator chacter.
If the field separator is &quot;,&quot; (comma) :

OldIFS=$IFS
IFS=','
while read Number Path Original
do
. . . . . . . .
done
IFS=$OldIFS
Jean Pierre.
 
I'd like to thamk everybody for their help.
I'm new to this - especially awk

I've managed to do most of what I wanted thanks to your sample scripts.
could I ask one more thing?
Below is a sample of my index file

1 /home/smith sample
2 /home/bin/test file1
3 /home/bin file2

I can search by filename (Column 3) by entering all or part of a file name, for example
search fi This would output
file1
file2
I want to be able to enter search * to list all files.
Is there a way of doing this?

Thanks again for all your help
 
echo &quot;Enter file to search \c&quot;
echo &quot;--> \c&quot;
read FILE_TO_SEARCH

echo &quot;Enter search string \c&quot;
echo &quot;--> \c&quot;
read SEARCH_STRING

if [ &quot;$FILE_TO_SEARCH&quot; = &quot;*&quot; ]
then
cat $INDEX|while read FILE
do
grep $SEARCH_STRING $FILE
done
else
grep $SEARCH_STRING $FILE_TO_SEARCH
fi

Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top