Parsing text with bash script
bash, parsing
Solution
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
Problem
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?