Hi ,
i am having an file which contains 5 file_name data, i need to read the file name and will perform certain operation and generate out file names with named as 5 individual file_names
for eg:
file.txt contains
file_name1.txt|hai
file_name2.txt|bye
file_name3.txt|how
file_name4.txt|are
file_name5.txt|you
i will read using
for i in `cat file.txt`
and do certain operations like
sed '$d' file_name1.txt >file_name1.tmp
echo "read the second row from file.txt">>file_name1.tmp
mv file_name1.tmp file_name1.txt
like the above i need to perform for all files and my output files should be
file_name1.txt
.
.
.
.
last line to print hai
file_name2.txt
.
.
.
bye
like this i need 5 individual files
you shouldn't read like that at all.
start with
while IFS='|' read -r filename data; do
...
done < "file.txt"
and you'll have "$filename"
and "$data"
to use as you need
how can i print the second row of my file.txt in echo using while
for eg
echo"hai">>file_name1.tmp
As neutronscott suggested, you may use the variables filename and data in the while loop.
Within the while loop you can refer to the filename using $filename and data using $data.
During first iteration, variables filename and data will contain
filename=file_name1.txt
data=hai
During second iteration, variables filename and data will contain
filename=file_name2.txt
data=bye
and so on.
So, the while loop takes care of reading the file file.txt line by line. You need not worry about reading the second line.
1 Like