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

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read each line from input file, assign variables, and echo to output file?
# 1  
Old 03-18-2014
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, assign sh variables to certain substrings, and then echo those values to an output file.

My script looks like this:
while read
VAR1=`awk '{print substr($0, 8, 6)}'`
VAR2=`awk '{print substr($0, 14, 2)}'`
VAR3=`awk '{print substr($0, 22, 4)}'`
echo XXX,ABCD,$VAR1,$VAR2,ZZ,$VAR3,HI,EX>>outfile1

But my output looks like this:
XXX,ABCD,123456 223456 323456,,ZZ,,HI,EX

I got one line of output. All of the values for $VAR1 were printed, while $VAR2 and $VAR3 were skipped. Instead I want one line of output per line of input. Thank you in advance.
# 2  
Old 03-18-2014
It is not clear what you really want.
But use awk only. awk -F '[ -]' '{awk code here}' infile gives you a way to define field separators, in the example space and hyphen.

awk -F '[ -]'  '{ print $2, $4, $5 }' infile

Let's start from this. Play with it. If you need more help: Please show us your desired output.
# 3  
Old 03-18-2014
Thanks for your reply. What I want is an output file that looks like this:

Instead I get an output file with one line.

# 4  
Old 03-18-2014
awk '{print "XXX","ABCD",substr($0,8,6),substr($0,14,2),"ZZ",substr($0,22,4),"HI","EX"}' OFS=, file

# 5  
Old 03-18-2014
You can also do this in straight bash:

while read
    echo XXX,ABCD,${REPLY:7:6},${REPLY:13:2},ZZ,${REPLY:21:4},HI,EX
done < INPUT_FILE > outfile1

# 6  
Old 03-18-2014
Thanks, Jedi Master and Chubler_XL. Both answers worked.
# 7  
Old 03-22-2014
The reason why you are getting one single output line only is that after you redirect stdin to INPUT_FILE, read eats up the first line, awk No. one consumes all the other lines of the file until EOF, and so just one line is printed during the first (and only) loop iteration. And I guess, that "123456" did not really appear in you sample output unless your sample input had an extra line at the top.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: ... (3 Replies)
Discussion started by: wtolentino
3 Replies

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

3. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

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: file.txt contains file_name1.txt|hai file_name2.txt|bye file_name3.txt|how... (3 Replies)
Discussion started by: rohit_shinez
3 Replies

6. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

7. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

8. Shell Programming and Scripting

How to read a file and assign variables to data?

Hi, I need a simple csh script to read a file containing data like this Buy Transactions : 175 Sell Transactions : 212 Server: sepo2 i want to read both field and its value and assign variables to each.. (2 Replies)
Discussion started by: pravsripad
2 Replies

9. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

10. Shell Programming and Scripting

Awk/shell question: Read from file and assign to variables.

Is anyone able to help with writing a program that will do the following: 1. Read the contents of a file, line by line, and on each line, assign each of the two columns to a shell variable. 2. perform an action on the variables 3. Read the next line. Here is what I've gotten so far. ... (3 Replies)
Discussion started by: akbar
3 Replies
Login or Register to Ask a Question

Featured Tech Videos