How to add quotes to line break field values in a file
Clash Royale CLAN TAG#URR8PPP
Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.
Input File:
100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24
Required output should be like below
"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"
text-processing
add a comment |
Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.
Input File:
100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24
Required output should be like below
"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"
text-processing
Are you able to modify the code or output format of whatever produces this data?
– Kusalananda
Jan 24 at 10:04
Wait, so there are linebreaks in the delimited strings?!
– Panki
Jan 24 at 10:16
Yes, OP says:second attribute contains new line characters
.
– RoVo
Jan 24 at 10:19
@RoVo what is "second attribute" anyway?
– Niko Gambt
Jan 24 at 11:45
1
You should ask OP ... but I guess it means second field of his|sv
.
– RoVo
Jan 24 at 11:58
add a comment |
Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.
Input File:
100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24
Required output should be like below
"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"
text-processing
Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.
Input File:
100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24
Required output should be like below
"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"
text-processing
text-processing
edited Jan 24 at 10:23
Jeff Schaller
41.3k1056131
41.3k1056131
asked Jan 24 at 9:59
chava kumarchava kumar
183
183
Are you able to modify the code or output format of whatever produces this data?
– Kusalananda
Jan 24 at 10:04
Wait, so there are linebreaks in the delimited strings?!
– Panki
Jan 24 at 10:16
Yes, OP says:second attribute contains new line characters
.
– RoVo
Jan 24 at 10:19
@RoVo what is "second attribute" anyway?
– Niko Gambt
Jan 24 at 11:45
1
You should ask OP ... but I guess it means second field of his|sv
.
– RoVo
Jan 24 at 11:58
add a comment |
Are you able to modify the code or output format of whatever produces this data?
– Kusalananda
Jan 24 at 10:04
Wait, so there are linebreaks in the delimited strings?!
– Panki
Jan 24 at 10:16
Yes, OP says:second attribute contains new line characters
.
– RoVo
Jan 24 at 10:19
@RoVo what is "second attribute" anyway?
– Niko Gambt
Jan 24 at 11:45
1
You should ask OP ... but I guess it means second field of his|sv
.
– RoVo
Jan 24 at 11:58
Are you able to modify the code or output format of whatever produces this data?
– Kusalananda
Jan 24 at 10:04
Are you able to modify the code or output format of whatever produces this data?
– Kusalananda
Jan 24 at 10:04
Wait, so there are linebreaks in the delimited strings?!
– Panki
Jan 24 at 10:16
Wait, so there are linebreaks in the delimited strings?!
– Panki
Jan 24 at 10:16
Yes, OP says:
second attribute contains new line characters
.– RoVo
Jan 24 at 10:19
Yes, OP says:
second attribute contains new line characters
.– RoVo
Jan 24 at 10:19
@RoVo what is "second attribute" anyway?
– Niko Gambt
Jan 24 at 11:45
@RoVo what is "second attribute" anyway?
– Niko Gambt
Jan 24 at 11:45
1
1
You should ask OP ... but I guess it means second field of his
|sv
.– RoVo
Jan 24 at 11:58
You should ask OP ... but I guess it means second field of his
|sv
.– RoVo
Jan 24 at 11:58
add a comment |
3 Answers
3
active
oldest
votes
You can try with this awk :
awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile
Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'
– chava kumar
Jan 26 at 11:46
@chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.
– ctac_
Jan 26 at 12:58
I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'
– chava kumar
Jan 26 at 14:08
Thanks a lot for your quick help.
– chava kumar
Jan 26 at 14:09
add a comment |
You can do it with perl
:
perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file
-0
read file at once, not line by line.-p
print the line in the end-e
the expressions/pattern/replacement/g
Replace pattern with replacement
add a comment |
$ cat input.txt | tr "n" "t"
|awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
|tr "t" "n"
tr
transform new line to a tab. So the input is linearized- We tell
awk
to handle the|
as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed tr
converts the tab back to a new line
Thank you for help, but this code didnt work in my case.
– chava kumar
Jan 26 at 11:59
"didn't work" is never a good error description. Could you please describe what doesn't work?
– finswimmer
Jan 26 at 12:07
Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"
– chava kumar
Jan 26 at 12:33
Could you try my edited version?
– finswimmer
Jan 28 at 4:58
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%2f496417%2fhow-to-add-quotes-to-line-break-field-values-in-a-file%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
You can try with this awk :
awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile
Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'
– chava kumar
Jan 26 at 11:46
@chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.
– ctac_
Jan 26 at 12:58
I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'
– chava kumar
Jan 26 at 14:08
Thanks a lot for your quick help.
– chava kumar
Jan 26 at 14:09
add a comment |
You can try with this awk :
awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile
Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'
– chava kumar
Jan 26 at 11:46
@chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.
– ctac_
Jan 26 at 12:58
I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'
– chava kumar
Jan 26 at 14:08
Thanks a lot for your quick help.
– chava kumar
Jan 26 at 14:09
add a comment |
You can try with this awk :
awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile
You can try with this awk :
awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile
answered Jan 24 at 13:49
ctac_ctac_
1,4141210
1,4141210
Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'
– chava kumar
Jan 26 at 11:46
@chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.
– ctac_
Jan 26 at 12:58
I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'
– chava kumar
Jan 26 at 14:08
Thanks a lot for your quick help.
– chava kumar
Jan 26 at 14:09
add a comment |
Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'
– chava kumar
Jan 26 at 11:46
@chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.
– ctac_
Jan 26 at 12:58
I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'
– chava kumar
Jan 26 at 14:08
Thanks a lot for your quick help.
– chava kumar
Jan 26 at 14:09
Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'
– chava kumar
Jan 26 at 11:46
Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'
– chava kumar
Jan 26 at 11:46
@chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.
– ctac_
Jan 26 at 12:58
@chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.
– ctac_
Jan 26 at 12:58
I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'
– chava kumar
Jan 26 at 14:08
I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'
– chava kumar
Jan 26 at 14:08
Thanks a lot for your quick help.
– chava kumar
Jan 26 at 14:09
Thanks a lot for your quick help.
– chava kumar
Jan 26 at 14:09
add a comment |
You can do it with perl
:
perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file
-0
read file at once, not line by line.-p
print the line in the end-e
the expressions/pattern/replacement/g
Replace pattern with replacement
add a comment |
You can do it with perl
:
perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file
-0
read file at once, not line by line.-p
print the line in the end-e
the expressions/pattern/replacement/g
Replace pattern with replacement
add a comment |
You can do it with perl
:
perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file
-0
read file at once, not line by line.-p
print the line in the end-e
the expressions/pattern/replacement/g
Replace pattern with replacement
You can do it with perl
:
perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file
-0
read file at once, not line by line.-p
print the line in the end-e
the expressions/pattern/replacement/g
Replace pattern with replacement
edited Jan 24 at 13:06
answered Jan 24 at 10:12
RoVoRoVo
3,141216
3,141216
add a comment |
add a comment |
$ cat input.txt | tr "n" "t"
|awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
|tr "t" "n"
tr
transform new line to a tab. So the input is linearized- We tell
awk
to handle the|
as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed tr
converts the tab back to a new line
Thank you for help, but this code didnt work in my case.
– chava kumar
Jan 26 at 11:59
"didn't work" is never a good error description. Could you please describe what doesn't work?
– finswimmer
Jan 26 at 12:07
Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"
– chava kumar
Jan 26 at 12:33
Could you try my edited version?
– finswimmer
Jan 28 at 4:58
add a comment |
$ cat input.txt | tr "n" "t"
|awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
|tr "t" "n"
tr
transform new line to a tab. So the input is linearized- We tell
awk
to handle the|
as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed tr
converts the tab back to a new line
Thank you for help, but this code didnt work in my case.
– chava kumar
Jan 26 at 11:59
"didn't work" is never a good error description. Could you please describe what doesn't work?
– finswimmer
Jan 26 at 12:07
Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"
– chava kumar
Jan 26 at 12:33
Could you try my edited version?
– finswimmer
Jan 28 at 4:58
add a comment |
$ cat input.txt | tr "n" "t"
|awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
|tr "t" "n"
tr
transform new line to a tab. So the input is linearized- We tell
awk
to handle the|
as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed tr
converts the tab back to a new line
$ cat input.txt | tr "n" "t"
|awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
|tr "t" "n"
tr
transform new line to a tab. So the input is linearized- We tell
awk
to handle the|
as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed tr
converts the tab back to a new line
edited Jan 28 at 4:56
answered Jan 24 at 13:30
finswimmerfinswimmer
52416
52416
Thank you for help, but this code didnt work in my case.
– chava kumar
Jan 26 at 11:59
"didn't work" is never a good error description. Could you please describe what doesn't work?
– finswimmer
Jan 26 at 12:07
Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"
– chava kumar
Jan 26 at 12:33
Could you try my edited version?
– finswimmer
Jan 28 at 4:58
add a comment |
Thank you for help, but this code didnt work in my case.
– chava kumar
Jan 26 at 11:59
"didn't work" is never a good error description. Could you please describe what doesn't work?
– finswimmer
Jan 26 at 12:07
Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"
– chava kumar
Jan 26 at 12:33
Could you try my edited version?
– finswimmer
Jan 28 at 4:58
Thank you for help, but this code didnt work in my case.
– chava kumar
Jan 26 at 11:59
Thank you for help, but this code didnt work in my case.
– chava kumar
Jan 26 at 11:59
"didn't work" is never a good error description. Could you please describe what doesn't work?
– finswimmer
Jan 26 at 12:07
"didn't work" is never a good error description. Could you please describe what doesn't work?
– finswimmer
Jan 26 at 12:07
Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"
– chava kumar
Jan 26 at 12:33
Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"
– chava kumar
Jan 26 at 12:33
Could you try my edited version?
– finswimmer
Jan 28 at 4:58
Could you try my edited version?
– finswimmer
Jan 28 at 4:58
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%2f496417%2fhow-to-add-quotes-to-line-break-field-values-in-a-file%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
Are you able to modify the code or output format of whatever produces this data?
– Kusalananda
Jan 24 at 10:04
Wait, so there are linebreaks in the delimited strings?!
– Panki
Jan 24 at 10:16
Yes, OP says:
second attribute contains new line characters
.– RoVo
Jan 24 at 10:19
@RoVo what is "second attribute" anyway?
– Niko Gambt
Jan 24 at 11:45
1
You should ask OP ... but I guess it means second field of his
|sv
.– RoVo
Jan 24 at 11:58