LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-07-2009, 06:05 PM   #1
neo009
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Rep: Reputation: 0
Smile 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
 
Old 04-09-2009, 03:03 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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?
 
Old 04-09-2009, 03:05 AM   #3
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware
Posts: 575

Rep: Reputation: 154Reputation: 154
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"
 
Old 04-10-2009, 11:57 AM   #4
neo009
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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.
 
Old 04-10-2009, 12:00 PM   #5
neo009
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ChrisAbela View Post
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.
 
Old 04-12-2009, 07:07 AM   #6
neo009
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neo009 View Post
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.
 
Old 04-12-2009, 07:49 AM   #7
neo009
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neo009 View Post
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...
 
  


Reply

Tags
cut, file, lines, paste, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
can't put an if statement as an increment in a for loop. Why? japhy Programming 2 02-07-2009 07:07 AM
Piping into a Case Statement embedded in a while loop telecom_is_me Programming 5 07-02-2008 05:45 PM
shell script loop though text file:-1 line at a time. knockout_artist Linux - Newbie 2 05-04-2008 07:58 PM
BASH Need help figuring out how to loop SELECT statement mcdrr Programming 2 05-08-2007 05:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration