Read file from input and redirect to output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read file from input and redirect to output file
# 1  
Old 08-21-2013
Read file from input and redirect to output file

Hi ,

i am having an file which contains 5 file_name data, i need to read the file name and will perform certain operation and generate out file names with named as 5 individual file_names
for eg:
Code:
file.txt contains
file_name1.txt|hai
file_name2.txt|bye
file_name3.txt|how
file_name4.txt|are
file_name5.txt|you

i will read using 
for i in `cat file.txt`
and do certain operations like

sed '$d' file_name1.txt >file_name1.tmp
echo "read the second row from file.txt">>file_name1.tmp
mv file_name1.tmp file_name1.txt

like the above i need to perform for all files and my output files should be
file_name1.txt
.
.
.
.
last line to print hai

file_name2.txt
.
.
.
bye

like this i need 5 individual files

# 2  
Old 08-21-2013
you shouldn't read like that at all.

start with

Code:
while IFS='|' read -r filename data; do
  ...
done < "file.txt"

and you'll have "$filename" and "$data" to use as you need
# 3  
Old 08-22-2013
how can i print the second row of my file.txt in echo using while

Code:
for eg 
echo"hai">>file_name1.tmp

# 4  
Old 08-22-2013
As neutronscott suggested, you may use the variables filename and data in the while loop.

Within the while loop you can refer to the filename using $filename and data using $data.

During first iteration, variables filename and data will contain
Code:
filename=file_name1.txt
data=hai

During second iteration, variables filename and data will contain
Code:
filename=file_name2.txt
data=bye

and so on.

So, the while loop takes care of reading the file file.txt line by line. You need not worry about reading the second line.
This User Gave Thanks to krishmaths For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - Read input from file and output CSV i same row

Hello I have the following output and want the output to look: FROM: GigabitEthernet0/0 is up, line protocol is up 1 input errors, 0 CRC, 0 frame, 1 overrun, 0 ignored 275 output errors, 0 collisions, 3 interface resets GigabitEthernet0/1 is up, line protocol is up 0... (4 Replies)
Discussion started by: JayJay2018
4 Replies

2. UNIX for Dummies Questions & Answers

Redirect output to the same input file in awk

Hi, I want to compare a value from test file and redirect the o/p value to the same file input file 250 32000 32 128 Below is my code awk '{ if ($1 < "300") print $1 > /tmp/test}' test want to compare 250 < 300 then print 300 to the same place below is the... (24 Replies)
Discussion started by: stew
24 Replies

3. 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

4. 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

5. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

6. Shell Programming and Scripting

How to redirect a input of find command into a text file

Hello friends, I want a command to print the reult files from find command into a text file.:) Iam looking from forum memebers. PLZ help me.ASAP Thanks in Advance, Siva Ranganath CH (5 Replies)
Discussion started by: sivaranga001
5 Replies

7. UNIX for Advanced & Expert Users

Complex Input/Output Redirect

Hi All, Sorry if the title is not good but I did not know how to explain with only some words! What I meant is: I have a unix command built from a private application vendor that when executed it prompts for two entries by the keyboard, let's say, for example: ... (1 Reply)
Discussion started by: felipe.vinturin
1 Replies

8. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

9. Shell Programming and Scripting

Input file redirect in output path and want name as inputfilename_new.txt

not required this time (6 Replies)
Discussion started by: Sandeep_Malik
6 Replies

10. UNIX for Dummies Questions & Answers

redirect input from file?

I need to run a command inside root-environment, and want to use: su - root -c "some command..." Then I am prompted for a password, of course. Is it possible to put this password in a file, and present this files content, to the command above?:confused: (1 Reply)
Discussion started by: bjornrud
1 Replies
Login or Register to Ask a Question