Hi,
i have doubt in read and write using bash shell script...
i will give the input file path and output file path in command line
ex : example.sh iputfile outpilepath
Here i need to read the input data then write that data to output file..
please give some example :
You can use the following code.
Give the command line argument like : script_name.sh file1 file2
infile=$1
outfile=$2
while read line
do
echo $line >> "$outfile"
done < "$infile"
Now it will read the file1 contents and writes to file2
need one more help...
if input has 3 lines
example :
- name1 aa bb cc
- name2 aa ff dd
- name3 ii kk lll
command :
example.sh inputfile outputfile searchdata
searchdata is name2 means
i want to write the 2nd line only to output file...
Don't post your home works here , try to do by your self .If there is any problem you can ask here .
infile=$1
outfile=$2
search_file=$3
count=1
while read line
do
echo "$line" >> "$outfile"
if [ $count -eq 2 ]
then
echo $line >> "$search_file"
fi
let count=count+1
done < "$infile"
I have made the changes as marked above.
Now it will write only the second line to the outfile
infile=$1
outfile=$2
new=$3;
var=0;
while read line
do
let var=var+1
if [ $var -eq 2 ]
then
echo $line >> "$new";
fi
echo $line >> "$outfile"
done < "$infile"