Help to process line by line and assign value to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to process line by line and assign value to variables
# 1  
Old 09-03-2012
Help to process line by line and assign value to variables

Hi, there
I have a file with tab and space as field separator. I need to assign values to variables then work with them, line by line. The code I wrote only works when each line has only one word. It doesn't work for the current situation.
Code:
for line in `cat file.txt`; do
    ID=`awk '{print $NF}' $line`
    START=`awk -F"\t" '{print $4}' $line`
    END=`awk -F"\t" '{print $5}' $line`
    ...more works with these three variables...
done

I also try something like this, still not working:
Code:
cat file.txt | while read line; do
    ID=`awk '{print $NF}' $line`
    START=`awk -F"\t" '{print $4}' $line`
    END=`awk -F"\t" '{print $5}' $line`
    ...more works with these three variables...
done

Can you please help me? Thanks!

Code:
cat file.txt
213 length:6407        abc      xyz       2         343       .       +       0        id 3290
213 length:6407        def      mno      453     3922     .       +       0        id 3291
......

the desire result (not printing them out, but assign to variables and then work with the variables) is:
for line 1, $ID=3290, $START=2, $END=343, and I will do a bunch of works with these variables before next line...
for line 2, $ID=3291, $START=453, $END=3922, and I will do a bunch of works with these variables before next line...
......

Last edited by cwzkevin; 09-03-2012 at 02:50 PM.. Reason: clarify
# 2  
Old 09-03-2012
Code:
awk -F"\t " '{printf("ID=%s,START=%s,END=%s\n",$11,$5,$6);}' input_file

This User Gave Thanks to msabhi For This Post:
# 3  
Old 09-03-2012
Quote:
Originally Posted by msabhi
Code:
awk -F"\t " '{printf("ID=%s,START=%s,END=%s\n",$11,$5,$6);}' input_file

Sorry, this is not what I want.
I need the values of current line be assigned to variables, work with these variables before next line, and so on...
I will edit my post to make it clear. Sorry for the confusion! Thanks for helping me!

Last edited by cwzkevin; 09-03-2012 at 04:52 PM..
# 4  
Old 09-03-2012
You need to use an array. The following is a bash array example.
Arrays start counting at 0 (zero), so an array of three words has 3 elements: word1 word2 word3

word1 is 0, word2 is 1, word3 is 2....
Code:
while read myline
do
   # make every word
   declare -a ( $myline )  # works with tab or space by default

   start=${arr[4]}
   end=${arr[5]}
   id=${arr[[10]}

done < inputfile

reference the elements you want and assign them to variables. I used your example to assign the variables.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 09-03-2012
Thanks, Jim
There might be a typo, but the follow code works:
Code:
while read line; do
   declare -a arr=($line)

   start=${arr[4]}
   end=${arr[5]}
   id=${arr[[10]}
   ...
done < inputfile

Quote:
Originally Posted by jim mcnamara
Code:
while read myline
do
   # make every word
   declare -a ( $myline )  # works with tab or space by default

   start=${arr[4]}
   end=${arr[5]}
   id=${arr[[10]}

done < inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

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

3. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

4. Shell Programming and Scripting

read line by line and calculate the co-presence of variables

Hey guyz, I have a table which shows the presence or absence of my variables (A,B,C,...) in my observations (1,2,3,...) * A B C ... 1 1 0 1 2 1 1 0 3 1 0 0 ... I want to calculate the co-presence of my variables. to have a table shows the pairwise presence of the variables (have... (1 Reply)
Discussion started by: @man
1 Replies

5. Shell Programming and Scripting

linux shell script to take variables from two different files line by line

Friends I need to have a shell script which will feed variables from two different files line-by-line. For example, I have two files - permission and file_name. Contents of permission is - 644 755 .... contents of file_name /file1 /file2 ..... Now I want 644 permission will be... (4 Replies)
Discussion started by: atanubanerji
4 Replies

6. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

7. Shell Programming and Scripting

Assign Line Numbers to each line of the file

Hi! I'm trying to assign line numbers to each line of the file for example consider the following.. The contents of the input file are hello how are you? I'm fine. How about you? I'm trying to get the following output.. 1 hello how are you? 2 I'm fine. 3 How about you? ... (8 Replies)
Discussion started by: abk07
8 Replies

8. UNIX for Dummies Questions & Answers

shell script to process file line by line

Hi , I am new to shell scripting (ksh shell) and trying to accomplish few requiremtns. I have a file with the following format EMP NO EMP NAME AGE Amt Paid 12 Mark Taylor 32 32333 14 James Brown... (5 Replies)
Discussion started by: royalsing
5 Replies

9. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

10. Programming

how do u assign the values to different variables when it is presneted in one line??

hey people.. i have a configuration file that looks like 7080 7988 net04.xxxxx.edu 20 where 20 is the number of threads in the thread pool initially. net04.xxxxx.edu is the hostname. and 7080 7988 are two ports. first one for client requests and second one for dns communication. now my... (2 Replies)
Discussion started by: SwetaShah
2 Replies
Login or Register to Ask a Question

Featured Tech Videos