Aiskya
Aiskya

Reputation: 73

Shell scripting extract specific terms from a line

I'm a beginner in using shell scripting. I wanted to pull out a specific term from several lines that I have already excluded from a large text file (say, temporal lobe), and add the values that I have extracted together.

For example,

Line 1: 1    3    13579 586  Right-Temporal  72    73    66   54
Line 2: 2    5    24680 587  Left-Temporal   89    44    65   56

*The spaces between each terms are very wide, but I can't seem to separate the spaces...

Say if I want to add the number 10455+10475 together using shell scripting, what will be the example of the script that I can type in? I have searched for this question on internet using multiple sites, and none of it seems to be associated with what I am doing

Upvotes: 1

Views: 76

Answers (1)

Raju
Raju

Reputation: 2962

Input text(added additional rows for better explanation)

Line 1: 1 3 13579 586 Right-Temporal 72 73 66 54
Line 2: 2 5 24680 587 Left-Temporal 89 44 65 56
Line 3: 2 5    24681 587 Left-Temporal 89 44 65 56
Line 4: 2 5        24682 587 Left-Temporal 89 44 65 56

For example, if you want to filter out 13579 & 24680 etc .. Which is 5th column with space as a delimiter, you can sum those values using below command

awk '{total += $5} END {print total}' TemporalLobe.txt

Note: Even if you have multiple space in-front of your text, it will be considered as single delimiter. For ex: I have added Line3 & Line 4. These two lines have additional spaces in-front of column 5. awk treats additional spaces as a part of delimiter.

Edit 1:

Assuming that, 'Temporal' is the key to grep.

grep Temporal TemporalLobe.txt. | awk '{total += $5} END {print total}' 

similarly, you can apply this for multiple columns.

grep Temporal TemporalLobe.txt. | awk '{total += $3 + $4} END {print total}' 
result = 25

Explanation:

  • grep will extract desired lines from large data file 'TemporalLobe.txt'.
  • This data is passed to awk using | (pipe)
  • awk will extract column3 ($3) , column4 ($4). It will sum both columns

i.e. Result = ( {1 + 3} + {2 + 5} + {2 + 5} + {2 + 5})

Result = 25

Upvotes: 1

Related Questions