find string and print first and last characters of line
up vote
1
down vote
favorite
I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character.
For example, cat file1.txt
1234567 New line with irrelevant info x end line
2345678 irrelevant line
3456789 New line with different irrelevant info y end line
4567890 irrelevant line
5678901 New line with yet more irrelevant info z end line
And my output would be:
1234567 x
3456789 y
5678901 z
sed grep cut
add a comment |
up vote
1
down vote
favorite
I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character.
For example, cat file1.txt
1234567 New line with irrelevant info x end line
2345678 irrelevant line
3456789 New line with different irrelevant info y end line
4567890 irrelevant line
5678901 New line with yet more irrelevant info z end line
And my output would be:
1234567 x
3456789 y
5678901 z
sed grep cut
1
Expected output seems wrong .... second line starting with2345678
is irrelevant.
– George Vasiliou
Dec 1 at 21:36
1
... as is the third.
– RudiC
Dec 1 at 21:45
thanks, typo on my part. fixed now.
– user2535719
Dec 1 at 21:56
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character.
For example, cat file1.txt
1234567 New line with irrelevant info x end line
2345678 irrelevant line
3456789 New line with different irrelevant info y end line
4567890 irrelevant line
5678901 New line with yet more irrelevant info z end line
And my output would be:
1234567 x
3456789 y
5678901 z
sed grep cut
I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character.
For example, cat file1.txt
1234567 New line with irrelevant info x end line
2345678 irrelevant line
3456789 New line with different irrelevant info y end line
4567890 irrelevant line
5678901 New line with yet more irrelevant info z end line
And my output would be:
1234567 x
3456789 y
5678901 z
sed grep cut
sed grep cut
edited Dec 1 at 21:55
asked Dec 1 at 21:09
user2535719
62
62
1
Expected output seems wrong .... second line starting with2345678
is irrelevant.
– George Vasiliou
Dec 1 at 21:36
1
... as is the third.
– RudiC
Dec 1 at 21:45
thanks, typo on my part. fixed now.
– user2535719
Dec 1 at 21:56
add a comment |
1
Expected output seems wrong .... second line starting with2345678
is irrelevant.
– George Vasiliou
Dec 1 at 21:36
1
... as is the third.
– RudiC
Dec 1 at 21:45
thanks, typo on my part. fixed now.
– user2535719
Dec 1 at 21:56
1
1
Expected output seems wrong .... second line starting with
2345678
is irrelevant.– George Vasiliou
Dec 1 at 21:36
Expected output seems wrong .... second line starting with
2345678
is irrelevant.– George Vasiliou
Dec 1 at 21:36
1
1
... as is the third.
– RudiC
Dec 1 at 21:45
... as is the third.
– RudiC
Dec 1 at 21:45
thanks, typo on my part. fixed now.
– user2535719
Dec 1 at 21:56
thanks, typo on my part. fixed now.
– user2535719
Dec 1 at 21:56
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
Choose one you like:
awk
solution:
awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt
sed
solution:
sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt
Sample output (for both approaches):
1234567 x
3456789 y
5678901 z
add a comment |
up vote
1
down vote
POSIXly:
Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr()
, you can add a && length >= 10
or && length >= 17
after /New/
to skip the lines that have fewer than 10 or 17 characters):
awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'
or assuming the lines contain at least 17 characters (the lines that don't will be skipped):
sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'
1
What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
– George Vasiliou
Dec 1 at 21:53
@GeorgeVasiliou, he edited theawk
solution in after I had started writing mine and I hadn't seen his edit before I posted mine. Hissed
one is GNU specific, my intention was to offer a standard solution.
– Stéphane Chazelas
Dec 1 at 21:56
ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
– George Vasiliou
Dec 1 at 22:00
add a comment |
up vote
0
down vote
This is one "brutal" gawk
solution that does the job, using null
as field separator FS
and output field separator OFS
, meaning each char of inputfile is considered to be a field for awk.
awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
1234567 x
3456789 y
5678901 z
More solutions with grep / sed will follow.
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',
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%2f485398%2ffind-string-and-print-first-and-last-characters-of-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Choose one you like:
awk
solution:
awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt
sed
solution:
sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt
Sample output (for both approaches):
1234567 x
3456789 y
5678901 z
add a comment |
up vote
1
down vote
Choose one you like:
awk
solution:
awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt
sed
solution:
sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt
Sample output (for both approaches):
1234567 x
3456789 y
5678901 z
add a comment |
up vote
1
down vote
up vote
1
down vote
Choose one you like:
awk
solution:
awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt
sed
solution:
sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt
Sample output (for both approaches):
1234567 x
3456789 y
5678901 z
Choose one you like:
awk
solution:
awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt
sed
solution:
sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt
Sample output (for both approaches):
1234567 x
3456789 y
5678901 z
edited Dec 1 at 21:46
answered Dec 1 at 21:37
RomanPerekhrest
22.8k12246
22.8k12246
add a comment |
add a comment |
up vote
1
down vote
POSIXly:
Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr()
, you can add a && length >= 10
or && length >= 17
after /New/
to skip the lines that have fewer than 10 or 17 characters):
awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'
or assuming the lines contain at least 17 characters (the lines that don't will be skipped):
sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'
1
What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
– George Vasiliou
Dec 1 at 21:53
@GeorgeVasiliou, he edited theawk
solution in after I had started writing mine and I hadn't seen his edit before I posted mine. Hissed
one is GNU specific, my intention was to offer a standard solution.
– Stéphane Chazelas
Dec 1 at 21:56
ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
– George Vasiliou
Dec 1 at 22:00
add a comment |
up vote
1
down vote
POSIXly:
Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr()
, you can add a && length >= 10
or && length >= 17
after /New/
to skip the lines that have fewer than 10 or 17 characters):
awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'
or assuming the lines contain at least 17 characters (the lines that don't will be skipped):
sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'
1
What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
– George Vasiliou
Dec 1 at 21:53
@GeorgeVasiliou, he edited theawk
solution in after I had started writing mine and I hadn't seen his edit before I posted mine. Hissed
one is GNU specific, my intention was to offer a standard solution.
– Stéphane Chazelas
Dec 1 at 21:56
ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
– George Vasiliou
Dec 1 at 22:00
add a comment |
up vote
1
down vote
up vote
1
down vote
POSIXly:
Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr()
, you can add a && length >= 10
or && length >= 17
after /New/
to skip the lines that have fewer than 10 or 17 characters):
awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'
or assuming the lines contain at least 17 characters (the lines that don't will be skipped):
sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'
POSIXly:
Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr()
, you can add a && length >= 10
or && length >= 17
after /New/
to skip the lines that have fewer than 10 or 17 characters):
awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'
or assuming the lines contain at least 17 characters (the lines that don't will be skipped):
sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'
answered Dec 1 at 21:48
Stéphane Chazelas
297k54562908
297k54562908
1
What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
– George Vasiliou
Dec 1 at 21:53
@GeorgeVasiliou, he edited theawk
solution in after I had started writing mine and I hadn't seen his edit before I posted mine. Hissed
one is GNU specific, my intention was to offer a standard solution.
– Stéphane Chazelas
Dec 1 at 21:56
ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
– George Vasiliou
Dec 1 at 22:00
add a comment |
1
What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
– George Vasiliou
Dec 1 at 21:53
@GeorgeVasiliou, he edited theawk
solution in after I had started writing mine and I hadn't seen his edit before I posted mine. Hissed
one is GNU specific, my intention was to offer a standard solution.
– Stéphane Chazelas
Dec 1 at 21:56
ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
– George Vasiliou
Dec 1 at 22:00
1
1
What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
– George Vasiliou
Dec 1 at 21:53
What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
– George Vasiliou
Dec 1 at 21:53
@GeorgeVasiliou, he edited the
awk
solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed
one is GNU specific, my intention was to offer a standard solution.– Stéphane Chazelas
Dec 1 at 21:56
@GeorgeVasiliou, he edited the
awk
solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed
one is GNU specific, my intention was to offer a standard solution.– Stéphane Chazelas
Dec 1 at 21:56
ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
– George Vasiliou
Dec 1 at 22:00
ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
– George Vasiliou
Dec 1 at 22:00
add a comment |
up vote
0
down vote
This is one "brutal" gawk
solution that does the job, using null
as field separator FS
and output field separator OFS
, meaning each char of inputfile is considered to be a field for awk.
awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
1234567 x
3456789 y
5678901 z
More solutions with grep / sed will follow.
add a comment |
up vote
0
down vote
This is one "brutal" gawk
solution that does the job, using null
as field separator FS
and output field separator OFS
, meaning each char of inputfile is considered to be a field for awk.
awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
1234567 x
3456789 y
5678901 z
More solutions with grep / sed will follow.
add a comment |
up vote
0
down vote
up vote
0
down vote
This is one "brutal" gawk
solution that does the job, using null
as field separator FS
and output field separator OFS
, meaning each char of inputfile is considered to be a field for awk.
awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
1234567 x
3456789 y
5678901 z
More solutions with grep / sed will follow.
This is one "brutal" gawk
solution that does the job, using null
as field separator FS
and output field separator OFS
, meaning each char of inputfile is considered to be a field for awk.
awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
1234567 x
3456789 y
5678901 z
More solutions with grep / sed will follow.
answered Dec 1 at 21:33
George Vasiliou
5,59531028
5,59531028
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f485398%2ffind-string-and-print-first-and-last-characters-of-line%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
1
Expected output seems wrong .... second line starting with
2345678
is irrelevant.– George Vasiliou
Dec 1 at 21:36
1
... as is the third.
– RudiC
Dec 1 at 21:45
thanks, typo on my part. fixed now.
– user2535719
Dec 1 at 21:56