LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 06-20-2011, 11:18 PM   #1
smart1seo
LQ Newbie
 
Registered: Sep 2010
Location: NYC
Posts: 4

Rep: Reputation: 0
bash script- to read file name with two digits


I have many files with name in order of number.
e.g)
u0101.asc
u0102.asc
u0103.asc
.
.
u0110.asc
u0111.asc
.
.


I am trying to read file using for loop.

for ((date=01; date<=31; date++))
do
echo ${date}
done


but '01' is read(print) as '1'
How can I make it read from '1' to '01'??
 
Old 06-21-2011, 12:33 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,422

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
Well you would need to use printf to get the leading 0 to appear in output.
However, if the question is how to cycle through your files with the for loop:
Code:
for x in u*.asc
do
    do_your_stuff_here
done
 
1 members found this post helpful.
Old 06-21-2011, 01:02 PM   #3
smart1seo
LQ Newbie
 
Registered: Sep 2010
Location: NYC
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Well you would need to use printf to get the leading 0 to appear in output.
However, if the question is how to cycle through your files with the for loop:
Code:
for x in u*.asc
do
    do_your_stuff_here
done
I could't quite done what I really need to do. If you can help me....

I need to remove text in the file(First 5 lines) for each existing file and save as same name.
The name of files are in order of date and hour. e.g) u0101.asc, u0102.asc... u0110.asc ..u0212.asc...u3123.asc...
So, I used 'tail' command ( I need last 94 lines)

for ((date=01; date<=31; date++))
for ((hour=00; hour<=23; hour++))
do
tail -94 directory/u${date}${hour}.asc > directory/u${date}${hour}.asc
done


But the variables ${date}and${hour} are recognized as one digit '1' instead of '01'.
Please help me to solve this case.
 
Old 06-21-2011, 01:16 PM   #4
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
for i in `ls u01*`
do
j=`cat $i|wc -l`
k=$j-5
cat $i|tail $k>temp
mv temp $i
done

Not Tested.. Just wrote it on the fly.. Please test it and modify accordingly.
variable j will have the number of lines in the file.
variable k will have the number deducted by 5(or whatever you need).
temp file will have temp content.
then move it to the actual name.
 
Old 06-21-2011, 01:32 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
As previously mentioned use the printf command to retrieve leading zeros:
Code:
for ((date=1; date<=31; date++))
do
  for ((hour=0; hour<=23; hour++))
  do
    echo u$(printf %02d $date)$(printf %02d $hour).asc
  done
done
Newer versions of bash (version 4) can do this by means of the enhanced extended brace expansion, that now accepts zero-padding:
Code:
for date in {01..31}
do
  for hour in {00..23}
  do
    echo u$date$hour.asc
  done
done

Last edited by colucix; 06-21-2011 at 01:35 PM.
 
Old 06-21-2011, 07:15 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
As with Grail on post #2, if you're just cycling through the files, you don't need to know what the nums mean, just process each file.
Re post #4; bash arithmetic http://tldp.org/LDP/abs/html/arithexp.html

In fact, you should bookmark these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 06-22-2011, 01:40 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,422

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
I am not even sure you need the loop structure at all:
Code:
sed -i '1,5 d' u01*
 
  


Reply


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 read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 07:53 AM
BASH - convert single digits to double digits. rickenbacherus Programming 7 05-07-2008 07:53 AM
bash script to preserve only first digits alenD Linux - Software 6 04-14-2008 01:51 PM
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 02:57 AM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 12:40 PM


All times are GMT -5. The time now is 12:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration