LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Defining loop statement for creating new file each time (https://www.linuxquestions.org/questions/linux-newbie-8/defining-loop-statement-for-creating-new-file-each-time-717537/)

neo009 04-07-2009 05:05 PM

Defining loop statement for creating new file each time
 
Hi, I am scripting new bee here please can anyone guide me i want to create a simple script which will redirect data from input file and while redirecting will check for output file if exist then will create new like if test1 there then test2 ..test3 so on...
here is my script i dont know how feasible it is..
#!/bin/bash
echo "Enter the file to be used: \c"
read flname
echo "searching for file $flname"
grep "$pname" $flname
echo "Selected records name shown above"
if [ ! -f $flname ];
then
echo "File $flname does not exists! Please provide the exact path"

else
echo "File exist using input from $flname"
fi
if [ -e $flname ];
then
echo "Enter first line number to start input data"
read n
echo "Enter last line number to input data"
read m
else
"Provide exact number of lines"
fi
test=${n}test
if [ -e $nline ];
then
cut -c $n-$m $flname > $test
else
echo "can'not input data please put right info"
fi

if [ -e $test ]; then
echo "File $test exist creating new `touch ${n}test`"
else
echo "File does not exists"
fi
I want here some statement which will loop and create new file..each time

colucix 04-09-2009 02:03 AM

It is not really clear what you're trying to do. Can you post an example of input file and what an output file should be based on the user's input? Anyway, here is some correction to your script:
Code:

#!/bin/bash
read -p "Enter the file to be used: " flname
echo "searching for file $flname"
# What is variable pname here?
grep "$pname" $flname
echo "Selected records name shown above"

if [ ! -f $flname ]
then
  echo "File $flname does not exists! Please provide the exact path"
else
  echo "File exist using input from $flname"
  # Here inserted statements from the next test, not necessary
  # since the condition is the opposite of the previous one
  # so it is implicit in this else statement

  read -p "Enter first line number to start input data: " n
  read -p "Enter last line number to input data: " m
fi

test=${n}test

# What is variable nline here?
if [ -e $nline ];
then
  cut -c ${n}-${m} $flname > $test
else
  echo "cannot input data please put right info"
fi

if [ -e $test ]; then
  # this is not clear: if file $test exist maybe you want to create
  # another one with a different name, but you touch the same file

  echo "File $test exist creating new"
  touch ${n}test
else
  echo "File does not exists"
fi

What part of the code do you want to loop over? Do you want to process multiple input file or the same file by selecting different records?

ChrisAbela 04-09-2009 02:05 AM

No need for loops if we limit to test9

$ num=$( ls test? | tail -1 | sed 's/test//' )
$ num=$(( num+1 ))

The file name is

$ test=test"$num"

neo009 04-10-2009 10:57 AM

Quote:

Originally Posted by colucix (Post 3503225)
It is not really clear what you're trying to do. Can you post an example of input file and what an output file should be based on the user's input? Anyway, here is some correction to your script:
Code:

#!/bin/bash
read -p "Enter the file to be used: " flname
echo "searching for file $flname"
# What is variable pname here?
Sorry not removed that there should be only $flname
grep "$pname" $flname
echo "Selected records name shown above"

if [ ! -f $flname ]
then
  echo "File $flname does not exists! Please provide the exact path"
else
  echo "File exist using input from $flname"
  # Here inserted statements from the next test, not necessary
  # since the condition is the opposite of the previous one
  # so it is implicit in this else statement

  read -p "Enter first line number to start input data: " n
  read -p "Enter last line number to input data: " m
fi

Yes you are right there i want first line number and then whatever the second line number,then want to cut those line and put into different new one...

test=${n}test

# What is variable nline here?
Sorry i forgot to remove that $nline here i want to check if $n and $m are there then just cut the lines and paste into new file.

if [ -e $nline ];
then
  cut -c ${n}-${m} $flname > $test
else
  echo "cannot input data please put right info"
fi

if [ -e $test ]; then
  # this is not clear: if file $test exist maybe you want to create
  # another one with a different name, but you touch the same file


Yes i think iam wrong here i want to create new file for example test1 or test2..whatever the next sequence...

  echo "File $test exist creating new"
  touch ${n}test
else
  echo "File does not exists"
fi

What part of the code do you want to loop over? Do you want to process multiple input file or the same file by selecting different
records?

yes you are right i want to input same file whatever it will be and selecting line from 1 to 100 or 10 will be pasted in new file which will be created new each time if exist.like if test1 there then test2 if test2 there then test3.

neo009 04-10-2009 11:00 AM

Quote:

Originally Posted by ChrisAbela (Post 3503227)
No need for loops if we limit to test9

$ num=$( ls test ? | tail -1 | sed 's/test//' )
$ num=$(( num+1 ))

The file name is

$ test=test"$num"

Hi this is great i understood the script,i am going to try this ,thank you so much.

neo009 04-12-2009 06:07 AM

Quote:

Originally Posted by neo009 (Post 3504731)
Hi this is great i understood the script,i am going to try this ,thank you so much.

Hi I got this working just i want to append date format while this will create new file in %Y%M%D format.

#!/bin/bash
echo "Enter the file to be used: \c"
read flname
if [ ! -f $flname ];
then
echo "File $flname does not exists! Please provide the exact path"
exit
else
echo "File exist using input from $flname"
fi
if [ -e $flname ];
then
read -p "Enter first line number to start input data: " n
read -p "Enter last line number to input data: " m

else
"Provide exact number of lines"
fi

num=1

while [ -e test$num ]; do
num=`expr $num + 1`
echo test_${num:1}
done
cut -c $n-$m $flname > test$num

if [ -e test$num ]; then
echo "File $test exist creating new test$num"
else
echo "File does not exists"
fi

just please guide me for appending date format...i dont want to use mv or paste..if file exist with date format then it should create new with %y%m%d-1 or %y%m%d-2 ....-3, -4 so on.

neo009 04-12-2009 06:49 AM

Quote:

Originally Posted by neo009 (Post 3506239)
Hi I got this working just i want to append date format while this will create new file in %Y%M%D format.

#!/bin/bash
echo "Enter the file to be used: \c"
read flname
if [ ! -f $flname ];
then
echo "File $flname does not exists! Please provide the exact path"
exit
else
echo "File exist using input from $flname"
fi
if [ -e $flname ];
then
read -p "Enter first line number to start input data: " n
read -p "Enter last line number to input data: " m

else
"Provide exact number of lines"
fi

num=1

while [ -e test$num ]; do
num=`expr $num + 1`
echo test_${num:1}
done
cut -c $n-$m $flname > test$num

if [ -e test$num ]; then
echo "File $test exist creating new test$num"
else
echo "File does not exists"
fi

just please guide me for appending date format...i dont want to use mv or paste..if file exist with date format then it should create new with %y%m%d-1 or %y%m%d-2 ....-3, -4 so on.

Hey I got it too..here is my script for appending date there..


#!/bin/bash
echo "Enter the file to be used: \c"
read flname
if [ ! -f $flname ];
then
echo "File $flname does not exists! Please provide the exact path"
exit
else
echo "File exist using input from $flname"
fi
if [ -e $flname ];
then
read -p "Enter first line number to start input data: " n
read -p "Enter last line number to input data: " m

else
"Provide exact number of lines"
fi

num=100
d=`date +%y-%m-%d`
while [ -e logfile.log-$d-$num ]; do
num=`expr $num + 1`
echo logfile.log-$d-${num:1}
done
cut -c $n-$m $flname > logfile.log-$d-$num


if [ -e logfile.log-$d-$num ]; then
echo "file exist created new"

else
echo "File does not exists"
fi


Thank you so much to both of you for helping me...


All times are GMT -5. The time now is 07:17 AM.