Extract the characters before last colonWhy is using a shell loop to process text considered bad practice?How to extract line from the file on specific conditionRemove the string before the whitespaceextract the characters before () using grepHow to use Sed to replace all characters before colon?Remove characters in a specific column before a specific characterText manipulation: Extract everything inside bracketsTo delete everything between square bracketsHow to remove last n characters of a particular columnHow to use sed and regular expressions to find pattern and remove last few characters?Print only the lines that with all digits except the last one or the last two characters or the first or second characters

Who will lead the country until there is a new Tory leader?

Employer asking for online access to bank account - Is this a scam?

Where is the logic in castrating fighters?

How to respond to an upset student?

How to know if a folder is a symbolic link?

Compactness of finite sets

Is Jon Snow the last of his House?

Construct a word ladder

Why does this if-statement combining assignment and an equality check return true?

Why doesn't the Earth accelerate towards the Moon?

Why does Mjolnir fall down in Age of Ultron but not in Endgame?

The art of clickbait captions

Count rotary dial pulses in a phone number (including letters)

Should one buy new hardware after a system compromise?

Is the Starlink array really visible from Earth?

Count Even Digits In Number

Why do Ryanair allow me to book connecting itineraries through a third party, but not through their own website?

Why were helmets and other body armour not commonplace in the 1800s?

Where's this lookout in Nova Scotia?

Is there another way of saying "to take refuge in alcohol"?

Popcorn is the only acceptable snack to consume while watching a movie

Employer demanding to see degree after poor code review

Does the unit of measure matter when you are solving for the diameter of a circumference?

Is the Indo-European language family made up?



Extract the characters before last colon


Why is using a shell loop to process text considered bad practice?How to extract line from the file on specific conditionRemove the string before the whitespaceextract the characters before () using grepHow to use Sed to replace all characters before colon?Remove characters in a specific column before a specific characterText manipulation: Extract everything inside bracketsTo delete everything between square bracketsHow to remove last n characters of a particular columnHow to use sed and regular expressions to find pattern and remove last few characters?Print only the lines that with all digits except the last one or the last two characters or the first or second characters






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















I need to extract the characters before last colon : and also remove the square brackets [] in the last line.
My file structure is



256.XXX.XXX.X:20234
214.XXX.XXX.X:7249
[2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX]:46288


I need output file as in the form of:



256.XXX.XXX.X
214.XXX.XXX.X
2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX









share|improve this question
























  • In your output, you also remove the [ ], but you don't mention it

    – Philippos
    May 13 at 15:06











  • Ok I edited my answer to remove brackets...

    – Luciano Andress Martini
    May 13 at 15:10











  • Is that netstat result which contains IP and port?

    – Ivan Chau
    May 14 at 6:15

















5















I need to extract the characters before last colon : and also remove the square brackets [] in the last line.
My file structure is



256.XXX.XXX.X:20234
214.XXX.XXX.X:7249
[2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX]:46288


I need output file as in the form of:



256.XXX.XXX.X
214.XXX.XXX.X
2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX









share|improve this question
























  • In your output, you also remove the [ ], but you don't mention it

    – Philippos
    May 13 at 15:06











  • Ok I edited my answer to remove brackets...

    – Luciano Andress Martini
    May 13 at 15:10











  • Is that netstat result which contains IP and port?

    – Ivan Chau
    May 14 at 6:15













5












5








5


1






I need to extract the characters before last colon : and also remove the square brackets [] in the last line.
My file structure is



256.XXX.XXX.X:20234
214.XXX.XXX.X:7249
[2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX]:46288


I need output file as in the form of:



256.XXX.XXX.X
214.XXX.XXX.X
2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX









share|improve this question
















I need to extract the characters before last colon : and also remove the square brackets [] in the last line.
My file structure is



256.XXX.XXX.X:20234
214.XXX.XXX.X:7249
[2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX]:46288


I need output file as in the form of:



256.XXX.XXX.X
214.XXX.XXX.X
2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX






text-processing awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 13 at 17:50









ilkkachu

64k10106184




64k10106184










asked May 13 at 14:58









NaniNani

7317




7317












  • In your output, you also remove the [ ], but you don't mention it

    – Philippos
    May 13 at 15:06











  • Ok I edited my answer to remove brackets...

    – Luciano Andress Martini
    May 13 at 15:10











  • Is that netstat result which contains IP and port?

    – Ivan Chau
    May 14 at 6:15

















  • In your output, you also remove the [ ], but you don't mention it

    – Philippos
    May 13 at 15:06











  • Ok I edited my answer to remove brackets...

    – Luciano Andress Martini
    May 13 at 15:10











  • Is that netstat result which contains IP and port?

    – Ivan Chau
    May 14 at 6:15
















In your output, you also remove the [ ], but you don't mention it

– Philippos
May 13 at 15:06





In your output, you also remove the [ ], but you don't mention it

– Philippos
May 13 at 15:06













Ok I edited my answer to remove brackets...

– Luciano Andress Martini
May 13 at 15:10





Ok I edited my answer to remove brackets...

– Luciano Andress Martini
May 13 at 15:10













Is that netstat result which contains IP and port?

– Ivan Chau
May 14 at 6:15





Is that netstat result which contains IP and port?

– Ivan Chau
May 14 at 6:15










5 Answers
5






active

oldest

votes


















10














Remove everything after the last colon, and then any brackets left anywhere:



sed 's/:[^:]*$//; s/[][]//g'


Or



sed 's/(.*):.*/1/; s/[][]//g'


(here using the fact that the first .* will be greedy and swallow as many :s as possible).






share|improve this answer




















  • 1





    The [ ] also need to be removed, it seems

    – Philippos
    May 13 at 15:05











  • @Philippos, OK, I'm with you now. Thanks. I've edited it in.

    – Stéphane Chazelas
    May 13 at 15:09











  • Thanks everyone. This is working

    – Nani
    May 13 at 15:14






  • 1





    [][] is something of a regex corner-case and might warrant some explaining.

    – Digital Trauma
    May 14 at 0:41


















3














This will extract all characters before last 'colon' and remove the brackets [ ] as the example you give.



rev <yourfile.txt | cut -d: -f2- | rev | tr -d '[]'


Replace yourfile.txt by your file name or remove the word <yourfile.txt to read the standard output.






share|improve this answer




















  • 1





    Note that rev is not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted [] will cause an error in csh or zsh.

    – Stéphane Chazelas
    May 13 at 15:17











  • Right I corrected it with quotes. In practice there are some unixes without rev or even some busybox environment that does not have it?

    – Luciano Andress Martini
    May 13 at 15:25







  • 2





    There's none on Solaris. BSDs, GNU, busybox, AIX and HPUX seem to all have one.

    – Stéphane Chazelas
    May 13 at 15:26











  • Thank you for the information!

    – Luciano Andress Martini
    May 13 at 15:26


















2














Shell only:



while IFS= read -r line; do
tmp=$line%:* # remove last colon and following chars
tmp=$tmp#"[" # remove leading open bracket
result=$tmp%"]" # remove trailing close bracket
printf "%sn" "$result"
done < file





share|improve this answer

























  • I don't know why but I always love this kind of solutions... Just because it does run with a minimal tools environment.

    – Luciano Andress Martini
    May 13 at 15:19












  • And I always cringe at them. Here, you can also add a portability dimension as those $tmp#[ won't work with ksh93 or zsh.

    – Stéphane Chazelas
    May 13 at 15:28












  • @StéphaneChazelas In true I agree with you in security / reliability terms. But I think you understand my point of view too.

    – Luciano Andress Martini
    May 13 at 15:30







  • 1





    Seems to work fine with ksh "version sh (AT&T Research) 93u+ 2012-08-01". I don't have an older version to test with.

    – glenn jackman
    May 13 at 15:44







  • 1





    ksh -c 'tmp=$tmp#[' gives a syntax error with ksh93u+, but tmp=anything ksh -c 'tmp=$tmp#[' doesn't. Looks like a bug. In any case, [ being a wildcard operator is better quoted. tmp=$tmp#"[" works OK in any POSIX-like shell. You'd want to replace echo with printf '%sn' as well.

    – Stéphane Chazelas
    May 13 at 19:00



















2














awk -F: 'OFS=":"; NF--; print $0' $file



or



cat file | awk -F: 'OFS=":"; NF--; print $0'



which breaks down as:




  • -F: set the input field separator to :


  • OFS=":" set the output field separator to :


  • NF-- reduce the Number of Fields by 1 (get rid of the last field)


  • print $0 print the remaining records, separated by the OFS (:) character.

Update to also remove the square brackets:



awk -F: 'OFS=":"; NF--; gsub(/[' $file



  • added gsub(/[|]/, "")1 which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.





share|improve this answer
































    0














    Command:



    awk -F ":" 'OFS=":"$NF="";print $0' filename | sed "s/:$//g"| sed "s/^[//g"|sed "s/]//g"


    output



    256.XXX.XXX.X
    214.XXX.XXX.X
    2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX





    share|improve this answer


















    • 1





      This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t need sed too...

      – Stephen Kitt
      May 15 at 16:38











    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f518698%2fextract-the-characters-before-last-colon%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10














    Remove everything after the last colon, and then any brackets left anywhere:



    sed 's/:[^:]*$//; s/[][]//g'


    Or



    sed 's/(.*):.*/1/; s/[][]//g'


    (here using the fact that the first .* will be greedy and swallow as many :s as possible).






    share|improve this answer




















    • 1





      The [ ] also need to be removed, it seems

      – Philippos
      May 13 at 15:05











    • @Philippos, OK, I'm with you now. Thanks. I've edited it in.

      – Stéphane Chazelas
      May 13 at 15:09











    • Thanks everyone. This is working

      – Nani
      May 13 at 15:14






    • 1





      [][] is something of a regex corner-case and might warrant some explaining.

      – Digital Trauma
      May 14 at 0:41















    10














    Remove everything after the last colon, and then any brackets left anywhere:



    sed 's/:[^:]*$//; s/[][]//g'


    Or



    sed 's/(.*):.*/1/; s/[][]//g'


    (here using the fact that the first .* will be greedy and swallow as many :s as possible).






    share|improve this answer




















    • 1





      The [ ] also need to be removed, it seems

      – Philippos
      May 13 at 15:05











    • @Philippos, OK, I'm with you now. Thanks. I've edited it in.

      – Stéphane Chazelas
      May 13 at 15:09











    • Thanks everyone. This is working

      – Nani
      May 13 at 15:14






    • 1





      [][] is something of a regex corner-case and might warrant some explaining.

      – Digital Trauma
      May 14 at 0:41













    10












    10








    10







    Remove everything after the last colon, and then any brackets left anywhere:



    sed 's/:[^:]*$//; s/[][]//g'


    Or



    sed 's/(.*):.*/1/; s/[][]//g'


    (here using the fact that the first .* will be greedy and swallow as many :s as possible).






    share|improve this answer















    Remove everything after the last colon, and then any brackets left anywhere:



    sed 's/:[^:]*$//; s/[][]//g'


    Or



    sed 's/(.*):.*/1/; s/[][]//g'


    (here using the fact that the first .* will be greedy and swallow as many :s as possible).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 13 at 17:51









    ilkkachu

    64k10106184




    64k10106184










    answered May 13 at 15:04









    Stéphane ChazelasStéphane Chazelas

    319k57605974




    319k57605974







    • 1





      The [ ] also need to be removed, it seems

      – Philippos
      May 13 at 15:05











    • @Philippos, OK, I'm with you now. Thanks. I've edited it in.

      – Stéphane Chazelas
      May 13 at 15:09











    • Thanks everyone. This is working

      – Nani
      May 13 at 15:14






    • 1





      [][] is something of a regex corner-case and might warrant some explaining.

      – Digital Trauma
      May 14 at 0:41












    • 1





      The [ ] also need to be removed, it seems

      – Philippos
      May 13 at 15:05











    • @Philippos, OK, I'm with you now. Thanks. I've edited it in.

      – Stéphane Chazelas
      May 13 at 15:09











    • Thanks everyone. This is working

      – Nani
      May 13 at 15:14






    • 1





      [][] is something of a regex corner-case and might warrant some explaining.

      – Digital Trauma
      May 14 at 0:41







    1




    1





    The [ ] also need to be removed, it seems

    – Philippos
    May 13 at 15:05





    The [ ] also need to be removed, it seems

    – Philippos
    May 13 at 15:05













    @Philippos, OK, I'm with you now. Thanks. I've edited it in.

    – Stéphane Chazelas
    May 13 at 15:09





    @Philippos, OK, I'm with you now. Thanks. I've edited it in.

    – Stéphane Chazelas
    May 13 at 15:09













    Thanks everyone. This is working

    – Nani
    May 13 at 15:14





    Thanks everyone. This is working

    – Nani
    May 13 at 15:14




    1




    1





    [][] is something of a regex corner-case and might warrant some explaining.

    – Digital Trauma
    May 14 at 0:41





    [][] is something of a regex corner-case and might warrant some explaining.

    – Digital Trauma
    May 14 at 0:41













    3














    This will extract all characters before last 'colon' and remove the brackets [ ] as the example you give.



    rev <yourfile.txt | cut -d: -f2- | rev | tr -d '[]'


    Replace yourfile.txt by your file name or remove the word <yourfile.txt to read the standard output.






    share|improve this answer




















    • 1





      Note that rev is not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted [] will cause an error in csh or zsh.

      – Stéphane Chazelas
      May 13 at 15:17











    • Right I corrected it with quotes. In practice there are some unixes without rev or even some busybox environment that does not have it?

      – Luciano Andress Martini
      May 13 at 15:25







    • 2





      There's none on Solaris. BSDs, GNU, busybox, AIX and HPUX seem to all have one.

      – Stéphane Chazelas
      May 13 at 15:26











    • Thank you for the information!

      – Luciano Andress Martini
      May 13 at 15:26















    3














    This will extract all characters before last 'colon' and remove the brackets [ ] as the example you give.



    rev <yourfile.txt | cut -d: -f2- | rev | tr -d '[]'


    Replace yourfile.txt by your file name or remove the word <yourfile.txt to read the standard output.






    share|improve this answer




















    • 1





      Note that rev is not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted [] will cause an error in csh or zsh.

      – Stéphane Chazelas
      May 13 at 15:17











    • Right I corrected it with quotes. In practice there are some unixes without rev or even some busybox environment that does not have it?

      – Luciano Andress Martini
      May 13 at 15:25







    • 2





      There's none on Solaris. BSDs, GNU, busybox, AIX and HPUX seem to all have one.

      – Stéphane Chazelas
      May 13 at 15:26











    • Thank you for the information!

      – Luciano Andress Martini
      May 13 at 15:26













    3












    3








    3







    This will extract all characters before last 'colon' and remove the brackets [ ] as the example you give.



    rev <yourfile.txt | cut -d: -f2- | rev | tr -d '[]'


    Replace yourfile.txt by your file name or remove the word <yourfile.txt to read the standard output.






    share|improve this answer















    This will extract all characters before last 'colon' and remove the brackets [ ] as the example you give.



    rev <yourfile.txt | cut -d: -f2- | rev | tr -d '[]'


    Replace yourfile.txt by your file name or remove the word <yourfile.txt to read the standard output.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 13 at 15:18

























    answered May 13 at 15:05









    Luciano Andress MartiniLuciano Andress Martini

    4,3601338




    4,3601338







    • 1





      Note that rev is not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted [] will cause an error in csh or zsh.

      – Stéphane Chazelas
      May 13 at 15:17











    • Right I corrected it with quotes. In practice there are some unixes without rev or even some busybox environment that does not have it?

      – Luciano Andress Martini
      May 13 at 15:25







    • 2





      There's none on Solaris. BSDs, GNU, busybox, AIX and HPUX seem to all have one.

      – Stéphane Chazelas
      May 13 at 15:26











    • Thank you for the information!

      – Luciano Andress Martini
      May 13 at 15:26












    • 1





      Note that rev is not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted [] will cause an error in csh or zsh.

      – Stéphane Chazelas
      May 13 at 15:17











    • Right I corrected it with quotes. In practice there are some unixes without rev or even some busybox environment that does not have it?

      – Luciano Andress Martini
      May 13 at 15:25







    • 2





      There's none on Solaris. BSDs, GNU, busybox, AIX and HPUX seem to all have one.

      – Stéphane Chazelas
      May 13 at 15:26











    • Thank you for the information!

      – Luciano Andress Martini
      May 13 at 15:26







    1




    1





    Note that rev is not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted [] will cause an error in csh or zsh.

    – Stéphane Chazelas
    May 13 at 15:17





    Note that rev is not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted [] will cause an error in csh or zsh.

    – Stéphane Chazelas
    May 13 at 15:17













    Right I corrected it with quotes. In practice there are some unixes without rev or even some busybox environment that does not have it?

    – Luciano Andress Martini
    May 13 at 15:25






    Right I corrected it with quotes. In practice there are some unixes without rev or even some busybox environment that does not have it?

    – Luciano Andress Martini
    May 13 at 15:25





    2




    2





    There's none on Solaris. BSDs, GNU, busybox, AIX and HPUX seem to all have one.

    – Stéphane Chazelas
    May 13 at 15:26





    There's none on Solaris. BSDs, GNU, busybox, AIX and HPUX seem to all have one.

    – Stéphane Chazelas
    May 13 at 15:26













    Thank you for the information!

    – Luciano Andress Martini
    May 13 at 15:26





    Thank you for the information!

    – Luciano Andress Martini
    May 13 at 15:26











    2














    Shell only:



    while IFS= read -r line; do
    tmp=$line%:* # remove last colon and following chars
    tmp=$tmp#"[" # remove leading open bracket
    result=$tmp%"]" # remove trailing close bracket
    printf "%sn" "$result"
    done < file





    share|improve this answer

























    • I don't know why but I always love this kind of solutions... Just because it does run with a minimal tools environment.

      – Luciano Andress Martini
      May 13 at 15:19












    • And I always cringe at them. Here, you can also add a portability dimension as those $tmp#[ won't work with ksh93 or zsh.

      – Stéphane Chazelas
      May 13 at 15:28












    • @StéphaneChazelas In true I agree with you in security / reliability terms. But I think you understand my point of view too.

      – Luciano Andress Martini
      May 13 at 15:30







    • 1





      Seems to work fine with ksh "version sh (AT&T Research) 93u+ 2012-08-01". I don't have an older version to test with.

      – glenn jackman
      May 13 at 15:44







    • 1





      ksh -c 'tmp=$tmp#[' gives a syntax error with ksh93u+, but tmp=anything ksh -c 'tmp=$tmp#[' doesn't. Looks like a bug. In any case, [ being a wildcard operator is better quoted. tmp=$tmp#"[" works OK in any POSIX-like shell. You'd want to replace echo with printf '%sn' as well.

      – Stéphane Chazelas
      May 13 at 19:00
















    2














    Shell only:



    while IFS= read -r line; do
    tmp=$line%:* # remove last colon and following chars
    tmp=$tmp#"[" # remove leading open bracket
    result=$tmp%"]" # remove trailing close bracket
    printf "%sn" "$result"
    done < file





    share|improve this answer

























    • I don't know why but I always love this kind of solutions... Just because it does run with a minimal tools environment.

      – Luciano Andress Martini
      May 13 at 15:19












    • And I always cringe at them. Here, you can also add a portability dimension as those $tmp#[ won't work with ksh93 or zsh.

      – Stéphane Chazelas
      May 13 at 15:28












    • @StéphaneChazelas In true I agree with you in security / reliability terms. But I think you understand my point of view too.

      – Luciano Andress Martini
      May 13 at 15:30







    • 1





      Seems to work fine with ksh "version sh (AT&T Research) 93u+ 2012-08-01". I don't have an older version to test with.

      – glenn jackman
      May 13 at 15:44







    • 1





      ksh -c 'tmp=$tmp#[' gives a syntax error with ksh93u+, but tmp=anything ksh -c 'tmp=$tmp#[' doesn't. Looks like a bug. In any case, [ being a wildcard operator is better quoted. tmp=$tmp#"[" works OK in any POSIX-like shell. You'd want to replace echo with printf '%sn' as well.

      – Stéphane Chazelas
      May 13 at 19:00














    2












    2








    2







    Shell only:



    while IFS= read -r line; do
    tmp=$line%:* # remove last colon and following chars
    tmp=$tmp#"[" # remove leading open bracket
    result=$tmp%"]" # remove trailing close bracket
    printf "%sn" "$result"
    done < file





    share|improve this answer















    Shell only:



    while IFS= read -r line; do
    tmp=$line%:* # remove last colon and following chars
    tmp=$tmp#"[" # remove leading open bracket
    result=$tmp%"]" # remove trailing close bracket
    printf "%sn" "$result"
    done < file






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 13 at 20:17

























    answered May 13 at 15:19









    glenn jackmanglenn jackman

    53.8k674115




    53.8k674115












    • I don't know why but I always love this kind of solutions... Just because it does run with a minimal tools environment.

      – Luciano Andress Martini
      May 13 at 15:19












    • And I always cringe at them. Here, you can also add a portability dimension as those $tmp#[ won't work with ksh93 or zsh.

      – Stéphane Chazelas
      May 13 at 15:28












    • @StéphaneChazelas In true I agree with you in security / reliability terms. But I think you understand my point of view too.

      – Luciano Andress Martini
      May 13 at 15:30







    • 1





      Seems to work fine with ksh "version sh (AT&T Research) 93u+ 2012-08-01". I don't have an older version to test with.

      – glenn jackman
      May 13 at 15:44







    • 1





      ksh -c 'tmp=$tmp#[' gives a syntax error with ksh93u+, but tmp=anything ksh -c 'tmp=$tmp#[' doesn't. Looks like a bug. In any case, [ being a wildcard operator is better quoted. tmp=$tmp#"[" works OK in any POSIX-like shell. You'd want to replace echo with printf '%sn' as well.

      – Stéphane Chazelas
      May 13 at 19:00


















    • I don't know why but I always love this kind of solutions... Just because it does run with a minimal tools environment.

      – Luciano Andress Martini
      May 13 at 15:19












    • And I always cringe at them. Here, you can also add a portability dimension as those $tmp#[ won't work with ksh93 or zsh.

      – Stéphane Chazelas
      May 13 at 15:28












    • @StéphaneChazelas In true I agree with you in security / reliability terms. But I think you understand my point of view too.

      – Luciano Andress Martini
      May 13 at 15:30







    • 1





      Seems to work fine with ksh "version sh (AT&T Research) 93u+ 2012-08-01". I don't have an older version to test with.

      – glenn jackman
      May 13 at 15:44







    • 1





      ksh -c 'tmp=$tmp#[' gives a syntax error with ksh93u+, but tmp=anything ksh -c 'tmp=$tmp#[' doesn't. Looks like a bug. In any case, [ being a wildcard operator is better quoted. tmp=$tmp#"[" works OK in any POSIX-like shell. You'd want to replace echo with printf '%sn' as well.

      – Stéphane Chazelas
      May 13 at 19:00

















    I don't know why but I always love this kind of solutions... Just because it does run with a minimal tools environment.

    – Luciano Andress Martini
    May 13 at 15:19






    I don't know why but I always love this kind of solutions... Just because it does run with a minimal tools environment.

    – Luciano Andress Martini
    May 13 at 15:19














    And I always cringe at them. Here, you can also add a portability dimension as those $tmp#[ won't work with ksh93 or zsh.

    – Stéphane Chazelas
    May 13 at 15:28






    And I always cringe at them. Here, you can also add a portability dimension as those $tmp#[ won't work with ksh93 or zsh.

    – Stéphane Chazelas
    May 13 at 15:28














    @StéphaneChazelas In true I agree with you in security / reliability terms. But I think you understand my point of view too.

    – Luciano Andress Martini
    May 13 at 15:30






    @StéphaneChazelas In true I agree with you in security / reliability terms. But I think you understand my point of view too.

    – Luciano Andress Martini
    May 13 at 15:30





    1




    1





    Seems to work fine with ksh "version sh (AT&T Research) 93u+ 2012-08-01". I don't have an older version to test with.

    – glenn jackman
    May 13 at 15:44






    Seems to work fine with ksh "version sh (AT&T Research) 93u+ 2012-08-01". I don't have an older version to test with.

    – glenn jackman
    May 13 at 15:44





    1




    1





    ksh -c 'tmp=$tmp#[' gives a syntax error with ksh93u+, but tmp=anything ksh -c 'tmp=$tmp#[' doesn't. Looks like a bug. In any case, [ being a wildcard operator is better quoted. tmp=$tmp#"[" works OK in any POSIX-like shell. You'd want to replace echo with printf '%sn' as well.

    – Stéphane Chazelas
    May 13 at 19:00






    ksh -c 'tmp=$tmp#[' gives a syntax error with ksh93u+, but tmp=anything ksh -c 'tmp=$tmp#[' doesn't. Looks like a bug. In any case, [ being a wildcard operator is better quoted. tmp=$tmp#"[" works OK in any POSIX-like shell. You'd want to replace echo with printf '%sn' as well.

    – Stéphane Chazelas
    May 13 at 19:00












    2














    awk -F: 'OFS=":"; NF--; print $0' $file



    or



    cat file | awk -F: 'OFS=":"; NF--; print $0'



    which breaks down as:




    • -F: set the input field separator to :


    • OFS=":" set the output field separator to :


    • NF-- reduce the Number of Fields by 1 (get rid of the last field)


    • print $0 print the remaining records, separated by the OFS (:) character.

    Update to also remove the square brackets:



    awk -F: 'OFS=":"; NF--; gsub(/[' $file



    • added gsub(/[|]/, "")1 which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.





    share|improve this answer





























      2














      awk -F: 'OFS=":"; NF--; print $0' $file



      or



      cat file | awk -F: 'OFS=":"; NF--; print $0'



      which breaks down as:




      • -F: set the input field separator to :


      • OFS=":" set the output field separator to :


      • NF-- reduce the Number of Fields by 1 (get rid of the last field)


      • print $0 print the remaining records, separated by the OFS (:) character.

      Update to also remove the square brackets:



      awk -F: 'OFS=":"; NF--; gsub(/[' $file



      • added gsub(/[|]/, "")1 which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.





      share|improve this answer



























        2












        2








        2







        awk -F: 'OFS=":"; NF--; print $0' $file



        or



        cat file | awk -F: 'OFS=":"; NF--; print $0'



        which breaks down as:




        • -F: set the input field separator to :


        • OFS=":" set the output field separator to :


        • NF-- reduce the Number of Fields by 1 (get rid of the last field)


        • print $0 print the remaining records, separated by the OFS (:) character.

        Update to also remove the square brackets:



        awk -F: 'OFS=":"; NF--; gsub(/[' $file



        • added gsub(/[|]/, "")1 which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.





        share|improve this answer















        awk -F: 'OFS=":"; NF--; print $0' $file



        or



        cat file | awk -F: 'OFS=":"; NF--; print $0'



        which breaks down as:




        • -F: set the input field separator to :


        • OFS=":" set the output field separator to :


        • NF-- reduce the Number of Fields by 1 (get rid of the last field)


        • print $0 print the remaining records, separated by the OFS (:) character.

        Update to also remove the square brackets:



        awk -F: 'OFS=":"; NF--; gsub(/[' $file



        • added gsub(/[|]/, "")1 which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 13 at 20:42

























        answered May 13 at 15:07









        Tim KennedyTim Kennedy

        14.9k23152




        14.9k23152





















            0














            Command:



            awk -F ":" 'OFS=":"$NF="";print $0' filename | sed "s/:$//g"| sed "s/^[//g"|sed "s/]//g"


            output



            256.XXX.XXX.X
            214.XXX.XXX.X
            2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX





            share|improve this answer


















            • 1





              This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t need sed too...

              – Stephen Kitt
              May 15 at 16:38















            0














            Command:



            awk -F ":" 'OFS=":"$NF="";print $0' filename | sed "s/:$//g"| sed "s/^[//g"|sed "s/]//g"


            output



            256.XXX.XXX.X
            214.XXX.XXX.X
            2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX





            share|improve this answer


















            • 1





              This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t need sed too...

              – Stephen Kitt
              May 15 at 16:38













            0












            0








            0







            Command:



            awk -F ":" 'OFS=":"$NF="";print $0' filename | sed "s/:$//g"| sed "s/^[//g"|sed "s/]//g"


            output



            256.XXX.XXX.X
            214.XXX.XXX.X
            2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX





            share|improve this answer













            Command:



            awk -F ":" 'OFS=":"$NF="";print $0' filename | sed "s/:$//g"| sed "s/^[//g"|sed "s/]//g"


            output



            256.XXX.XXX.X
            214.XXX.XXX.X
            2200:XXXX:XXXX:XXX:XXXX:XXXX:XXXX:XXXX






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 15 at 15:51









            Praveen Kumar BSPraveen Kumar BS

            1,9092311




            1,9092311







            • 1





              This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t need sed too...

              – Stephen Kitt
              May 15 at 16:38












            • 1





              This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t need sed too...

              – Stephen Kitt
              May 15 at 16:38







            1




            1





            This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t need sed too...

            – Stephen Kitt
            May 15 at 16:38





            This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t need sed too...

            – Stephen Kitt
            May 15 at 16:38

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f518698%2fextract-the-characters-before-last-colon%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

            What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

            Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?