Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing text with bash script

Tags:

bash

parsing

I want to build a bash script that reads a file and produces some command line parameters. my input file looks like

20.83      0.05     0.05  __adddf3
20.83      0.10     0.05  __aeabi_fadd
16.67      0.14     0.04  gaussian_smooth
8.33      0.16     0.02   __aeabi_ddiv

I must detect and copy all the __* strings and turncate them into a command such as

gprof -E __adddf3 -E __aeabi_fadd -E __aeabi_ddiv ./nameof.out

So far I use

#!/bin/bash
while read line
do
    if [[ "$line" == *__* ]] 
    then
        echo $line;
    fi
done <input.txt

to detect the requested lines but i guess, what i need is a one-line-command thing that i can't figure out. Any kind suggestions?

like image 315
Dimitris Avatar asked Jun 06 '26 19:06

Dimitris


1 Answers

Modifying your script:

#!/bin/bash
while read -r _ _ _ arg
do
    if [[ $arg == __* ]] 
    then
        args+=("-E" "$arg")
    fi
done <input.txt
gprof "${args[@]}" ./nameof.out

The underscores are valid variable names and serve to discard the fields you don't need.

The final line executes the command with the arguments.

You can feed the result of another command into the while loop by using process substitution:

#!/bin/bash
while read -r _ _ _ arg
do
    if [[ $arg == __* ]] 
    then
        args+=("-E" "$arg")
    fi
done < <(gprof some arguments filename)
gprof "${args[@]}" ./nameof.out
like image 185
Dennis Williamson Avatar answered Jun 10 '26 02:06

Dennis Williamson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!