Read line by line from a text file and print how I want in shell scripting
I want to read below file line by line from a text file and print how I want in shell scripting
Text file content:
zero#123456
one#123
two#12345678
I want to print this as:
zero@1-6
one@1-3
two@1-8
I tried the following:
file="readFile.txt"
while IFS= read -r line
do echo "$line"
done <printf '%sn' "$file"
shell
add a comment |
I want to read below file line by line from a text file and print how I want in shell scripting
Text file content:
zero#123456
one#123
two#12345678
I want to print this as:
zero@1-6
one@1-3
two@1-8
I tried the following:
file="readFile.txt"
while IFS= read -r line
do echo "$line"
done <printf '%sn' "$file"
shell
1
And if you havethree#123789
? Should the result bethree@1-9
or something likethree@1-3,7-9
?
– Dominique
Nov 15 '18 at 10:54
add a comment |
I want to read below file line by line from a text file and print how I want in shell scripting
Text file content:
zero#123456
one#123
two#12345678
I want to print this as:
zero@1-6
one@1-3
two@1-8
I tried the following:
file="readFile.txt"
while IFS= read -r line
do echo "$line"
done <printf '%sn' "$file"
shell
I want to read below file line by line from a text file and print how I want in shell scripting
Text file content:
zero#123456
one#123
two#12345678
I want to print this as:
zero@1-6
one@1-3
two@1-8
I tried the following:
file="readFile.txt"
while IFS= read -r line
do echo "$line"
done <printf '%sn' "$file"
shell
shell
edited Nov 15 '18 at 9:00
tso
3,11821025
3,11821025
asked Nov 15 '18 at 8:57
Santosh KumarSantosh Kumar
161
161
1
And if you havethree#123789
? Should the result bethree@1-9
or something likethree@1-3,7-9
?
– Dominique
Nov 15 '18 at 10:54
add a comment |
1
And if you havethree#123789
? Should the result bethree@1-9
or something likethree@1-3,7-9
?
– Dominique
Nov 15 '18 at 10:54
1
1
And if you have
three#123789
? Should the result be three@1-9
or something like three@1-3,7-9
?– Dominique
Nov 15 '18 at 10:54
And if you have
three#123789
? Should the result be three@1-9
or something like three@1-3,7-9
?– Dominique
Nov 15 '18 at 10:54
add a comment |
3 Answers
3
active
oldest
votes
Create a script like below: my_print.sh
file="readFile.txt"
while IFS= read -r line
do
one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
done <"$file"
Run it like:
mayankp@mayank:~/$ sh my_print.sh
mayankp@mayank:~/$ cat output.txt
zero@1-6
one@1-3
two@1-8
Let me know of this helps.
It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5
– Santosh Kumar
Nov 15 '18 at 9:45
I am writing the output in a fileoutput.txt
. If you just want to print it, check my updated answer.
– Mayank Porwal
Nov 15 '18 at 9:46
Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt
– Santosh Kumar
Nov 15 '18 at 9:59
I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.
– Mayank Porwal
Nov 15 '18 at 10:03
Thanks mayank. Could you please explain the steps. It will be helpfull
– Santosh Kumar
Nov 15 '18 at 10:22
|
show 2 more comments
It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:
$ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
Text file content:
zero#1-6
one#1-3
two#1-8
Explained some:
s//-/
replace with a-
(?<=[0-9])
positive lookbehind, if preceeded by a number(?=[0-9])
positive lookahead, if followed by a number
add a comment |
With sed:
sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt
-r
: using extented regular expressions (just to write some stuff without escaping them by a backslash)s/expr1/expr2/
: substituteexpr1
byexpr2
epxr1
is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).epxr2
retrieves captured strings (1
,2
,3
) and insert them in a formatted output (the one you wanted).
Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.
Update: Also you could do that with awk:
awk -F'#' '
gsub(/s*/,"", $2) ;
print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
' < test.txt
I added a gsub()
call because your file seems to have trailing blank characters.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53315645%2fread-line-by-line-from-a-text-file-and-print-how-i-want-in-shell-scripting%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
Create a script like below: my_print.sh
file="readFile.txt"
while IFS= read -r line
do
one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
done <"$file"
Run it like:
mayankp@mayank:~/$ sh my_print.sh
mayankp@mayank:~/$ cat output.txt
zero@1-6
one@1-3
two@1-8
Let me know of this helps.
It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5
– Santosh Kumar
Nov 15 '18 at 9:45
I am writing the output in a fileoutput.txt
. If you just want to print it, check my updated answer.
– Mayank Porwal
Nov 15 '18 at 9:46
Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt
– Santosh Kumar
Nov 15 '18 at 9:59
I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.
– Mayank Porwal
Nov 15 '18 at 10:03
Thanks mayank. Could you please explain the steps. It will be helpfull
– Santosh Kumar
Nov 15 '18 at 10:22
|
show 2 more comments
Create a script like below: my_print.sh
file="readFile.txt"
while IFS= read -r line
do
one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
done <"$file"
Run it like:
mayankp@mayank:~/$ sh my_print.sh
mayankp@mayank:~/$ cat output.txt
zero@1-6
one@1-3
two@1-8
Let me know of this helps.
It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5
– Santosh Kumar
Nov 15 '18 at 9:45
I am writing the output in a fileoutput.txt
. If you just want to print it, check my updated answer.
– Mayank Porwal
Nov 15 '18 at 9:46
Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt
– Santosh Kumar
Nov 15 '18 at 9:59
I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.
– Mayank Porwal
Nov 15 '18 at 10:03
Thanks mayank. Could you please explain the steps. It will be helpfull
– Santosh Kumar
Nov 15 '18 at 10:22
|
show 2 more comments
Create a script like below: my_print.sh
file="readFile.txt"
while IFS= read -r line
do
one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
done <"$file"
Run it like:
mayankp@mayank:~/$ sh my_print.sh
mayankp@mayank:~/$ cat output.txt
zero@1-6
one@1-3
two@1-8
Let me know of this helps.
Create a script like below: my_print.sh
file="readFile.txt"
while IFS= read -r line
do
one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
done <"$file"
Run it like:
mayankp@mayank:~/$ sh my_print.sh
mayankp@mayank:~/$ cat output.txt
zero@1-6
one@1-3
two@1-8
Let me know of this helps.
edited Nov 15 '18 at 10:28
answered Nov 15 '18 at 9:28
Mayank PorwalMayank Porwal
4,9702725
4,9702725
It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5
– Santosh Kumar
Nov 15 '18 at 9:45
I am writing the output in a fileoutput.txt
. If you just want to print it, check my updated answer.
– Mayank Porwal
Nov 15 '18 at 9:46
Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt
– Santosh Kumar
Nov 15 '18 at 9:59
I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.
– Mayank Porwal
Nov 15 '18 at 10:03
Thanks mayank. Could you please explain the steps. It will be helpfull
– Santosh Kumar
Nov 15 '18 at 10:22
|
show 2 more comments
It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5
– Santosh Kumar
Nov 15 '18 at 9:45
I am writing the output in a fileoutput.txt
. If you just want to print it, check my updated answer.
– Mayank Porwal
Nov 15 '18 at 9:46
Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt
– Santosh Kumar
Nov 15 '18 at 9:59
I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.
– Mayank Porwal
Nov 15 '18 at 10:03
Thanks mayank. Could you please explain the steps. It will be helpfull
– Santosh Kumar
Nov 15 '18 at 10:22
It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5
– Santosh Kumar
Nov 15 '18 at 9:45
It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5
– Santosh Kumar
Nov 15 '18 at 9:45
I am writing the output in a file
output.txt
. If you just want to print it, check my updated answer.– Mayank Porwal
Nov 15 '18 at 9:46
I am writing the output in a file
output.txt
. If you just want to print it, check my updated answer.– Mayank Porwal
Nov 15 '18 at 9:46
Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt
– Santosh Kumar
Nov 15 '18 at 9:59
Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt
– Santosh Kumar
Nov 15 '18 at 9:59
I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.
– Mayank Porwal
Nov 15 '18 at 10:03
I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.
– Mayank Porwal
Nov 15 '18 at 10:03
Thanks mayank. Could you please explain the steps. It will be helpfull
– Santosh Kumar
Nov 15 '18 at 10:22
Thanks mayank. Could you please explain the steps. It will be helpfull
– Santosh Kumar
Nov 15 '18 at 10:22
|
show 2 more comments
It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:
$ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
Text file content:
zero#1-6
one#1-3
two#1-8
Explained some:
s//-/
replace with a-
(?<=[0-9])
positive lookbehind, if preceeded by a number(?=[0-9])
positive lookahead, if followed by a number
add a comment |
It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:
$ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
Text file content:
zero#1-6
one#1-3
two#1-8
Explained some:
s//-/
replace with a-
(?<=[0-9])
positive lookbehind, if preceeded by a number(?=[0-9])
positive lookahead, if followed by a number
add a comment |
It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:
$ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
Text file content:
zero#1-6
one#1-3
two#1-8
Explained some:
s//-/
replace with a-
(?<=[0-9])
positive lookbehind, if preceeded by a number(?=[0-9])
positive lookahead, if followed by a number
It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:
$ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
Text file content:
zero#1-6
one#1-3
two#1-8
Explained some:
s//-/
replace with a-
(?<=[0-9])
positive lookbehind, if preceeded by a number(?=[0-9])
positive lookahead, if followed by a number
edited Nov 15 '18 at 10:51
answered Nov 15 '18 at 10:45
James BrownJames Brown
19.6k31836
19.6k31836
add a comment |
add a comment |
With sed:
sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt
-r
: using extented regular expressions (just to write some stuff without escaping them by a backslash)s/expr1/expr2/
: substituteexpr1
byexpr2
epxr1
is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).epxr2
retrieves captured strings (1
,2
,3
) and insert them in a formatted output (the one you wanted).
Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.
Update: Also you could do that with awk:
awk -F'#' '
gsub(/s*/,"", $2) ;
print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
' < test.txt
I added a gsub()
call because your file seems to have trailing blank characters.
add a comment |
With sed:
sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt
-r
: using extented regular expressions (just to write some stuff without escaping them by a backslash)s/expr1/expr2/
: substituteexpr1
byexpr2
epxr1
is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).epxr2
retrieves captured strings (1
,2
,3
) and insert them in a formatted output (the one you wanted).
Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.
Update: Also you could do that with awk:
awk -F'#' '
gsub(/s*/,"", $2) ;
print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
' < test.txt
I added a gsub()
call because your file seems to have trailing blank characters.
add a comment |
With sed:
sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt
-r
: using extented regular expressions (just to write some stuff without escaping them by a backslash)s/expr1/expr2/
: substituteexpr1
byexpr2
epxr1
is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).epxr2
retrieves captured strings (1
,2
,3
) and insert them in a formatted output (the one you wanted).
Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.
Update: Also you could do that with awk:
awk -F'#' '
gsub(/s*/,"", $2) ;
print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
' < test.txt
I added a gsub()
call because your file seems to have trailing blank characters.
With sed:
sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt
-r
: using extented regular expressions (just to write some stuff without escaping them by a backslash)s/expr1/expr2/
: substituteexpr1
byexpr2
epxr1
is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).epxr2
retrieves captured strings (1
,2
,3
) and insert them in a formatted output (the one you wanted).
Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.
Update: Also you could do that with awk:
awk -F'#' '
gsub(/s*/,"", $2) ;
print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
' < test.txt
I added a gsub()
call because your file seems to have trailing blank characters.
edited Nov 15 '18 at 11:01
answered Nov 15 '18 at 9:51
AmessihelAmessihel
2,6641825
2,6641825
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53315645%2fread-line-by-line-from-a-text-file-and-print-how-i-want-in-shell-scripting%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
And if you have
three#123789
? Should the result bethree@1-9
or something likethree@1-3,7-9
?– Dominique
Nov 15 '18 at 10:54