0

I am currently trying to grab the nth line of text out of a file namelist.txt, store it in a variable "name", and write it to EnergyData.txt using "head -n," as shown:

#!/bin/bash

counter=0

for folder in */
do

   counter=$((counter+1))
   name=$(cat namelist.txt | head -n $counter)
   echo $name
   printf $name >> EnergyData.txt
   cd $folder

   for i in */
   do
      cd $i
      ground_state=$(cat aims.out | grep  "Total energy of the DFT" | cut -c 77-94)
      HL_gap=$(cat aims.out | grep "Overall HOMO-LUMO gap" | tail -n 1 | cut -c 30-41 )
      echo "$i G1: $ground_state " >> ../../EnergyData.txt
      echo "$i HOMO-LUMO gap: $HL_gap "  >> ../../EnergyData.txt
      cd ..
   done

   cd ..
   min=$(cat EnergyData.txt | grep "G1" | cut -c 7-24 | sort -n | head -1)
   echo $min
   minline=$(cat EnergyData.txt | grep "$min" | cut -c 1-25 )
   echo -e "min energy: $minline\n" >> EnergyData.txt

done

Instead of grabbing just the nth line, it grabs the 1st-nth line, pasting progressively more lines as the loop continues. Any advice on how to fix this problem?
Zak
  • 6,976
  • 2
  • 26
  • 48
ogclifford
  • 37
  • 4
  • 3
    This feels like you're halfway through something that might not be the best approach to begin with - instead of reading the entire file for each line, do you maybe want to read it just once, line by line, as in [BashFAQ/001](https://mywiki.wooledge.org/BashFAQ/001)? – Benjamin W. Jun 14 '21 at 23:13
  • @BenjaminW. I am trying to insert the name in between the other blocks of data gathered in the nested for loop, so it's important that I read the file over every iteration of the for loop. – ogclifford Jun 14 '21 at 23:29
  • Run your script through shellcheck: https://www.shellcheck.net/ –  Jun 14 '21 at 23:46
  • 1
    Does this answer your question? [Bash tool to get nth line from a file](https://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file) – pjh Jun 15 '21 at 22:29

1 Answers1

0

You may want to use awk command to print the specific line that you want, you can pass the counter as a variable with the -v flag, for example:

awk -v count=$counter 'NR==count {print}' namelist.txt

and you can print the line and adding it to the file EnergyData.txt at the same time with:

awk -v count=$counter 'NR==count {print}' namelist.txt | tee -a >> EnergyData.txt

awk is designed to process the file line by line so you may want to exit after processing the line that you want, so try this:

awk -v count=$counter 'NR==count {print; exit;}' namelist.txt | tee -a >> EnergyData.txt
gil-i
  • 3
  • 1
  • 2