Nested sed commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested sed commands
# 1  
Old 01-02-2015
Nested sed commands

Hi,
I get the following response by gphoto2 and I would like to substract the index number of the current item. In this case 3.
Code:
gphoto2 --get-config /main/imgsettings/iso 
Label: ISO Speed
Type: RADIO
Current: 200
Choice: 0 100
Choice: 1 125
Choice: 2 160
Choice: 3 200
Choice: 4 250
..
Choice: 13 2000
Choice: 14 2500

By calling
Code:
gphoto2 --get-config /main/imgsettings/iso | sed -n 's/Current: //p'
200

I would like to use this to filter on the 2nd critera as
Code:
gphoto2 --get-config /main/imgsettings/iso | sed -n 's_Choice: \([0-9]\+\)_\1_p'
0 100
1 125
2 160
3 200
4 250
..
13 2000
14 2500

But I would like to call only by a single line with nested sed commands that the final result looks like
Code:
3 200

or better only the index 3. But the call have to be robust that I never would get Choice 13 with 2000.

Any ideas ?
Best regards,
Nic
# 2  
Old 01-02-2015
Untested:

Code:
gphoto2 --get-config /main/imgsettings/iso | \
    grep -v [LTC][auy]] | \
    awk '{print $2" "$3}' | \
    grep ^3" "

hth

Last edited by sea; 01-02-2015 at 12:47 PM.. Reason: Code fix
# 3  
Old 01-02-2015
I am confused, how does your code filter by "200" on "Choice x: xyz...." ?
# 4  
Old 01-02-2015
It should only show the line of index 3, so the output would be:
Code:
3 200

---------- Post updated at 17:51 ---------- Previous update was at 17:49 ----------

If you turn the 2nd grep with an modified awk it could look like this:
Code:
gphoto2 --get-config /main/imgsettings/iso | \
    grep -v [LTC][auy]] | \
    grep ^3" " | \
    awk '{print $3}'

In this case we first filter out the Label, Type and current, and then limit the output to index 3, and using awk to just print the value of 200.

hth
# 5  
Old 01-02-2015
No index 3 is what i have to found and this value can change next time if i call this statement again. gphoto returns the current setting of the ISO on the digital camera and this can changing. So next time it could be the index 1, then 10 etc...
# 6  
Old 01-02-2015
uhm am i misunderstanding something?
At first i thought the command just prints that list, now i assume it expects the user to do some input?

By my thinking, those grep and awk commands should work just as good as sed.
But, since it requires/expects user interaction, the output might remain empty until the choice was done.

If that applies, you could try to redirect the output to a file abort the task and parse the files output, catch the return value, and call the command again, this time with here-doc to pass the selected option.

hth

---------- Post updated at 18:02 ---------- Previous update was at 17:58 ----------

Quote:
Originally Posted by Nic2015
No index 3 is what i have to found and this value can change next time if i call this statement again. gphoto returns the current setting of the ISO on the digital camera and this can changing. So next time it could be the index 1, then 10 etc...
In that case you should make it a script:
Code:
#!/bin/bash
gphoto2 --get-config /main/$1 | \
    grep -v [LTC][auy]] | \
    grep ^$2" " | \
    awk '{print $3}'

Place it in (as example for yourself): $HOME/bin, lets say photo-conf.sh.
Give it execution falg:
Code:
chmod +x $HOME/bin/photo-conf.sh

And call it:
Code:
photo-conf.sh "imsettings/iso" 3

hth
# 7  
Old 01-02-2015
No user interaction. The gphoto cmd to return iso is calling by node.js for automatic scripting. The filtered result by a nested sed or greb command should return always the integer of the current iso setting.

User will changes the iso on the camera and the automated script monitoring periodically the current iso.

There is a weakness of the call gphoto2 --get-config /main/imgsettings/iso it returns the current value but all the time the enum.

My script calls internally and autom.
gphoto2 --get-config /main/imgsettings/iso | sed <nested commands>
to return the index of current iso setting only for further processin.

Last edited by Nic2015; 01-02-2015 at 01:16 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Sed, matching nested brackets and deleting

1. The problem statement, all variables and given/known data: I have to write a script using sed, which delete everything between curly brackets and the brackets themself. The brackets might be nested. The input-file is: aaa { bbb ccc { ddd eee } fff { ... (2 Replies)
Discussion started by: FuzzyGnome
2 Replies

2. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

3. Shell Programming and Scripting

Grouping sed commands

Hello, would you please help me with why my SED command file is outputting the entire input file instead of only the text that I'm trying to block? cat testfile O 111111111-00 DUE-DATE METHOD: FREQUENCY: O 222222222-00 DUE-DATE METHOD: FREQUENCY: O 333333333-02 DUE-DATE METHOD:... (4 Replies)
Discussion started by: lneedh1
4 Replies

4. Shell Programming and Scripting

sed help - nested pattern

Hello, I am very new to Unix and using sed so I'm struggling a little bit with this command and was hoping someone could help me out. I want to find a nested pattern in a file and replace some text in that file with text from another file. For example, in file one I have something like... (11 Replies)
Discussion started by: planetary12
11 Replies

5. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

6. Shell Programming and Scripting

merging sed commands

Hi, I've a shell that uses two sed commands to tailor a file. sed 's/ */ /g' | sed 's/%/%%/g' Is it possible to merge this in to a single sed? Thanks! (2 Replies)
Discussion started by: dvah
2 Replies

7. Shell Programming and Scripting

Running sed commands

Hello I need to run some sed commands but it involves "/" in the substitute or delete, any ideas how I get round the problem. Example: cat file1.txt | sed -e '/</Header>/d' > file2.txt This errors due to the forward slash before the Header text. Thanks (3 Replies)
Discussion started by: Dolph
3 Replies

8. UNIX for Dummies Questions & Answers

combining sed commands

I would like to change the lines: originalline1 originalline2 to: originalline1new originalline1newline originalline2new originalline2newline To do this, id like to combine the commands: sed 's/^/&new/g' file > newfile1 and sed '/^/ a\\ newline\\ \\ (2 Replies)
Discussion started by: Dave724001
2 Replies

9. Shell Programming and Scripting

multiple sed commands

hello! I have a few sed commands sed '/^$/d' < $1 > tmp.t sed '/^ \{3,\}/d' < tmp.t > tmp1.txt ..... how can I write them in a single line? sed '/^$/d' < $1 > | '/^ \{3,\}/d' < $1 > tmp1.txt any idea? thanks. (5 Replies)
Discussion started by: george_
5 Replies

10. Shell Programming and Scripting

sed commands

I have a configuration file that when a certain script runs in updates. I want to use sed and can't seem to get the syntax right. A line from the configuration file looks like: DATE=20040909 12:00:10 When the script is run I want to change the date and time, i.e. removing the previous... (7 Replies)
Discussion started by: dbrundrett
7 Replies
Login or Register to Ask a Question