How to read a file and copy from one file to another file in shell script - linux

How to read a file and copy from one file to another file in shell script:
#!/bin/csh -f
echo ---file.txt---
cat file.txt
echo ######## file.text is opened ########
#set file_1="export/home/caratins/trial/file.txt"
while read line
do
echo "$line"
cp file.txt files
done<file.txt
Actually one folder trial is there, inside trial folder 4 text files are there. I want to open a file-'file.txt'. Inside file.txt 3 files names are there: test1.txt, test2.txt, test3.txt. My work is using file.txt file I have read all 3 files names and copy it to another folder. So for that I have to open file.txt, read the file and print 3 files and only copy these 3 files not full folder and copy these 3 files to another folder'files' which is in same directory.

if you want to copy entire file as it is then
cat filename >> newfilename
for three files
cat file1.txt file2.txt file3.txt >>file.txt
if you want to copy line by line then
while IFS= read -r line
do
echo "$line"
echo -e "$line\n" >>newfilename
done <"filename"

try this,
here test1 is you source folder, which will contail you files,
and test2 is destination folder where you will move your files after reading..
#!/bin/sh
cd test1;
echo "list of files:";
ls;
for filename in *;
do echo "file: ${filename}";
echo "reading..."
exec<${filename}
value=0
while read line
do
#value='expr ${value} +1';
echo ${line};
done
echo "read done for ${filename}";
cp ${filename} ../test2;
echo "file ${filename} moved to test2";
done
or you can try this...
ls;
echo "reading main file...";
filenames="filenames";
exec<${filenames}
while read name
do
echo "file: ${name}";
echo "reading..."
cd test1;
exec<${name}
value=0
while read line
do
#value='expr ${value} +1';
echo ${line};
done
echo "read done for ${name}";
cp ${name} ../test2;
cd ..;
echo "file ${file} moved to test2";
done
yo...

Related

Copy a folder contents and save the file with diff name Unix

I have a bunch of .txt files in a directory.
I m looking for a command to copy all .txt files and save it with <filename>_2.txt.
Eg: abc.txt -> abc_2.txt (After copy)
Thanks in tons in advance
EDIT: As per OP's extension request adding following code now.
for file in *.txt
do
if [[ ! -f "${file%.*}_MED_2.txt" ]]
then
cp "$file" "${file%.*}_MED_2.txt"
fi
done
Try following.
for file in *.txt
do
echo "cp $file ${file%.*}_2"
done
Above will print cp commands, if you are ok with them then run following.
for file in *.txt
do
if [[ ! -f "${file%.*}_2" ]]
then
cp "$file" "${file%.*}_2"
fi
done

Bash shell script file exists

Hi my bash script says the that the first parameter passed into it doesn't exist as a file but it does. Please what have I done wrong?
to run the script and pass in two arguments from linux:
./concantenatefile.sh file1.txt file2.txt
#!/bin/bash
file1="$1"
if [ -e file1 ]
then
echo "The file $file1 exists"
else
echo "The file $file1 doesn't exist"
fi
It says
The file file1.txt doesn't exist
However there is a file called file1.txt in the present working directory.
you are not currently derefencing the file1 variable
In the line
if [ -e file1 ]
You need to add a $ to derefence the variable and access its content as so
if [ -e $file1 ]

Need a solution for grep command for .gz files

I have file names starts with RACHEL_20180814_092356.csv.gz
and need to grep in format like RACHEL_20180814*.gz nd unzip it, but am unable too. Here is sample code I have been working on.need to also insert a date parameter, which changes with each day. tried using zgrep but I am out of luck! Any help please
Process GMRA file
echo "Starting file get for Rachel.gz files"
for SUBDIR in prices; do
set -A getlist `/bin/ls ${ENV_DIR_SCR}/bat/prices`
if [ ! -z ${getlist[0]} ]; then
for FILENAME in ${getlist[*]}; do
echo "Found File ${ENV_DIR_SCR}/bat/prices/${FILENAME}"
if [ `echo $FILENAME | grep "RACHEL*.gz"` ]; then
$FILENAME = gunzip $FILENAME
GETFILES="$GETFILES ${FILENAME}"
break
fi
done
fi
done echo "Completed file_get for RACHEL.gz files"

Unix script / replace string in file from variable and move into another directory

I'm trying to do a script which find specific string in .txt files and replace by variable. I don't want to change original files but do the copies and then move into another (specific) directory. Let's assume 3 .txt files:
files.txt,
file2.txt
file3.txt
First step is to find string "string1" and "string2" in all .txt files and do the copies (eg. tmp files).
Second is to replace a string by variable $1 and $2 (working on tmp files).
Then move all of them to 'directoryname' directory.
That's what I'v got:
#!/bin/bash
echo "$1 - first parameter"
echo "$2 - second"
configurer() {
for file in *.txt
do
echo "Processing file .... $file"
orig_file=$file
tmp_file=$orig_file.tmp
cp $orig_file $tmp_file
sed "s/string1/$1/g;s/string2/$2/g" $tmp_file
mv $tmp_file directorname/$orig_file
done
}
configurer
echo "Done ..."
It's almost correct, (correct move into another directory, do the tmp files), but sed function doesn't work as it should and I have no idea why. Could anyone take a look ?
Regards
try below sed, its always problem with sed with variable
sed -i -e 's/string1/'"$1"'/g' -e 's/string2/'"$2"'/g' $tmp_file
let me know if it works
for your piece of code
#!/bin/bash
echo "$1 - first parameter"
echo "$2 - second"
configurer() {
for file in *.txt
do
echo "Processing file .... $file"
orig_file=$file
tmp_file=$orig_file.tmp
cp $orig_file $tmp_file
sed -i -e 's/string1/'"$1"'/g' -e 's/string2/'"$2"'/g' $tmp_file
mv $tmp_file directorname/$orig_file
done }
configurer $1 $2
echo "Done ..."

Script is trying to move a directory instead of a file - need assistance

The script appears to be correct. However, after FTP'ing all the files in the directory, it gives me the error that it is trying to move a directory into a directory of itself.
Any ideas on why this is occurring?
mysql -u ????? -p????? -h ????? db < $SCRIPT_FOLDER/script.sql > script.xls
echo "###############################################################################"
echo "FTP the files"
#for FILE in `ls $SOURCE_FOLDER/`
for FILE in $SOURCE_FOLDER/*.xls
do
echo "# Uploading $SOURCE_FOLDER/$FILE" >> /tmp/CasesReport.copy.out
sshpass -p ???? sftp -oBatchMode=no -b - user#ftp << END
cd /source/directory/
put $SOURCE_FOLDER/$FILE
bye
END
echo "Moving $FILE to $SOURCE_FOLDER/history/"
mv $SOURCE_FOLDER/$FILE $SOURCE_FOLDER/history/$FILE
$FILE already contains $SOURCE_FOLDER, so you put command is doubling the path.
Example
$ cd /tmp
$ touch foo.txt bar.txt
$ cd
$ SOURCE_FOLDER=/tmp
$ for FILE in $SOURCE_FOLDER/*.txt; do echo "put $SOURCE_FOLDER/$FILE"; done
put /tmp//tmp/bar.txt
put /tmp//tmp/foo.txt
Inside the for loop, just use "$FILE"

Resources