bashubuntu

Search thru a text file with multiple lines and extract the data from single command line


I am trying to search thru a text file with multiple lines and extract the data from search criteria.

The text, brought by boinccmd --get_tasks to stdout

======== Tasks ========
1) -----------
   name: p2030.1748802307.G42.81-01.36.N.b4s0g0.00000_2163_0
   WU name: p2030.1748802307.G42.81-01.36.N.b4s0g0.00000_2163
   project URL: https://einstein.phys.uwm.edu/
   received: Mon Jun  2 19:50:08 2025
   report deadline: Mon Jun 16 19:50:08 2025
   ready to report: no
   state: downloaded
   scheduler state: scheduled
   active_task_state: EXECUTING
   app version num: 147
   resources: 1 CPU
   CPU time at last checkpoint: 31027.740000
   current CPU time: 31050.890000
   estimated CPU time remaining: 9751.101616
   fraction done: 0.741819
   swap size: 134 MB
   working set size: 134 MB
   suspended via GUI: no
2) -----------
   name: p2030.1748802307.G42.81-01.36.N.b4s0g0.00000_3190_1
   WU name: p2030.1748802307.G42.81-01.36.N.b4s0g0.00000_3190
   project URL: https://einstein.phys.uwm.edu/
   received: Mon Jun  2 20:38:20 2025
   report deadline: Mon Jun 16 20:38:20 2025
   ready to report: no
   state: downloaded
   scheduler state: scheduled
   active_task_state: EXECUTING
   app version num: 147
   resources: 1 CPU
   CPU time at last checkpoint: 25980.920000
   current CPU time: 25986.440000
   estimated CPU time remaining: 2658.877011
   fraction done: 0.929601
   swap size: 134 MB
   working set size: 134 MB
 

I want to printf a report "fraction done;" and "name:" on one line on a terminal. Some text files will not contain "fraction done:" --- Those text files need to be ignored.

What I have been able to do: Get a list of "fraction done:" (Will ignore files without "fraction done:" data

for task in $(boinccmd --get_tasks | sed -n 's/\s\s\sfraction done: //p'); do printf "%s"${task}"\n";done

Get a list of "name:":

for task in $(boinccmd --get_tasks | sed -n 's/\s\s\sname: //p'); do printf "%s"${task}"\n";done

What I am wanting to do is get a list of: printf "fraction done: name: \n"

What I have basically tried including dozens of alternates:

for task in $(boinccmd --get_tasks | sed -n 's/\s\s\sfraction done: ,\s\s\sname: //p') | awk -F\  '{print $1,$2}'); do printf "%s"${task}"\n"; done



for task in $(boinccmd --get_tasks | sed -n 's/\s\s\sfraction done: //p' &&  sed -n 's/\s\s\sname: //p'); do printf "%s"${task}"\n"; done



for task in $(boinccmd --get_tasks | sed -n 's/\s\s\sfraction done: //p'); do printf "%s"${task}"\n"; done



$(boinccmd --get_tasks | sed -n 's/\s\s\sfraction done: ,\s\s\sname: /\2/' | awk -F\  '{print $1,$2}'); do printf "%s"${task}"\n"; done

Thanks. I appreciate the help.


Solution

  • Since 2023, bionccmd has had a get_task_summary option which looks like it would provide a very simple solution:

    boinccmd --get_task_summary cp
    

    However, as you appear to be using an extremely old version, filtering its output with awk as others suggest may be appropriate. Here is another variation:

    boinccmd --get_tasks |
    awk -F '^[^:]+: ' '/^   name:/{n=$2; next} /^   fraction done:/{print $2,n}'
    

    For your sample data, this will output:

    0.741819 p2030.1748802307.G42.81-01.36.N.b4s0g0.00000_2163_0
    0.929601 p2030.1748802307.G42.81-01.36.N.b4s0g0.00000_3190_1
    

    You don't provide sample desired output, but your sed attempts seem to indicate you want to strip off the name: and fraction done: prefixes.

    ^ at start of the awk FS regular expression matches start of line. As the first character of a bracket expression it means negation.

    Note that \s in your sed commands is non-standard. If you want to match any whitespace character, for portability you should use [[:space:]].


    Your printf "%s"${task}"\n" command is broken:

    Perhaps you meant:

    printf '%s\n' "$task"