Concatenate strings with literal tab and new line character
Clash Royale CLAN TAG#URR8PPP
I've a tab/space delimited file as:
31000BL 50014_10011
25467BL 50050_10003
47406BL 50001_10015
40831BL 50114_10006
40830BL 50114_10009
Two columns. I use a while loop to read this:
while read LINE; do
printf "$LINEn"
old=($(echo $LINE | awk 'print $1'))
new=($(echo $LINE | awk 'print $2'))
ll=$old'^V<tab>'$new #I need to work here.
printf "$lln"
done < update_ids.txt
The output I'd like is
31000BLt50014_10011n25467BLt50050_10003n
There is a literal tab and new line character. I cannot concatenate or print with tab and/or new line character.
shell-script shell text-formatting
add a comment |
I've a tab/space delimited file as:
31000BL 50014_10011
25467BL 50050_10003
47406BL 50001_10015
40831BL 50114_10006
40830BL 50114_10009
Two columns. I use a while loop to read this:
while read LINE; do
printf "$LINEn"
old=($(echo $LINE | awk 'print $1'))
new=($(echo $LINE | awk 'print $2'))
ll=$old'^V<tab>'$new #I need to work here.
printf "$lln"
done < update_ids.txt
The output I'd like is
31000BLt50014_10011n25467BLt50050_10003n
There is a literal tab and new line character. I cannot concatenate or print with tab and/or new line character.
shell-script shell text-formatting
add a comment |
I've a tab/space delimited file as:
31000BL 50014_10011
25467BL 50050_10003
47406BL 50001_10015
40831BL 50114_10006
40830BL 50114_10009
Two columns. I use a while loop to read this:
while read LINE; do
printf "$LINEn"
old=($(echo $LINE | awk 'print $1'))
new=($(echo $LINE | awk 'print $2'))
ll=$old'^V<tab>'$new #I need to work here.
printf "$lln"
done < update_ids.txt
The output I'd like is
31000BLt50014_10011n25467BLt50050_10003n
There is a literal tab and new line character. I cannot concatenate or print with tab and/or new line character.
shell-script shell text-formatting
I've a tab/space delimited file as:
31000BL 50014_10011
25467BL 50050_10003
47406BL 50001_10015
40831BL 50114_10006
40830BL 50114_10009
Two columns. I use a while loop to read this:
while read LINE; do
printf "$LINEn"
old=($(echo $LINE | awk 'print $1'))
new=($(echo $LINE | awk 'print $2'))
ll=$old'^V<tab>'$new #I need to work here.
printf "$lln"
done < update_ids.txt
The output I'd like is
31000BLt50014_10011n25467BLt50050_10003n
There is a literal tab and new line character. I cannot concatenate or print with tab and/or new line character.
shell-script shell text-formatting
shell-script shell text-formatting
edited Jan 25 at 21:44
ilkkachu
58.7k892165
58.7k892165
asked Jan 25 at 20:58
Death MetalDeath Metal
79211
79211
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I'm not quite sure that I understand correctly, but if you'd just want to make sure that there's a literal tab character between your two columns, you can do that in a single awk
invocation:
awk -v OFS='t' ' $1=$1; print ' file >newfile
Setting OFS
on the command line to t
sets the output field separator to a tab character. Using $1=$1
will force awk
to re-form the current record, and the subsequent print
would print it out with whatever whitespace it originally used as delimiter replaced with tabs.
The result is written to newfile
through the use of a redirection.
Alternatively, use a more explicit printf()
call:
awk ' printf("%st%sn", $1, $2) ' file >newfile
Note that this only handles exactly two columns, whereas the first awk
program would work with data containing multiple columns, without modification.
Related to the question:
- Why is using a shell loop to process text considered bad practice?
i think you mean$0=$0
in the first example.
– lesmana
Jan 28 at 16:27
@lesmana Do I? That would not causeawk
to re-form the current record, which means that the value ofOFS
would not be inserted between the fields.
– Kusalananda
Jan 28 at 16:49
i learned something new.$1=$1
causes rewriting of$0
withOFS
. here is an explanation: stackoverflow.com/a/41941143 thank you.
– lesmana
Jan 28 at 21:28
add a comment |
The shell read
command can read into multiple variables. You need to set the IFS
variable to be a tab, which in bash you do with ANSI-C Quoting: IFS=$'t'
For output, the printf
builtin command is what you want.
while IFS=$'t' read -r old new; do
printf '%st%sn' "$old" "$new"
done < update_ids.txt
To save tab-separated data into a variable you can do:
var=$(printf '%st%sn' "$old" "$new")
# or
printf -v var '%st%sn' "$old" "$new"
# or this quoting disaster
var="$old"$'t'"$new"
ref: https://www.gnu.org/software/bash/manual/bashref.html#index-printf
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%2f496746%2fconcatenate-strings-with-literal-tab-and-new-line-character%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not quite sure that I understand correctly, but if you'd just want to make sure that there's a literal tab character between your two columns, you can do that in a single awk
invocation:
awk -v OFS='t' ' $1=$1; print ' file >newfile
Setting OFS
on the command line to t
sets the output field separator to a tab character. Using $1=$1
will force awk
to re-form the current record, and the subsequent print
would print it out with whatever whitespace it originally used as delimiter replaced with tabs.
The result is written to newfile
through the use of a redirection.
Alternatively, use a more explicit printf()
call:
awk ' printf("%st%sn", $1, $2) ' file >newfile
Note that this only handles exactly two columns, whereas the first awk
program would work with data containing multiple columns, without modification.
Related to the question:
- Why is using a shell loop to process text considered bad practice?
i think you mean$0=$0
in the first example.
– lesmana
Jan 28 at 16:27
@lesmana Do I? That would not causeawk
to re-form the current record, which means that the value ofOFS
would not be inserted between the fields.
– Kusalananda
Jan 28 at 16:49
i learned something new.$1=$1
causes rewriting of$0
withOFS
. here is an explanation: stackoverflow.com/a/41941143 thank you.
– lesmana
Jan 28 at 21:28
add a comment |
I'm not quite sure that I understand correctly, but if you'd just want to make sure that there's a literal tab character between your two columns, you can do that in a single awk
invocation:
awk -v OFS='t' ' $1=$1; print ' file >newfile
Setting OFS
on the command line to t
sets the output field separator to a tab character. Using $1=$1
will force awk
to re-form the current record, and the subsequent print
would print it out with whatever whitespace it originally used as delimiter replaced with tabs.
The result is written to newfile
through the use of a redirection.
Alternatively, use a more explicit printf()
call:
awk ' printf("%st%sn", $1, $2) ' file >newfile
Note that this only handles exactly two columns, whereas the first awk
program would work with data containing multiple columns, without modification.
Related to the question:
- Why is using a shell loop to process text considered bad practice?
i think you mean$0=$0
in the first example.
– lesmana
Jan 28 at 16:27
@lesmana Do I? That would not causeawk
to re-form the current record, which means that the value ofOFS
would not be inserted between the fields.
– Kusalananda
Jan 28 at 16:49
i learned something new.$1=$1
causes rewriting of$0
withOFS
. here is an explanation: stackoverflow.com/a/41941143 thank you.
– lesmana
Jan 28 at 21:28
add a comment |
I'm not quite sure that I understand correctly, but if you'd just want to make sure that there's a literal tab character between your two columns, you can do that in a single awk
invocation:
awk -v OFS='t' ' $1=$1; print ' file >newfile
Setting OFS
on the command line to t
sets the output field separator to a tab character. Using $1=$1
will force awk
to re-form the current record, and the subsequent print
would print it out with whatever whitespace it originally used as delimiter replaced with tabs.
The result is written to newfile
through the use of a redirection.
Alternatively, use a more explicit printf()
call:
awk ' printf("%st%sn", $1, $2) ' file >newfile
Note that this only handles exactly two columns, whereas the first awk
program would work with data containing multiple columns, without modification.
Related to the question:
- Why is using a shell loop to process text considered bad practice?
I'm not quite sure that I understand correctly, but if you'd just want to make sure that there's a literal tab character between your two columns, you can do that in a single awk
invocation:
awk -v OFS='t' ' $1=$1; print ' file >newfile
Setting OFS
on the command line to t
sets the output field separator to a tab character. Using $1=$1
will force awk
to re-form the current record, and the subsequent print
would print it out with whatever whitespace it originally used as delimiter replaced with tabs.
The result is written to newfile
through the use of a redirection.
Alternatively, use a more explicit printf()
call:
awk ' printf("%st%sn", $1, $2) ' file >newfile
Note that this only handles exactly two columns, whereas the first awk
program would work with data containing multiple columns, without modification.
Related to the question:
- Why is using a shell loop to process text considered bad practice?
edited Jan 25 at 21:30
answered Jan 25 at 21:07
KusalanandaKusalananda
129k17246404
129k17246404
i think you mean$0=$0
in the first example.
– lesmana
Jan 28 at 16:27
@lesmana Do I? That would not causeawk
to re-form the current record, which means that the value ofOFS
would not be inserted between the fields.
– Kusalananda
Jan 28 at 16:49
i learned something new.$1=$1
causes rewriting of$0
withOFS
. here is an explanation: stackoverflow.com/a/41941143 thank you.
– lesmana
Jan 28 at 21:28
add a comment |
i think you mean$0=$0
in the first example.
– lesmana
Jan 28 at 16:27
@lesmana Do I? That would not causeawk
to re-form the current record, which means that the value ofOFS
would not be inserted between the fields.
– Kusalananda
Jan 28 at 16:49
i learned something new.$1=$1
causes rewriting of$0
withOFS
. here is an explanation: stackoverflow.com/a/41941143 thank you.
– lesmana
Jan 28 at 21:28
i think you mean
$0=$0
in the first example.– lesmana
Jan 28 at 16:27
i think you mean
$0=$0
in the first example.– lesmana
Jan 28 at 16:27
@lesmana Do I? That would not cause
awk
to re-form the current record, which means that the value of OFS
would not be inserted between the fields.– Kusalananda
Jan 28 at 16:49
@lesmana Do I? That would not cause
awk
to re-form the current record, which means that the value of OFS
would not be inserted between the fields.– Kusalananda
Jan 28 at 16:49
i learned something new.
$1=$1
causes rewriting of $0
with OFS
. here is an explanation: stackoverflow.com/a/41941143 thank you.– lesmana
Jan 28 at 21:28
i learned something new.
$1=$1
causes rewriting of $0
with OFS
. here is an explanation: stackoverflow.com/a/41941143 thank you.– lesmana
Jan 28 at 21:28
add a comment |
The shell read
command can read into multiple variables. You need to set the IFS
variable to be a tab, which in bash you do with ANSI-C Quoting: IFS=$'t'
For output, the printf
builtin command is what you want.
while IFS=$'t' read -r old new; do
printf '%st%sn' "$old" "$new"
done < update_ids.txt
To save tab-separated data into a variable you can do:
var=$(printf '%st%sn' "$old" "$new")
# or
printf -v var '%st%sn' "$old" "$new"
# or this quoting disaster
var="$old"$'t'"$new"
ref: https://www.gnu.org/software/bash/manual/bashref.html#index-printf
add a comment |
The shell read
command can read into multiple variables. You need to set the IFS
variable to be a tab, which in bash you do with ANSI-C Quoting: IFS=$'t'
For output, the printf
builtin command is what you want.
while IFS=$'t' read -r old new; do
printf '%st%sn' "$old" "$new"
done < update_ids.txt
To save tab-separated data into a variable you can do:
var=$(printf '%st%sn' "$old" "$new")
# or
printf -v var '%st%sn' "$old" "$new"
# or this quoting disaster
var="$old"$'t'"$new"
ref: https://www.gnu.org/software/bash/manual/bashref.html#index-printf
add a comment |
The shell read
command can read into multiple variables. You need to set the IFS
variable to be a tab, which in bash you do with ANSI-C Quoting: IFS=$'t'
For output, the printf
builtin command is what you want.
while IFS=$'t' read -r old new; do
printf '%st%sn' "$old" "$new"
done < update_ids.txt
To save tab-separated data into a variable you can do:
var=$(printf '%st%sn' "$old" "$new")
# or
printf -v var '%st%sn' "$old" "$new"
# or this quoting disaster
var="$old"$'t'"$new"
ref: https://www.gnu.org/software/bash/manual/bashref.html#index-printf
The shell read
command can read into multiple variables. You need to set the IFS
variable to be a tab, which in bash you do with ANSI-C Quoting: IFS=$'t'
For output, the printf
builtin command is what you want.
while IFS=$'t' read -r old new; do
printf '%st%sn' "$old" "$new"
done < update_ids.txt
To save tab-separated data into a variable you can do:
var=$(printf '%st%sn' "$old" "$new")
# or
printf -v var '%st%sn' "$old" "$new"
# or this quoting disaster
var="$old"$'t'"$new"
ref: https://www.gnu.org/software/bash/manual/bashref.html#index-printf
edited Jan 25 at 22:00
answered Jan 25 at 21:52
glenn jackmanglenn jackman
51.5k572111
51.5k572111
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.
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%2f496746%2fconcatenate-strings-with-literal-tab-and-new-line-character%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