LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-23-2003, 08:04 PM   #1
zokik
Member
 
Registered: Dec 2002
Distribution: SuSE 8.2
Posts: 58

Rep: Reputation: 15
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.
 
Old 10-23-2003, 08:17 PM   #2
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
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.

Last edited by J_Szucs; 10-23-2003 at 08:25 PM.
 
Old 10-23-2003, 08:33 PM   #3
zokik
Member
 
Registered: Dec 2002
Distribution: SuSE 8.2
Posts: 58

Original Poster
Rep: Reputation: 15
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?
 
Old 10-23-2003, 09:35 PM   #4
lenlutz
Member
 
Registered: May 2003
Location: philadelhpia pa
Posts: 92

Rep: Reputation: 15
try this (if i understand your question)

while read i
do
echo $i # or whatever you want
done < filename
 
Old 10-23-2003, 10:02 PM   #5
zokik
Member
 
Registered: Dec 2002
Distribution: SuSE 8.2
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks, that works perfect! Do you also know how could I check the suffix?
 
Old 10-24-2003, 01:57 PM   #6
zokik
Member
 
Registered: Dec 2002
Distribution: SuSE 8.2
Posts: 58

Original Poster
Rep: Reputation: 15
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
 
Old 12-10-2008, 09:24 AM   #7
dreidu
LQ Newbie
 
Registered: Sep 2008
Posts: 6

Rep: Reputation: 0
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



Last edited by dreidu; 12-10-2008 at 09:48 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 12:06 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 02:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration