3

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?

4
  • 1
    @shellter: == works fin with globbing. Commented Jun 2, 2012 at 3:44
  • @DennisWilliamson - correct, but I assume you mean "fine". Commented Jun 2, 2012 at 6:23
  • 1
    @jordanm: I said "fin" probably because I was looking at the name of the person who edited this question. ;-) Commented Jun 2, 2012 at 7:35
  • @shellter: It means equal here, too. It's just equal to something with wildcards. Commented Jun 2, 2012 at 14:17

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

3

One more way to skin the cat:

gprof `sed -ne '/__/s/.* / -E /p' input.txt` ./nameof.out

The sed script searches for lines with __, then changes everything up to the last space with -E and prints the result. You may have to adjust things a little if your whitespace could include tabs. For clarity, I didn't account for that here.

Comments

2

Using awk:

awk '$4 ~/^__/{a=a" -E "$4}END{system("gprof "a" ./nameof.out")}' inputFile

4 Comments

That is perfect! I just need to turn that print into an executable command, so run instead of print.
system() instead of print is the answer! :-)
@user1431903 - don't forget to "accept" the answer by clicking on the arrow.
@ghoti I didn't. I'm just searching for a solution without awk

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.