简体   繁体   English

如何在Shell脚本中读取文件并将文件从一个文件复制到另一个文件

[英]How to read a file and copy from one file to another file in shell script

How to read a file and copy from one file to another file in shell script: 如何在shell脚本中读取文件并将文件从一个文件复制到另一个文件:

#!/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. 实际上,这里有一个文件夹试用版,在试用文件夹中有4个文本文件。 I want to open a file-'file.txt'. 我想打开一个文件“ file.txt”。 Inside file.txt 3 files names are there: test1.txt, test2.txt, test3.txt. 在file.txt内部,有3个文件名: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. 我的工作是使用file.txt文件,我已读取所有3个文件名并将其复制到另一个文件夹。 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. 因此,为此,我必须打开file.txt,读取文件并打印3个文件,然后仅复制这3个文件而不是完整文件夹,并将这3个文件复制到同一目录中的另一个文件夹“文件”中。

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.. 尝试一下,这里test1是您的源文件夹,它将占用您的文件,而test2是目标文件夹,您将在读取后移动文件。

#!/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... 哟...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Shell脚本将可变数量的行从一个文件复制到另一个文件? - How to I copy a variable number of lines from one file to another using shell script? 使用 shell 脚本将特定单词从一个文件复制到另一个文件 - Copy specific word from a file to another file using shell script Shell脚本从一个文件读取值并将其替换为另一文件 - shell script to read values from one file and replace it in other file 如何使用 shell 脚本读取文件并写入另一个文件 - How to read a file and write in to another file using a shell script 如何使用Linux Shell脚本读取文本文件列并将文件复制到子目录中的另一个路径 - How to read the column of text file using Linux shell script and copy the files to another path with in sub directories 如何在Shell脚本中读取文件 - How to read a file in shell script 如何从文件中读取特定字符串并将整行保存在 Bash Shell 脚本中的另一个文件中 - How to read a specific string from a file and save the whole line in another file in Bash Shell script Shell脚本从文件读取值并将它们与另一个值进行比较 - Shell script to read values from a file and to compare them with another value 如何从另一个Shell脚本运行一个Shell脚本,该脚本从CSV文件中读取第一个Shell脚本名称 - How to run a shell script from another shell script reading first shell script name from a CSV file 从配置文件中的Shell脚本读取变量 - Read variable from a shell script in config file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM