c'est moi
c'est moi

Reputation: 1

Can I combine a long chain of sed commands into something shorter?

i successfully wrote a long line that works well for my usage. it get a file, and format the text to be as i want.

is it possible to make it shorter ?

wget http://user:[email protected]/details.cgx \
  && sed "s/value/\n/g" details.cgx >> step1 \
  && sed "s/text/</g" step1 >> step2 \
  && sed "s/id/</g" step2 >> step3 \
  && tr -d '<>/' < step3 >> step4 \
  && sed "s/formFanLevel/FanLevel/g" step4 >> step5 \
  && sed '123,155d' step5 >> step6 \
  && sed '79,120d' step6 >> step7 \
  && sed '57,66d' step7 >> step8 \
  && sed '47,48d' step8 >> step9 \
  && sed '37,44d' step9 >> step10 \
  && sed '13,26d' step10 >> VMCDF.txt \
  && rm step* && rm details.cgx

Upvotes: 0

Views: 140

Answers (3)

glenn jackman
glenn jackman

Reputation: 246827

I think you want this:

wget -O- http://user:[email protected]/details.cgx | sed -E '
    s/value/\n/g
    s/text|id/</g
    s,<>/,,g
    s/form(FanLevel)/\1/g
' | sed '
    13,26d
    37,44d
    47,48d
    57,66d
    79,120d
    123,155d
' > VMCDF.txt

Upvotes: 2

Mitsakos
Mitsakos

Reputation: 244

You can write a shell script that can apply the same command chain to any file, then use the script instead of typing all these lines.

If, though unlikely, you always use the same file, just make an alias for it by giving a name to the command chain, eg:

    alias wgetsed='wget http://user:[email protected]/details.cgx \
    && sed "s/value/\n/g" details.cgx >> step1 \
    && sed "s/text/</g" step1 >> step2 \
    ...
    && rm step* && rm details.cgx'

You then add the code above to ~/.bash_rc to make it permanent for your user account.

Upvotes: 0

Gareth Williams
Gareth Williams

Reputation: 643

A good start would be to learn how the pipe command (|) can be used to pass the output from one program as input to the next - this would remove the need to create, reference and clean up all the intermediate files (step1 .. step10) from your command line.

Upvotes: 0

Related Questions