How i can obtain differents data on a single pass?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How i can obtain differents data on a single pass?
# 1  
Old 02-13-2012
Help for the creation of a Dynamic menu generator for pekwm (Windows Manager)

#!/bin/bash
Code:
for i in `ls -c1 /usr/share/applications`
do
#name=`cat /usr/share/applications/$i | grep ^Name= | cut -d = -f2`
#categories=`cat /usr/share/applications/$i | grep ^Categories= | sed 's/;/=/g' | cut -d = -f2`
name=$(grep ^Name=  /usr/share/applications/$i | cut -d = -f2)
executable=$(grep ^Exec= /usr/share/applications/$i | cut -d = -f2)
icon=$(grep ^Icon= /usr/share/applications/$i | cut -d = -f2)
categories=$(grep ^Categories= /usr/share/applications/$i | sed 's/;/=/g' | cut -d = -f2)
echo $name
echo $icon
echo $executable
echo $categories
done

Actually i make 4 access on the single file for obtain each time one single data,is it possible obtain all the data on a single pass?
While is ok for me maintain 4 distinct variable.
Naturally using only bash or bash+sed or bash+awk or bash+grep+cut.

Last edited by alexscript; 02-16-2012 at 11:59 AM.. Reason: I think that title is more appropriate
# 2  
Old 02-13-2012
Can you provide a sample of the file?
# 3  
Old 02-13-2012
Thanks i already resolved my initial questions few moment ago. Smilie

Code:
#!/usr/bin/awk -f
BEGIN {
    print "Dynamic {"
    while ( ( "ls -c1 /usr/share/applications" | getline line ) >0 ) {
        #print line
        #print "/usr/share/applications/"line 
        while ( ( "cat /usr/share/applications/"line  | getline line1 ) >0 ) {
            #print line1
            if (match(line1,/^Name=/)) 
                name=substr(line1, 6, 30)
            if (match(line1,/^Exec=/)) 
                executable=substr(line1, 6, 30)
            if (match(line1,/^Icon=/)) 
                icon=substr(line1, 6, 30)
            if (match(line1,/^Categories=/)) { 
                categories=substr(line1, 12, 30)
                n=index(categories,";")
                categories=substr(categories, 1, n-1)
            }
        }
        #print name
        #print executable
        #print icon
        #print categories
        print  "Entry = \42" name  "\42 {Actions = \42Exec " executable "\42 }"
        # L'output deve venire con gli apici doppi attorno al nome ed all'eseguibile.
        # Entry = "Xarchiver" { Actions = "Exec xarchiver" }
        #c++        
    }
    exit
}
END  {
    print "}"
    #print "Total files: ", c
}

Problem solved.Smilie

Last edited by alexscript; 02-16-2012 at 11:56 AM.. Reason: In the end this for only a first attempt, see the follow reply until, i hope, the finish of the project ...
# 4  
Old 02-13-2012
I'm impressed you managed a useless use of cat while constrained to the limitations of awk.

Why not just while(getline <filename) ?
# 5  
Old 02-14-2012
I find myself doing this sort of thing a fair bit and setting up a local param_val() function makes the code much more maintainable/extendable/readable,
and as long as your only processing a few files the overhead can pretty much be ignored, more often than not the file will still be in the cache anyway.


Remember your going to be comming along in a year or two and going "What the $%#$ is this damn awk script doing!"
Code:
#!/bin/bash
 
function param_val {
    sed -n "s/^${1}=//p" $2 2> /dev/null
}
 
 
for i in `ls -c1 /usr/share/applications`
do
    CF=/usr/share/applications/$i
    name=$(param_val Name $CF)
    executable=$(param_val Exec $CF)
    icon=$(parm_val Icon $CF)
    categories=$(param_val Categories $CF | cut -d\; -f1)
done


Last edited by Chubler_XL; 02-14-2012 at 12:57 AM..
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 02-14-2012
Quote:
Originally Posted by Corona688
I'm impressed you managed a useless use of cat while constrained to the limitations of awk.

Why not just while(getline <filename) ?
I don't see the need for the ls either:

Code:
#!/usr/bin/env ksh
awk '
    function printit( )
    {
        print  "Entry = \42" name  "\42 {Actions = \42Exec " executable "\42 }"
        name = executable = icon = categories = "";         # reset to prevent cary over if next file is missing data
    }

    BEGIN { print "Dynamic {" }

    NR > 1 && FILENAME != last_file { printit( ); }

    { last_file = FILENAME; }

    /^Name=/    { name=substr( $0, 6, 30); next; }
    /^Exec=/    { executable=substr( $0, 6, 30); next; }
    /^Icon=/        { icon = substr( $0, 6, 30 ); next; }
    /^Categories=/ { gsub( ";.*", "" ); categories=substr( $0, 12, 30); next; }

    END  {
        printit( );
        print "}"
    } ' /usr/share/applications/*

Setting a hard stop at 30 for each substr seems wrong, but without knowing exactly what the input data is it's hard to say what is better.

Last edited by agama; 02-14-2012 at 01:39 AM.. Reason: added #!
This User Gave Thanks to agama For This Post:
# 7  
Old 02-14-2012
Quote:
Originally Posted by Corona688
Why not just while(getline <filename) ?
I need line1, i tried to replace:
Code:
while ( ( "cat /usr/share/applications/"line  | getline line1 ) >0 ) {

with
Code:
while((getline line1 < "/usr/share/applications/"line )>0 ) {

but on this way i obtain a wrong output.
Code:
Entry = "" {Actions = "Exec " }

So how i can write it properly?

Quote:
Originally Posted by Chubler_XL
Code:
#!/bin/bash
 
function param_val {
    sed -n "s/^${1}=//p" $2 2> /dev/null
}
...

Interesting.
I fix one error:
icon=$(param_val Icon $CF)
and i put the output and now work. Smilie
What mean the regular expression used on sed?

---------- Post updated at 10:14 AM ---------- Previous update was at 08:12 AM ----------

After more then one hour of search on that follow command:
Code:
sed -n "s/^${1}=//p" $2 2> /dev/null

i don't understand why use the symbol { and } over the first parameter.
And the last:
2.
While the /p if i understand correctly from Sed - An Introduction and Tutorial means:
Quote:
When the "-n" option is used, the "p" flag will cause the modified line to be printed.
I even make an attempt on test of the sed outside the script but i don't known where i made a mistake:

$ sed -e "s/^{Name}=//p" /usr/share/applications/firefox.desktop
Naturally using -n i don't have output so i used e.

Last edited by alexscript; 02-14-2012 at 05:21 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass a single quote to the program?

Dear forum members, My question is simple. I want to pass a single quote (') to be read as belonging as an addition to the letters C1', C2', C3', C4', C5', O1', O2', O3', O4' & O5' to the program voronota query-contacts which would be run through shell script and should print out numerical... (7 Replies)
Discussion started by: Aurimas
7 Replies

2. Shell Programming and Scripting

Pass some data from csv to xml file using shell/python

Hello gurus, I have a csv file with bunch of datas in each column. (see attached) Now I have an .xml file in the structure of below: ?xml version="1.0" ?> <component id="root" name="root"> <component id="system" name="system"> <param name="number_of_A" value="8"/> ... (5 Replies)
Discussion started by: Zam_1234
5 Replies

3. Shell Programming and Scripting

awk - matching on 2 columns for differents lines

Given this file (I separated them in block to make my explanation clearer): 92157768877;Sof_deme_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0 92157768877;Sof_trav_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0 91231838895;Sof_deme_faible_Email_am;EMAIL;26/01/2015;1 0;0... (1 Reply)
Discussion started by: Andy_K
1 Replies

4. Programming

Query SQL get two values differents from the same columns

Hi, I have 2 different values in the same column and two different values in other column Query 1 ins name value 1 Test 12345 1 TestV1 12/10/2014 8 Test 85435 8 TestV1 11/11/2005 9 Test 42232 9 TestV1 19/10/2000 6 Test 54321... (6 Replies)
Discussion started by: faka
6 Replies

5. Shell Programming and Scripting

Extract sequences of bytes from binary for differents blocks

Hello to all, I would like to search sequences of bytes inside big binary file. The bin file contains blocks of information, each block begins is estructured as follow: 1- Each block begins with the hex 32 (1 byte) and ends with FF. After the FF of the last block, it follows 33. 2- Next... (59 Replies)
Discussion started by: Ophiuchus
59 Replies

6. Shell Programming and Scripting

How to pass different number of arguments in a single shot

I have one configuration file. The number of lines in the file will vary. I need to pass each line as a parameter to a shell script in a single shot. Ex: Suppose file contains: ou=x,o=z o=y Suppose the shell script name is sample.sh. Then the script should be called like sample.sh ou=x.o=z... (6 Replies)
Discussion started by: saurabhkoar
6 Replies

7. Shell Programming and Scripting

Read Oracle Username password SID from single file and pass it to shell

Dear All I am trying to write one shell which will be running through Cron which contain one SQL query. But I want to draw/fetch the Username password and Instance name (required to loging to the database) from one single file to run that SQL query . Also this file contain details of multiple... (2 Replies)
Discussion started by: jhon
2 Replies

8. Web Development

how to pass data in webrick sevelts? please help

Hi all, I am a newbie to servlet programming using webrick... Was wondering how to accept data from a form and do necessary processing.. To start with i wrote a sample servlet that accepts a name and prints "welcome <name>" But I do not know how to take this value from the form into the... (0 Replies)
Discussion started by: wrapster
0 Replies

9. Shell Programming and Scripting

pass curls progress/status data to a file

hello hackers again. please help me out once again. i have a script which executes CURL to fetch a file from the web and instantly outputs the files content to STDOUT. now my question - can i somehow write the progress-status to a file? so that curl acts silently (-s) and only puts... (0 Replies)
Discussion started by: scarfake
0 Replies

10. Shell Programming and Scripting

how to use cut function to obtain this data out in script?

Hi....can you guys help me out in this script?? Below is a text file script....called Bukom.txt and it contains these: BUKOM 20060101 2.5 2.6 2.7 2.8 2.9 2.3 2.1 BUKOM 20060102 2.4 2.5 2.6 2.7 2.7 2.6 2.4 BUKOM 20060103 2.1 2.3 2.5 2.6 2.7 2.7 2.6 Can you guys help... (2 Replies)
Discussion started by: forevercalz
2 Replies
Login or Register to Ask a Question