0

I have a dataset with subjects of which the SUVr is calculated and put on the 3rd line of a txtfile, specific to each subject (so every subject has a different txtfile containing the SUVr). My question is: How can I get the 3rd line (which is the SUVr value) of every subjects' txtfile and place them in a new txtfile, so all the SUVRs of every subject are placed in one txtfile? I'm using the Linux terminal to do this. Thank you very much!

3 Answers3

0
for file in $(find . -type f); do sed '3q;d' $file; done > output.txt

Some explanation about the command parts:

  • find . -type f: list all files
    • If you need to filter by extension: find . -type f -name "*.txt"
  • sed '3q;d' filename: prints the 3rd line of the file
  • > output.txt: write to output.txt (use >> if you want to append)
Paulo Amaral
  • 747
  • 1
  • 5
  • 24
0

awk works well for this also.

awk '3 == FNR { print $0; }' /path/to/$files.* >found

FNR is File Number of Records, so it grabs the 3rd line from each file without executing over and over, or needing a find, etc.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
0

Really simple:

sed -n 3p -s *.txt > output.txt
Darby_Crash
  • 446
  • 3
  • 6