How to strip some characters before putting in array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to strip some characters before putting in array?
# 1  
Old 01-13-2015
How to strip some characters before putting in array?

Hi Gurus,

my current code like below:
Code:
 nawk '{f1 = (NF>1)?$1:""}{print f1, $NF}'|sed -e 's/s(/,/g;s/)//g;s/ *,/,/'|nawk -F"," '{a[$1]b[$2]}END{for (i in b) if (!(i in a))print i}'

I have file like below. (this is autosys job dependencies)
the job with s() is dependencies, the job without s() is job need to be run. my task is to find all dependency jobs which not in the box. some of the above code I got from the folks in this fourm.

I am wondering if it is possible to remove
Code:
sed -e 's/s(/,/g;s/)//g;s/ *,/,/'

and in second awk, I can strip s() and put only job name in the array.

Code:
deptnm-appnm-code     -------     s(deptnm-ocode-30-ddd)
                                  s(deptnm-ocode-00-dum)
                                  s(deptnm-appnm-ecode)
deptnm-appnm-code-dld -------     s(deptnm-on-rundt-run)
                                  s(deptnm-appnm-ocodel-su)
                                  s(deptnm-appnm-ecode-dld)
                                  s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum -------     s(deptnm-on-rundt-bp)

thanks in advance.

Last edited by ken6503; 01-13-2015 at 05:44 PM..
# 2  
Old 01-13-2015
Post a sample of the input and desired output...because imo this can all be done with a single nawk without the need for a pipe...
This User Gave Thanks to shamrock For This Post:
# 3  
Old 01-13-2015
Quote:
Originally Posted by shamrock
Post a sample of the input and desired output...because imo this can all be done with a single nawk without the need for a pipe...
Thanks for your reply, shamrock,

my input like below:
Code:
deptnm-appnm-code     -------     s(deptnm-ocode-30-ddd)
                                  s(deptnm-ocode-00-dum)
deptnm-appnm-ocodel-su   ------- s(deptnm-appnm-ecode)
deptnm-appnm-code-dld -------     s(deptnm-on-rundt-run)
                                  s(deptnm-appnm-ocodel-su)
                                  s(deptnm-appnm-ecode-dld)
                                  s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum -------     s(deptnm-on-rundt-bp)

my expected output is
Code:
deptnm-ocode-30-ddd
deptnm-ocode-00-dum
deptnm-appnm-ecode
deptnm-on-rundt-run
deptnm-appnm-ecode-dld
deptnm-ocode-50-curcnt
deptnm-on-rundt-bp

list all jobs in right side, not exist in left side.
# 4  
Old 01-13-2015
Hello,
You can try (work with gawk --posix):
Code:
nawk 'gsub(/[s()]/,"") && NF == 3 {s[$1]=1;d[a++]=$3;next};{d[a++]=$1};END{while(i<a) if (!s[d[i++]]) print d[i-1] }' input-file

This User Gave Thanks to disedorgue For This Post:
# 5  
Old 01-13-2015
Quote:
Originally Posted by disedorgue
Hello,
You can try (work with gawk --posix):
Code:
nawk 'gsub(/[s()]/,"") && NF == 3 {s[$1]=1;d[a++]=$3;next};{d[a++]=$1};END{while(i<a) if (!s[d[i++]]) print d[i-1] }' input-file

Hi disedorgue,

thanks for your reply,
the code works. there is a little thing
Code:
gsub(/[s()]/,"")

replace all "s" in the file. example:
Code:
deptnm-appnm-ocodel-su

become
Code:
deptnm-appnm-ocodel-u

is there any way I can replace "s(" as whole?

thanks in advance.

---------- Post updated at 09:58 PM ---------- Previous update was at 09:09 PM ----------

Quote:
Originally Posted by ken6503
Hi disedorgue,

thanks for your reply,
the code works. there is a little thing
Code:
gsub(/[s()]/,"")

replace all "s" in the file. example:
Code:
deptnm-appnm-ocodel-su

become
Code:
deptnm-appnm-ocodel-u

is there any way I can replace "s(" as whole?

thanks in advance.
I found the command
gsub(/s\(|\)/,"")
# 6  
Old 01-13-2015
Hello ken6503,

Could you please try following, it may help you.
Code:
awk 'FNR==NR{if(NF>1){A[$1]=$1;gsub(/s\(/,X,$NF);gsub(/\)$/,X,$NF);B[$NF]=$NF} else {gsub(/s\(/,X,$NF);gsub(/\)$/,X,$NF);B[$NF]=$NF}} END{for(j in A){delete B[j]}for(u in B){print B[u]}}' Input_file Input_file

Output will be as follows.
Code:
deptnm-ocode-00-dum
deptnm-appnm-ecode
deptnm-on-rundt-bp
deptnm-on-rundt-run
deptnm-appnm-ecode-dld
deptnm-ocode-30-ddd
deptnm-ocode-50-curcnt

EDIT: Adding a non one liner form for same.
Code:
awk 'FNR==NR{
                if(NF>1){
                                A[$1]=$1;
                                gsub(/s\(/,X,$NF);
                                gsub(/\)$/,X,$NF);
                                B[$NF]=$NF
                        }
                else    {
                                gsub(/s\(/,X,$NF);
                                gsub(/\)$/,X,$NF);
                                B[$NF]=$NF
                        }
             }
     END     {
                for(j in A){
                                delete B[j]
                           }
                for(u in B){
                                print B[u]
                           }
             }
    ' Input_file Input_file


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-14-2015 at 12:44 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 01-14-2015
How about:
Code:
awk 'NR==FNR{if(NF>1)A[$1]; next} !($2 in A){print $2}' input-file FS='[)(]' input-file

Code:
eptnm-ocode-30-ddd
deptnm-ocode-00-dum
deptnm-appnm-ecode
deptnm-on-rundt-run
deptnm-appnm-ecode-dld
deptnm-ocode-50-curcnt
deptnm-on-rundt-bp


Last edited by Scrutinizer; 01-14-2015 at 04:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Putting strings into positioning array in loop

i need to add 2 string variables into a positioning array , repeatedly - in loop. First string in $2, second to $3 then up to the desired count incrementing the "position". Using set -- alone does not increment the count so I end up with 2 variables in the array. How do I increment the... (7 Replies)
Discussion started by: annacreek
7 Replies

2. Shell Programming and Scripting

Need to strip control-A characters from a column in a file

Hi All, I currently have flat file with 32 columns. The field delimiter is cntl-A ( \x01). The file has been extracted from an oracle table using a datastage job. However, in the 6th field, the data contains additional control -A characters which came as a part of the table data. I need... (12 Replies)
Discussion started by: harsha1238
12 Replies

3. Shell Programming and Scripting

Taking information from a postgres sql query and putting it into a shell script array

I have a postgres sql statement that is the following: select age from students; which gives me the entries: Age --- 10 15 13 12 9 14 10 which is about 7 rows of data. Now what I would like to do with this is use a shell script to create an array age. As a results... (3 Replies)
Discussion started by: JSNY
3 Replies

4. Shell Programming and Scripting

Help awk/sed: putting a space after numbers:to separate number and characters.

Hi Experts, How to sepearate the list digit with letters : with a space from where the letters begins, or other words from where the digits ended. file 52087mo(enbatl) 52049mo(enbatl) 52085mo(enbatl) 25051mo(enbatl) The output should be looks like: 52087 mo(enbatl) 52049... (10 Replies)
Discussion started by: rveri
10 Replies

5. Shell Programming and Scripting

Strip First few Characters

I want to strip first few characters from each record until a proper datesamp is found. Request for getNextPage.................06/29/12 07:49:30 VVUKOVIC@67.208.166.131{7A805FEF76A62FCBB23EA78B5380EF95.tomcat1}TP-Processor14 LogExchUsage: ERROR:: isprof=false : exch=NSDQ output should be... (2 Replies)
Discussion started by: ratheeshjulk
2 Replies

6. Shell Programming and Scripting

Putting a character between two other characters?

I need to separate Pascal style identifiers (TheyLookLikeThis) into words separated by an underscore (_). I've tried sed 's//&_&/' but this won't work (obviously). I'd love some help. (4 Replies)
Discussion started by: Ilja
4 Replies

7. UNIX for Dummies Questions & Answers

Array with special Characters

Hi, I would write a shell script to execute a series of command. However, if the cmd contains "-" in the array. It fails to do so. I'd tried use ', " or \ but get the same result. Output: (1 Reply)
Discussion started by: donaldfung
1 Replies

8. Shell Programming and Scripting

create array holding characters from sring then echo array.

Hi, I wish to store $string1 in $string1array a character in each array element. Then i wish to echo the entire array to the screen so that it reads as the normal string again. I have been trying with the code below but does not work. Please help... To put string into array: ... (5 Replies)
Discussion started by: rorey_breaker
5 Replies

9. UNIX for Dummies Questions & Answers

Strip characters after the last "/"

Hi, Could anybody please guide how to strip all charaters after the last '/' from a string in a shell script. I have got few strings like "/tmp/oracle/oradata1/abc" or "/var/log/backup/bdfd" and I want to strip anything after the last "/" i.e "abc" and "bdfd" in my case. Thanks, Jamal (3 Replies)
Discussion started by: ahjaeasedqa
3 Replies
Login or Register to Ask a Question