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;
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
add a comment |
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
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 thatnetstatresult which contains IP and port?
– Ivan Chau
May 14 at 6:15
add a comment |
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
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
text-processing awk sed
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 thatnetstatresult which contains IP and port?
– Ivan Chau
May 14 at 6:15
add a comment |
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 thatnetstatresult 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
add a comment |
5 Answers
5
active
oldest
votes
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).
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
add a comment |
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.
1
Note thatrevis not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted[]will cause an error incshorzsh.
– 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
add a comment |
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
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+, buttmp=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 replaceechowithprintf '%sn'as well.
– Stéphane Chazelas
May 13 at 19:00
add a comment |
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 $0print the remaining records, separated by the OFS (:) character.
Update to also remove the square brackets:
awk -F: 'OFS=":"; NF--; gsub(/[' $file
- added
gsub(/[|]/, "")1which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.
add a comment |
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
1
This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t needsedtoo...
– Stephen Kitt
May 15 at 16:38
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
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
add a comment |
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).
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
add a comment |
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).
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).
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
add a comment |
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
add a comment |
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.
1
Note thatrevis not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted[]will cause an error incshorzsh.
– 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
add a comment |
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.
1
Note thatrevis not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted[]will cause an error incshorzsh.
– 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
add a comment |
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.
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.
edited May 13 at 15:18
answered May 13 at 15:05
Luciano Andress MartiniLuciano Andress Martini
4,3601338
4,3601338
1
Note thatrevis not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted[]will cause an error incshorzsh.
– 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
add a comment |
1
Note thatrevis not a standard (POSIX) command (comes from plan9, GNU has one as well). That unquoted[]will cause an error incshorzsh.
– 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
add a comment |
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
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+, buttmp=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 replaceechowithprintf '%sn'as well.
– Stéphane Chazelas
May 13 at 19:00
add a comment |
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
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+, buttmp=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 replaceechowithprintf '%sn'as well.
– Stéphane Chazelas
May 13 at 19:00
add a comment |
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
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
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+, buttmp=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 replaceechowithprintf '%sn'as well.
– Stéphane Chazelas
May 13 at 19:00
add a comment |
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+, buttmp=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 replaceechowithprintf '%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
add a comment |
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 $0print the remaining records, separated by the OFS (:) character.
Update to also remove the square brackets:
awk -F: 'OFS=":"; NF--; gsub(/[' $file
- added
gsub(/[|]/, "")1which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.
add a comment |
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 $0print the remaining records, separated by the OFS (:) character.
Update to also remove the square brackets:
awk -F: 'OFS=":"; NF--; gsub(/[' $file
- added
gsub(/[|]/, "")1which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.
add a comment |
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 $0print the remaining records, separated by the OFS (:) character.
Update to also remove the square brackets:
awk -F: 'OFS=":"; NF--; gsub(/[' $file
- added
gsub(/[|]/, "")1which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.
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 $0print the remaining records, separated by the OFS (:) character.
Update to also remove the square brackets:
awk -F: 'OFS=":"; NF--; gsub(/[' $file
- added
gsub(/[|]/, "")1which performs a global substitution on the square brackets, replacing them with nothing, and returning the substituted string.
edited May 13 at 20:42
answered May 13 at 15:07
Tim KennedyTim Kennedy
14.9k23152
14.9k23152
add a comment |
add a comment |
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
1
This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t needsedtoo...
– Stephen Kitt
May 15 at 16:38
add a comment |
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
1
This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t needsedtoo...
– Stephen Kitt
May 15 at 16:38
add a comment |
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
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
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 needsedtoo...
– Stephen Kitt
May 15 at 16:38
add a comment |
1
This doesn’t seem any better than Tim Kennedy’s existing answer. For future reference, if you’re using AWK, you don’t needsedtoo...
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
netstatresult which contains IP and port?– Ivan Chau
May 14 at 6:15