# Automatic file input to code

**URL:** <https://community.unix.com/t/automatic-file-input-to-code/254440>\
**Category:** Shell Programming and Scripting\
**Created:** [November 25, 2009, 7:14am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440 "2009-11-25T07:14:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maruti](https://community.unix.com/letter_avatar/maruti/32/6_22734026db12803ebb3e059622ac3534.png) [@Maruti](https://community.unix.com/u/Maruti)\
**Post date:** [November 25, 2009, 7:14am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/1 "2009-11-25T07:14:04Z")

</div>

i have got many files like this in my folder temp(say)  
imp\_02042008.txt for date 02-04-2008  
imp\_03092009.txt for date 03-09-2009  
imp\_25112009.txt for date 25-11-2009  
...................  
........  
in some folder.  
and one of my shell code uses one of the above files based on date.  
for example for todays date it should pick the file imp\_25112009.txt  
for tomorrows date it should pick the file like imp\_26112009.txt  
My code is like below one:  
print substr($1,4,3),"",substr($2,22,8) inputfile.txt\>outputfile.txt

This code should run on day to day basis.  
What i want is a code to pick up the file (eg..imp\_26112009.txt) automatically from folder temp based on day.. i.e, for a given day particular file should be taken as input in my code...  
pls help me in this regard..

---------- Post updated at 07:14 AM ---------- Previous update was at 07:02 AM ----------

pls any coders do look into matter... m novice to shell scripting...

---

<div class="post-metadata">

**Author:** ![thegeek](https://community.unix.com/letter_avatar/thegeek/32/6_22734026db12803ebb3e059622ac3534.png) [@thegeek](https://community.unix.com/u/thegeek)\
**Post date:** [November 25, 2009, 8:24am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/2 "2009-11-25T08:24:09Z")

</div>

in bash, it is one of the simplest way to form your file name ...

```nohighlight
todaydate=`date +%d%m%Y`
echo $todaydate
file=imp_$todaydate
echo $file

```

output:

```nohighlight
25112009
imp_25112009

```

if you want to run it daily automatically, call it normally from cron.

---

<div class="post-metadata">

**Author:** ![Maruti](https://community.unix.com/letter_avatar/maruti/32/6_22734026db12803ebb3e059622ac3534.png) [@Maruti](https://community.unix.com/u/Maruti)\
**Post date:** [November 25, 2009, 10:06am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/3 "2009-11-25T10:06:47Z")

</div>

what i want is to pick up file from the temp directory if its todays file.that file will act as input to my code... I already have files in the given form.. i need some code that will search the file(of current date.. i.e for today i need to pick file imp\_25112009) from temp directory and place it as input to my code...

wat m searching is code which contains sed or grep or awk...etc something like that...

sorry for disturbance... n thank you for ur help... 🙂

---

<div class="post-metadata">

**Author:** ![steadyonabix](https://community.unix.com/letter_avatar/steadyonabix/32/6_22734026db12803ebb3e059622ac3534.png) [@steadyonabix](https://community.unix.com/u/steadyonabix)\
**Post date:** [November 25, 2009, 12:54pm UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/4 "2009-11-25T12:54:42Z")

</div>

Do you mean like this: -

```nohighlight
nawk ' 1 ' imp_$(date +%d%m%Y) > outputfile.txt

```

---

<div class="post-metadata">

**Author:** ![Maruti](https://community.unix.com/letter_avatar/maruti/32/6_22734026db12803ebb3e059622ac3534.png) [@Maruti](https://community.unix.com/u/Maruti)\
**Post date:** [November 27, 2009, 1:46am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/5 "2009-11-27T01:46:46Z")

</div>

thank you for your help. but my requirement has slightly changed..i have got directory home/ext/eca/pool that contains  
below kind of files...

imp\_05172\_02042008\_124956\_02042008\_123.txt

imp\_01342\_02042008\_124956\_02042008\_128.txt

imp\_02229\_25112009\_124956\_25112009\_222.txt

imp\_02347\_26112009\_124956\_26112009\_332.txt  
.....  
....

what i want is that my code should pick the file corresponding to current date i.e for today it should pick the file imp\_02347\_26112009\_124956\_26112009\_332.txt  
here the numbers before&after 26112009 doesnt make any difference to me..

i tried following code:

cat \</dev/null \>inputtomycode.txt;

awk '$0 ~ /^imp\_$(date +%d%m%Y) {print $0}' /home/ext/eca/pool\> inputtomycode.txt

but its not working properly...some loopholes in code..pls help to patch up the code...

by the way is there any way how to make my code run regularly at specific time..(say my code file name is reg.txt) ..?

Is cron will suffice my need....

---------- Post updated 11-27-09 at 01:46 AM ---------- Previous update was 11-26-09 at 07:57 AM ----------

anybody pls look into above post

---

<div class="post-metadata">

**Author:** ![gcampton](https://community.unix.com/user_avatar/community.unix.com/gcampton/32/1727_2.png) [@gcampton](https://community.unix.com/u/gcampton)\
**Post date:** [November 27, 2009, 2:02am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/6 "2009-11-27T02:02:52Z")

</div>

You might want to try to explain what you mean by things like "pick up the file". Instead use terminology that most people generally understand for instance:

"I want to read the file1 that corresponds to todays date in folder tmp, and input that file into file2.... file2 is located in a different subdirectory, and I need this to run automated. eg. using cron."

rather than "pick up" use terminology like "read or write" "input or output"

---

<div class="post-metadata">

**Author:** ![Maruti](https://community.unix.com/letter_avatar/maruti/32/6_22734026db12803ebb3e059622ac3534.png) [@Maruti](https://community.unix.com/u/Maruti)\
**Post date:** [November 27, 2009, 2:15am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/7 "2009-11-27T02:15:43Z")

</div>

ok... you have you have described wat my requirement is... thanQ

---

<div class="post-metadata">

**Author:** ![gcampton](https://community.unix.com/user_avatar/community.unix.com/gcampton/32/1727_2.png) [@gcampton](https://community.unix.com/u/gcampton)\
**Post date:** [November 27, 2009, 9:24am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/8 "2009-11-27T09:24:56Z")

</div>

unfortunately, I don't know unix programs enough to help I was simply trying to help you get a result, otherwise people generally read through a topic, try to help but get frustrated because they can't understand what the OP is trying to accomplish or what their needs are, and so they unsubscribe and don't bother replying or continuing to help.

However you have been given some guidelines already, you don't need to worry about the automate processes just yet, figure it out until the script works then you can read up on how to use cron: "man cron"

did you play with steady's example?

```nohighlight
nawk ' 1 ' imp_$(date +%d%m%Y) > outputfile.txt

```

---

<div class="post-metadata">

**Author:** ![steadyonabix](https://community.unix.com/letter_avatar/steadyonabix/32/6_22734026db12803ebb3e059622ac3534.png) [@steadyonabix](https://community.unix.com/u/steadyonabix)\
**Post date:** [November 27, 2009, 1:33pm UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/9 "2009-11-27T13:33:05Z")

</div>

```nohighlight
ls | xargs -n1 | nawk '/imp_[0-9]*_'$(date +%d%m%Y)'_[0-9]*_'$(date +%d%m%Y)'_[0-9]*.txt/' /home/ext/eca/pool> inputtomycode.txt

```

---

<div class="post-metadata">

**Author:** ![Maruti](https://community.unix.com/letter_avatar/maruti/32/6_22734026db12803ebb3e059622ac3534.png) [@Maruti](https://community.unix.com/u/Maruti)\
**Post date:** [November 28, 2009, 1:44am UTC](https://community.unix.com/t/automatic-file-input-to-code/254440/10 "2009-11-28T01:44:30Z")

</div>

thanQ guys..finally its working..
