I got a "working" script for monitoring Autosys jobs. Basically, it starts with listing all the jobs that are due to run in a "shift". Now, I'm in the part where I want to delete a job/word from a list BUT only the first occurrence of such job name. This is to consider jobs that runs multiple times.

Ex: I got a list of jobs in a file:

JOB1
JOB2
JOB3
JOB1
JOB4
JOB2

Is there any one-liner (awk, sed, etc.) that can delete the first occurrence only, e.g. JOB1, leaving the other - after JOB3 - in the list? Or other way to implement this?

I saw a sed one-liner but deletes all. By the way, the jobs listed is stored in a variable JOBNAME.

sed -ie "\|^$JOBNAME\$|d"

Please put some explanation. :) I really am a newbie to scripting but working hard to learn the way of the force.

Cheers,
sprok

Instead of :

sed -ie "\|^$JOBNAME\$|d"

try:

sed -ie "0,/$JOBNAMES/{//d;}" filename

The workhorse bit is "0,/RE/". It looks from starting line until first occurrence of REGEX

>I really am a newbie to scripting but working hard to learn the way of the force.
Here's the tools to construct your "light saber," young Jedi.

Thanks for taking time. Appreciate it a TON!

I tried it and a couple more variations [the lazy way :)] but nothing is working.

Sproky: $ echo $JOBNAMES
JOB1
Sproky: $ sed -e "0,/$JOBNAMES/{//d;}" JOBLIST.txt
JOB1
JOB2
JOB3
JOB1
JOB4
JOB5

Don't give up on me yet...

(I left... somewhere checking the tools.)

I don't know if it's giving up but I will make other ways, may be loop on this to keep it going rather than a one-liner.

As they say, short code not necessarily cool at the expense of readability.

But, I'll still look for that [one-liner]. ;)

>But, I'll still look for that [one-liner].
Because originally you used the switch -i I assumed your version of sed was GNU.
Thus sed -ie "0,/$JOBNAMES/{//d;}" filename should have work.
However, if you are not using GNU sed this rather more involved syntax will delete the first occurrence of a regex.

sed '/'"$JOBNAMES"'/{x;/Y/!{s/^/Y/;h;d;};x;}' JOBLIST.txt

Watch the quotes scheme (')(")$JOBNAMES(")(')

Perfect!! Apologies for the delay. Quite busy lately on the new job, to many trainings! :)

Anyway, one more favor, care to explain (before I mark this as solved)? TIA.

>Anyway, one more favor, care to explain
It would require a base understanding that would take some time to write. Fortunately, others has done it already. Take a look at this link, and pay attention to commands and grouping with {}.
The sed FAQ has a lot of valuable information as well.

try sed "s/$jobname//1" this will only remove the dirst substitution

try sed "s/$jobname//1" this will only remove the dirst substitution

I am afraid that command doesn't produce the advertised result.

oh... i forgot...
convert everything to one line with tr '\n' ';' then convert everything back tr ';' '\n' and youre done

Fair enough. Thanks again guys for all your support, another lesson learned! 'Til then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.