0

I have very limited knowledge of bash coding.

I have some data stored in a text file in the following format (picture attached). Each line contains some text and a list of numbers. Each of the entries are separated by a tab.

The texts from each line should go to respective variables. For line-1: $var_1='SomeText', $var_2='(Sometext)', $var_3='myLabel1' etc. Numbers following the texts should be treated as array entries. For line-1, Number_array=[2,0,-1,-2]

Afterwards I want to do something like this:

for each line:
   for i=0 to length(Number_array)-1:
     $a=Number_array[i]
     $b=Number_array[i+1]

     <Some other code>

    end inner loop
end outer loop

enter image description here

3
  • 1
    1. Don't post pictures of text. Copy it (use cat -T when you have tabs so we can see them). 2. Have a look at awk. Commented Apr 17, 2020 at 16:01
  • Also, you will be getter help if you include the required output from that sample input AND include any current output from your code and exact text of any error messages you are getting. ("It doesn't work" doesn't help us!) . Agree that you want to read up on awk. Check out the Awk Tutorial. Good luck. Commented Apr 17, 2020 at 16:06
  • However if you have a definite requirement to use bash, then something like while read var1 var2 var3 var4 num1 num2 num3 num4 any_remaing_values ; do printf "%s.......%s\n", $var1, .... $num4 ; done < inputFile would be the place to start. You can search here for [bash] while read and get much more detail. You'll need to understand how printf and it's format string and format specifiers work. I'd recommend Awk printf tutorial for that. There are tiny differences when using printf in bash vs awk, so do small tests first.Good Luck Commented Apr 17, 2020 at 16:10

1 Answer 1

1

If the fields are separated by tab and there are always 8 of them, a single loop is sufficient;

cat filename.txt | while read x x x x a b c d
do 
  <some code>
done

the first 4 fields are read into the variable x (and ignored), while last 4 fields of numbers are stored in a,b,c and d, respectively.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.