How can I use AWK to break line content into multiple lines using a separator2019 Community Moderator ElectionHow can you combine all lines that end with a backslash character?How can I use awk to (sometimes) remove the first character of a column?How to use AWK record separator?Merge two csvfiles by column headerCompare two files with four columnsHow to use Awk to format numbers with a thousands separatorComparing two lines in BASH using awkGet average for all rows every 3 columnsFinding and extracting immediate charactersHow can I use awk to get value multiple times from one file to other file?
Why is "la Gestapo" feminine?
Is xar preinstalled on macOS?
What is the reasoning behind standardization (dividing by standard deviation)?
How to read string as hex number in bash?
"Marked down as someone wanting to sell shares." What does that mean?
Turning a hard to access nut?
Asserting that Atheism and Theism are both faith based positions
Should I be concerned about student access to a test bank?
Can "few" be used as a subject? If so, what is the rule?
10 year ban after applying for a UK student visa
Emojional cryptic crossword
Why didn’t Eve recognize the little cockroach as a living organism?
Why doesn't the chatan sign the ketubah?
Can other pieces capture a threatening piece and prevent a checkmate?
How old is Nick Fury?
Does the Shadow Magic sorcerer's Eyes of the Dark feature work on all Darkness spells or just his/her own?
Why are there no stars visible in cislunar space?
Recursively updating the MLE as new observations stream in
TDE Master Key Rotation
Should a narrator ever describe things based on a characters view instead of fact?
Determine voltage drop over 10G resistors with cheap multimeter
pipe commands inside find -exec?
Hot air balloons as primitive bombers
Why I don't get the wanted width of tcbox?
How can I use AWK to break line content into multiple lines using a separator
2019 Community Moderator ElectionHow can you combine all lines that end with a backslash character?How can I use awk to (sometimes) remove the first character of a column?How to use AWK record separator?Merge two csvfiles by column headerCompare two files with four columnsHow to use Awk to format numbers with a thousands separatorComparing two lines in BASH using awkGet average for all rows every 3 columnsFinding and extracting immediate charactersHow can I use awk to get value multiple times from one file to other file?
Given an input like this:
field1,field2,field3,field4
I would like to get an output like this
field1
field2
field3
field4
How can I do this with awk?
linux text-processing awk
New contributor
add a comment |
Given an input like this:
field1,field2,field3,field4
I would like to get an output like this
field1
field2
field3
field4
How can I do this with awk?
linux text-processing awk
New contributor
2
how "like that" is your actual data? Because if there are multiple lines of input, you'll have more trouble distinguishing records afterwards (if that's even a problem).
– Jeff Schaller
11 hours ago
Does any field contain embedded commas?
– Kusalananda
11 hours ago
No embedded commas. Source file will have multiple lines but i mean to obtain just one before applying this parse
– Santi
10 hours ago
add a comment |
Given an input like this:
field1,field2,field3,field4
I would like to get an output like this
field1
field2
field3
field4
How can I do this with awk?
linux text-processing awk
New contributor
Given an input like this:
field1,field2,field3,field4
I would like to get an output like this
field1
field2
field3
field4
How can I do this with awk?
linux text-processing awk
linux text-processing awk
New contributor
New contributor
edited 11 hours ago
Stephen Kitt
176k24401479
176k24401479
New contributor
asked 11 hours ago
SantiSanti
1011
1011
New contributor
New contributor
2
how "like that" is your actual data? Because if there are multiple lines of input, you'll have more trouble distinguishing records afterwards (if that's even a problem).
– Jeff Schaller
11 hours ago
Does any field contain embedded commas?
– Kusalananda
11 hours ago
No embedded commas. Source file will have multiple lines but i mean to obtain just one before applying this parse
– Santi
10 hours ago
add a comment |
2
how "like that" is your actual data? Because if there are multiple lines of input, you'll have more trouble distinguishing records afterwards (if that's even a problem).
– Jeff Schaller
11 hours ago
Does any field contain embedded commas?
– Kusalananda
11 hours ago
No embedded commas. Source file will have multiple lines but i mean to obtain just one before applying this parse
– Santi
10 hours ago
2
2
how "like that" is your actual data? Because if there are multiple lines of input, you'll have more trouble distinguishing records afterwards (if that's even a problem).
– Jeff Schaller
11 hours ago
how "like that" is your actual data? Because if there are multiple lines of input, you'll have more trouble distinguishing records afterwards (if that's even a problem).
– Jeff Schaller
11 hours ago
Does any field contain embedded commas?
– Kusalananda
11 hours ago
Does any field contain embedded commas?
– Kusalananda
11 hours ago
No embedded commas. Source file will have multiple lines but i mean to obtain just one before applying this parse
– Santi
10 hours ago
No embedded commas. Source file will have multiple lines but i mean to obtain just one before applying this parse
– Santi
10 hours ago
add a comment |
3 Answers
3
active
oldest
votes
The idiomatic Awk way of doing this would probably be
awk 'BEGINFS=","; OFS="n" $1=$1 1'
or equivalently
awk '$1=$1 1' FS=, OFS='n'
The $1=$1
just forces re-evaluation of the record with the new separator OFS
, and the 1
triggers the default print
action. You can use other expressions, such as NF += 0
, to force the re-evaluation if you prefer - it's a matter of preference.
A quick'n'dirty way would be
awk -vRS=, 1
(or awk 1 RS=,
) which treats each comma-separated word as a whole record and outputs them with the default newline record separator.
add a comment |
Use tr to translate the comma to a newline:
echo "field1,field2,field3,field4" | tr "," "n"
This is likely faster than using awk
, though if you insist, you could do:
echo "field1,field2,field3,field4" | awk 'gsub(",","n");print'
From the GNU awk documentation:
gsub(regexp, replacement [, target])
Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement. The ‘g’ in gsub() stands for “global,” which means replace everywhere.
Thanks, the tr solution is just what i needed
– Santi
10 hours ago
add a comment |
Using bash
and csvformat
from the csvkit
collection to change the field delimiter from a comma to a newline:
$ csvformat -D $'n' file
field1
field2
field3
field4
This is assuming that the input in file
is properly formatted CSV. This would also properly handle embedded commas:
$ cat file
"field,1",field2,field3,field4
$ csvformat -D $'n' file
field,1
field2
field3
field4
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
);
);
Santi is a new contributor. Be nice, and check out our Code of Conduct.
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%2f506987%2fhow-can-i-use-awk-to-break-line-content-into-multiple-lines-using-a-separator%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
The idiomatic Awk way of doing this would probably be
awk 'BEGINFS=","; OFS="n" $1=$1 1'
or equivalently
awk '$1=$1 1' FS=, OFS='n'
The $1=$1
just forces re-evaluation of the record with the new separator OFS
, and the 1
triggers the default print
action. You can use other expressions, such as NF += 0
, to force the re-evaluation if you prefer - it's a matter of preference.
A quick'n'dirty way would be
awk -vRS=, 1
(or awk 1 RS=,
) which treats each comma-separated word as a whole record and outputs them with the default newline record separator.
add a comment |
The idiomatic Awk way of doing this would probably be
awk 'BEGINFS=","; OFS="n" $1=$1 1'
or equivalently
awk '$1=$1 1' FS=, OFS='n'
The $1=$1
just forces re-evaluation of the record with the new separator OFS
, and the 1
triggers the default print
action. You can use other expressions, such as NF += 0
, to force the re-evaluation if you prefer - it's a matter of preference.
A quick'n'dirty way would be
awk -vRS=, 1
(or awk 1 RS=,
) which treats each comma-separated word as a whole record and outputs them with the default newline record separator.
add a comment |
The idiomatic Awk way of doing this would probably be
awk 'BEGINFS=","; OFS="n" $1=$1 1'
or equivalently
awk '$1=$1 1' FS=, OFS='n'
The $1=$1
just forces re-evaluation of the record with the new separator OFS
, and the 1
triggers the default print
action. You can use other expressions, such as NF += 0
, to force the re-evaluation if you prefer - it's a matter of preference.
A quick'n'dirty way would be
awk -vRS=, 1
(or awk 1 RS=,
) which treats each comma-separated word as a whole record and outputs them with the default newline record separator.
The idiomatic Awk way of doing this would probably be
awk 'BEGINFS=","; OFS="n" $1=$1 1'
or equivalently
awk '$1=$1 1' FS=, OFS='n'
The $1=$1
just forces re-evaluation of the record with the new separator OFS
, and the 1
triggers the default print
action. You can use other expressions, such as NF += 0
, to force the re-evaluation if you prefer - it's a matter of preference.
A quick'n'dirty way would be
awk -vRS=, 1
(or awk 1 RS=,
) which treats each comma-separated word as a whole record and outputs them with the default newline record separator.
edited 11 hours ago
answered 11 hours ago
steeldriversteeldriver
37.1k45287
37.1k45287
add a comment |
add a comment |
Use tr to translate the comma to a newline:
echo "field1,field2,field3,field4" | tr "," "n"
This is likely faster than using awk
, though if you insist, you could do:
echo "field1,field2,field3,field4" | awk 'gsub(",","n");print'
From the GNU awk documentation:
gsub(regexp, replacement [, target])
Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement. The ‘g’ in gsub() stands for “global,” which means replace everywhere.
Thanks, the tr solution is just what i needed
– Santi
10 hours ago
add a comment |
Use tr to translate the comma to a newline:
echo "field1,field2,field3,field4" | tr "," "n"
This is likely faster than using awk
, though if you insist, you could do:
echo "field1,field2,field3,field4" | awk 'gsub(",","n");print'
From the GNU awk documentation:
gsub(regexp, replacement [, target])
Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement. The ‘g’ in gsub() stands for “global,” which means replace everywhere.
Thanks, the tr solution is just what i needed
– Santi
10 hours ago
add a comment |
Use tr to translate the comma to a newline:
echo "field1,field2,field3,field4" | tr "," "n"
This is likely faster than using awk
, though if you insist, you could do:
echo "field1,field2,field3,field4" | awk 'gsub(",","n");print'
From the GNU awk documentation:
gsub(regexp, replacement [, target])
Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement. The ‘g’ in gsub() stands for “global,” which means replace everywhere.
Use tr to translate the comma to a newline:
echo "field1,field2,field3,field4" | tr "," "n"
This is likely faster than using awk
, though if you insist, you could do:
echo "field1,field2,field3,field4" | awk 'gsub(",","n");print'
From the GNU awk documentation:
gsub(regexp, replacement [, target])
Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement. The ‘g’ in gsub() stands for “global,” which means replace everywhere.
edited 11 hours ago
answered 11 hours ago
JRFergusonJRFerguson
10.3k32432
10.3k32432
Thanks, the tr solution is just what i needed
– Santi
10 hours ago
add a comment |
Thanks, the tr solution is just what i needed
– Santi
10 hours ago
Thanks, the tr solution is just what i needed
– Santi
10 hours ago
Thanks, the tr solution is just what i needed
– Santi
10 hours ago
add a comment |
Using bash
and csvformat
from the csvkit
collection to change the field delimiter from a comma to a newline:
$ csvformat -D $'n' file
field1
field2
field3
field4
This is assuming that the input in file
is properly formatted CSV. This would also properly handle embedded commas:
$ cat file
"field,1",field2,field3,field4
$ csvformat -D $'n' file
field,1
field2
field3
field4
add a comment |
Using bash
and csvformat
from the csvkit
collection to change the field delimiter from a comma to a newline:
$ csvformat -D $'n' file
field1
field2
field3
field4
This is assuming that the input in file
is properly formatted CSV. This would also properly handle embedded commas:
$ cat file
"field,1",field2,field3,field4
$ csvformat -D $'n' file
field,1
field2
field3
field4
add a comment |
Using bash
and csvformat
from the csvkit
collection to change the field delimiter from a comma to a newline:
$ csvformat -D $'n' file
field1
field2
field3
field4
This is assuming that the input in file
is properly formatted CSV. This would also properly handle embedded commas:
$ cat file
"field,1",field2,field3,field4
$ csvformat -D $'n' file
field,1
field2
field3
field4
Using bash
and csvformat
from the csvkit
collection to change the field delimiter from a comma to a newline:
$ csvformat -D $'n' file
field1
field2
field3
field4
This is assuming that the input in file
is properly formatted CSV. This would also properly handle embedded commas:
$ cat file
"field,1",field2,field3,field4
$ csvformat -D $'n' file
field,1
field2
field3
field4
answered 11 hours ago
KusalanandaKusalananda
136k17257425
136k17257425
add a comment |
add a comment |
Santi is a new contributor. Be nice, and check out our Code of Conduct.
Santi is a new contributor. Be nice, and check out our Code of Conduct.
Santi is a new contributor. Be nice, and check out our Code of Conduct.
Santi is a new contributor. Be nice, and check out our Code of Conduct.
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%2f506987%2fhow-can-i-use-awk-to-break-line-content-into-multiple-lines-using-a-separator%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
2
how "like that" is your actual data? Because if there are multiple lines of input, you'll have more trouble distinguishing records afterwards (if that's even a problem).
– Jeff Schaller
11 hours ago
Does any field contain embedded commas?
– Kusalananda
11 hours ago
No embedded commas. Source file will have multiple lines but i mean to obtain just one before applying this parse
– Santi
10 hours ago