LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash: read a line from file (http://www.linuxquestions.org/questions/linux-general-1/bash-read-a-line-from-file-107803/)

zokik 10-23-2003 08:04 PM

bash: read a line from file
 
I need to copy a lot of files, all are listed in a text file, one filename (with full path) per line.

What I'm missing is a command or something that would read that file line by line so I could make a loop and pass filename (i.e. current line read) to the cp command.

BTW, is there a C or C++ function for copying files, or is that usually done with system("cp whatever whatever") or even reading source file, creating and writing to outfile? I'm more familiar with C than bash scripting, that's why I thought of that, but I'd prefer a more elegant solution.

J_Szucs 10-23-2003 08:17 PM

Are there spaces in the full filenames?

If not, this should work:
for filename in `cat /path/to/filelist` ; do
cp $filename /path/to/new/location/
done

Are there hundreds of files? If not, this would work and is more simple:
cp `cat /path/to/filelist` /path/to/new/location/

Notes:
- in both cases, this kind of copying will not preserve directory structure, all files will be copied into one single directory, so, if there are files with the same short name (i.e. without path), they will owerwrite each other
- if there may be spaces in filenames, the above examples will not work.
So watch out.

zokik 10-23-2003 08:33 PM

Unfortunately there are spaces in filenames. In fact, I don't think there is even a single filename without spaces :(

Quote:

Are there hundreds of files?
Well, not literally ;) . There are *thousands* of them. That's why they can't be passed to cp all at once.

One more thing: how do I detect the suffix of file? I need something like this:
if [filename = *.txt]; then
...
else
...
fi

The syntax in the if sentence test condition is probably incorrect, how do I do it right?

lenlutz 10-23-2003 09:35 PM

try this (if i understand your question)

while read i
do
echo $i # or whatever you want
done < filename

zokik 10-23-2003 10:02 PM

Thanks, that works perfect! Do you also know how could I check the suffix?

zokik 10-24-2003 01:57 PM

Nevermind, I managed to check the suffix with this:

if [ "$(basename "$i" .txt)" != "$(basename "$i")" ]; then
echo File \"$i\" has suffix .txt

Good thing I tok another look at man basename :)

dreidu 12-10-2008 09:24 AM

sorry for the digging butt I'm having a similar problem

i need to read a file line at line, and echo the line itself,
i know it's stupid but for now is what i want for the big picture.

i tried something like this

for filename in `cat file` ;
do
echo $filename
done

and it seems to print every word in the file, separating them by spaces, not by new lines.

i will need this because i'm trying to compare 2 files, that have a line that starts with a number, so i can match it on both files.

for now my code is something like this

[bin]# vi compare.sh

Quote:

#!/bin/bash
# global vars
mallocs=$1
frees=$2
tempValue=0

# function to validate mtu size
function compareFiles
{

exec<$mallocs
while read line
do
tempValue=`expr $tempValue + 1`;
echo $tempValue;
done
echo "**** $tempValue";

# go to malloc file, open it and read each line and get the number
# for i in $`cat $mallocs`;
# do
# echo "-> $i "
# done
# match the number with second file
# not found ? print to bash
# found, go to next number in mallocs file
}

# script sequence
if [ $# -lt 2 ]; then
echo "Invalid number of parameters. Required 2 parameters"
echo "parameter 1: filename of mallocs"
echo "parameter 2: filename of frees"
else
compareFiles
fi
edit:

it must have been a stupid error, because now it works fine

final result

Quote:


#!/bin/bash
# global vars
mallocs=$1
frees=$2

# function to validate mtu size
function compareFiles
{
# go to malloc file, open it and read each line and get the number
exec<$mallocs
while read line
do
#mytemp will store the number in the line, we know the number is in the first position
MYTEMP=$(echo $line | cut -f 1 -d ' ')
#retvalue will store the result of the comparisson between
#mytemp and all the values in the free files
RETVALUE=`cat $frees | grep $MYTEMP`
if [ "$RETVALUE" == "" ]; then
# not found means it's not on both files, print it
echo $MYTEMP
fi
done
}

# script sequence
if [ $# -lt 2 ]; then
echo "Invalid number of parameters. Required 2 parameters"
echo "parameter 1: filename of mallocs"
echo "parameter 2: filename of frees"
else
compareFiles
fi





All times are GMT -5. The time now is 07:08 AM.