Get input from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get input from another file
# 1  
Old 02-28-2010
Get input from another file

Hi,

i have the below code which performs some action on the selected file(here it is doing on abc*.txt)
Code:
file=$(ls -tr abc*.txt | tail -1)
length=$(expr length "$file")
let date_len=$length-11
let text_len=$length-13
var_date=$(expr substr $file $date_len 8)
echo $var_date
v1=$(cat reporting_date.txt)
year=$(expr  substr $v1 7 4)
mon=$(expr substr $v1 1 2)
day=$(expr substr $v1 4 2)
v_rdate=$year$mon$day
echo $v_rdate
var_text=$(expr substr $file 1 $text_len)
var_newfile=$var_text.txt
if [ "$var_date" = "$v_rdate" ]
then
cp $file $var_newfile
else
echo "not equal"
exit
fi

i have another file(r.txt) in which different file name's is stored as shown below
Code:
abc_20091030.txt
dgt_20091030.txt
wqe_20091030.txt

i need to pass file name abc_20091030.txt as input to above script & then the script performs above operation.
Then dgt_20091030.txt as input to perform above operation.

so bascially i need to pass each filename as input from r.txt to above script.

can someone help me on this?

Thanks in advance.

Last edited by Scott; 03-01-2010 at 02:36 AM.. Reason: Please use code tags
# 2  
Old 02-28-2010
Quote:
Originally Posted by jagadeeshn04
...
i need to pass file name abc_20091030.txt as input to above script & then the script performs above operation.
Then dgt_20091030.txt as input to perform above operation.

so bascially i need to pass each filename as input from r.txt to above script.
...
Something like this ?

Code:
$ 
$ cat process.sh
#!/bin/bash
echo "You passed : $1"
$ 
$ cat r.txt
abc_20091030.txt
dgt_20091030.txt
wqe_20091030.txt
$ 
$ while read FILENAME
> do
>   ./process.sh $FILENAME
> done <r.txt
You passed : abc_20091030.txt
You passed : dgt_20091030.txt
You passed : wqe_20091030.txt
$ 
$

tyler_durden
# 3  
Old 02-28-2010
now
first make a file with starting three letters as

file1(r.txt)
Code:
abc
dgf
...
..

file2(script.sh)
Code:
file=$(ls -tr ${1}*.txt | tail -1)

now u want to pass each line of r.txt to the second file so

Code:
cat r.txt|xargs -i echo  script.sh {}

i think u get the basic idea now u can change it as you wish
if i am wrong im sorry as im also newbie to unix

Last edited by Scott; 03-01-2010 at 02:37 AM.. Reason: Code tags
# 4  
Old 03-01-2010
If you want to parse the files mentioned in another file via a shell script you can use the following way also.

Consider the files to parse are mentioned in the following file.
action:
a.test
b.test
c.test

The all test files are need to be processed and they are mentioned in the action file

Now I am passing the file action to my bash script. So that it will read all the files mentioned and start to parse
Code:
Filename=$1
for files_to_parse in `cat $Filename`
do
        echo "Parsing file \"$files_to_parse\" from $Filename"
done

Run this script as
script_name.sh action

The output will be as follows
Code:
Parsing file "a.test" from action
Parsing file "b.test" from action
Parsing file "c.test" from action

# 5  
Old 03-01-2010
Quote:
Originally Posted by thillai_selvan
...
Code:
...
for files_to_parse in `cat $Filename`
do
        echo "Parsing file \"$files_to_parse\" from $Filename"
done

...
This should be nominated for the Useless Use of Cat/Backticks Award :

Useless Use of Cat Award

tyler_durden
# 6  
Old 03-01-2010
How about this?
Code:
Filename=$1
while read files_to_parse
do
        echo "Parsing file \"$files_to_parse\" from $Filename"
done < $Filename

# 7  
Old 03-01-2010
Quote:
Originally Posted by thillai_selvan
How about this?
Code:
Filename=$1
while read files_to_parse
do
        echo "Parsing file \"$files_to_parse\" from $Filename"
done < $Filename

Awesome !

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

Avoid overwriting backup file when multiple entries need to replace in one file input from another

Hello, I have been working on script in which search and replace the multiple pattern. 1. update_params.sh read the multiple pattern from input file ParamMapping.txt(old_entry|New_entry) and passing this values one by one to change_text.sh 2. change_text.sh read... (0 Replies)
Discussion started by: ketanraut
0 Replies

3. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

5. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

6. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

7. Shell Programming and Scripting

XML variable for input in same input file

Dear All , i stuck in one problem executing xml .. i have input xml as <COMMAND name="ARRANGEMENT.WRITE" timestamp="0" so="initial"> <SVLOBJECT> <LONG name="CSP_PMNT_ID" val="-1"/> <MONEY name="CSP_CEILING" amount="0.0" currency="AUD"/> ... (6 Replies)
Discussion started by: arvindng
6 Replies

8. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

9. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

10. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies
Login or Register to Ask a Question