0

My code and input file are below. But my code doesn't print what I want. It prints all contents of the input file. But I want to print only "standard deviation" column. How can I do this using bash script?

My code:

#!/bin/bash
file="/path/input.txt"
while IFS= read -r line
do
   echo "$line" >> out.xvg
done <"$file"

input.txt:

The terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version
of the License.

Read 2 sets of 201 points, dt = 0.01


                       standard      
set      average       deviation    

a1   2.445857e+01   2.145235e+01   
a2  -1.158344e+02   5.452454e+01  
a3   2.314415e+04   3.652432e+05  
a4  -5.153647e-03   7.235728e-02 

Requested output:

2.145235e+01   
5.452454e+01  
3.652432e+05  
7.235728e-02 

Updated:

sed '/a1/,$!d' input.xvg | sed '$d' | awk '{print $3}' > output.txt

This code works but it ignores the last line of the file. I checked the input file. That is missing the newline character after its last line. But I want to print all lines of "standard deviation" column. How can I fix this problem?

1
  • 1
    if you can use awk awk 'NR>10{print $3}' file Commented Feb 26, 2015 at 13:34

1 Answer 1

1

With the cut argument you can solve your problem :

echo "$line" | tr -s " " | cut -d " " -f 3
  • tr -s " " removes all the " " (spaces) char to only one.
  • cut -d defines your delimiter
  • -f choses the colomn your want to print out (fieldset)
Sign up to request clarification or add additional context in comments.

5 Comments

well isn't "trim" but "tr", my bad. I edit my response
It works now. But It doesn't print only "standard deviation" column. It also prints some characters like "sets, deviation.."
My solution will be to use this command on your input file tail -n +9 input.txt > output.txt, and make your while/done on the output.txt file (9 is the first line which will be copy in ouput.txt)
@William The tail operation can also be done on the stdin of the while loop directly. done < <(tail -n +9 "$file")
It is solved: sed '/a1/,$!d' input.xvg | sed '$d' | awk '{print $3}' > output.txt

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.