Korn/Bash Shell: How to do I convert the contents to the below format?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
pid name tid mod state data
--------------------------------------------------------------------------------
39523 srv0051_0001_0 39642 20-10:59:28 Working 820000:500196:500077
43137 srv0051_0005_0 43156 20-10:59:28 Working 820000:4250501:840057
43895 srv0051_0006_0 43903 20-10:59:28 Working 820000:4250501:840057
47523 srv0051_0009_0 47547 20-10:59:28 Working 600005:4250501:4250846
48841 srv0051_0010_0 48851 20-10:59:28 Working 600005:4290000:4290000
58182 srv0051_0020_0 58188 20-10:59:28 Working 820000:4250501:840057
8297 srv0079_0008_0 8316 20-10:59:27 Working 600005:3070001:3050012
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
linux text-processing
add a comment |Â
up vote
1
down vote
favorite
pid name tid mod state data
--------------------------------------------------------------------------------
39523 srv0051_0001_0 39642 20-10:59:28 Working 820000:500196:500077
43137 srv0051_0005_0 43156 20-10:59:28 Working 820000:4250501:840057
43895 srv0051_0006_0 43903 20-10:59:28 Working 820000:4250501:840057
47523 srv0051_0009_0 47547 20-10:59:28 Working 600005:4250501:4250846
48841 srv0051_0010_0 48851 20-10:59:28 Working 600005:4290000:4290000
58182 srv0051_0020_0 58188 20-10:59:28 Working 820000:4250501:840057
8297 srv0079_0008_0 8316 20-10:59:27 Working 600005:3070001:3050012
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
linux text-processing
1
pray tell, what have you tried so far ?
â steve
Jun 25 at 19:13
1
@steve I'm very new to scripting so I'm not sure where to start.
â fball4life36
Jun 25 at 19:18
Your header has more fields than the data. Is this on purpose?
â glenn jackman
Jun 25 at 19:32
OK, which of the two is original and which is the desired result ?
â Sergiy Kolodyazhnyy
Jun 26 at 2:01
@glennjackman Yes. The second is the desired state.
â fball4life36
Jun 26 at 13:26
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
pid name tid mod state data
--------------------------------------------------------------------------------
39523 srv0051_0001_0 39642 20-10:59:28 Working 820000:500196:500077
43137 srv0051_0005_0 43156 20-10:59:28 Working 820000:4250501:840057
43895 srv0051_0006_0 43903 20-10:59:28 Working 820000:4250501:840057
47523 srv0051_0009_0 47547 20-10:59:28 Working 600005:4250501:4250846
48841 srv0051_0010_0 48851 20-10:59:28 Working 600005:4290000:4290000
58182 srv0051_0020_0 58188 20-10:59:28 Working 820000:4250501:840057
8297 srv0079_0008_0 8316 20-10:59:27 Working 600005:3070001:3050012
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
linux text-processing
pid name tid mod state data
--------------------------------------------------------------------------------
39523 srv0051_0001_0 39642 20-10:59:28 Working 820000:500196:500077
43137 srv0051_0005_0 43156 20-10:59:28 Working 820000:4250501:840057
43895 srv0051_0006_0 43903 20-10:59:28 Working 820000:4250501:840057
47523 srv0051_0009_0 47547 20-10:59:28 Working 600005:4250501:4250846
48841 srv0051_0010_0 48851 20-10:59:28 Working 600005:4290000:4290000
58182 srv0051_0020_0 58188 20-10:59:28 Working 820000:4250501:840057
8297 srv0079_0008_0 8316 20-10:59:27 Working 600005:3070001:3050012
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
linux text-processing
edited Jun 25 at 19:53
Jeff Schaller
30.8k846104
30.8k846104
asked Jun 25 at 19:09
fball4life36
347
347
1
pray tell, what have you tried so far ?
â steve
Jun 25 at 19:13
1
@steve I'm very new to scripting so I'm not sure where to start.
â fball4life36
Jun 25 at 19:18
Your header has more fields than the data. Is this on purpose?
â glenn jackman
Jun 25 at 19:32
OK, which of the two is original and which is the desired result ?
â Sergiy Kolodyazhnyy
Jun 26 at 2:01
@glennjackman Yes. The second is the desired state.
â fball4life36
Jun 26 at 13:26
add a comment |Â
1
pray tell, what have you tried so far ?
â steve
Jun 25 at 19:13
1
@steve I'm very new to scripting so I'm not sure where to start.
â fball4life36
Jun 25 at 19:18
Your header has more fields than the data. Is this on purpose?
â glenn jackman
Jun 25 at 19:32
OK, which of the two is original and which is the desired result ?
â Sergiy Kolodyazhnyy
Jun 26 at 2:01
@glennjackman Yes. The second is the desired state.
â fball4life36
Jun 26 at 13:26
1
1
pray tell, what have you tried so far ?
â steve
Jun 25 at 19:13
pray tell, what have you tried so far ?
â steve
Jun 25 at 19:13
1
1
@steve I'm very new to scripting so I'm not sure where to start.
â fball4life36
Jun 25 at 19:18
@steve I'm very new to scripting so I'm not sure where to start.
â fball4life36
Jun 25 at 19:18
Your header has more fields than the data. Is this on purpose?
â glenn jackman
Jun 25 at 19:32
Your header has more fields than the data. Is this on purpose?
â glenn jackman
Jun 25 at 19:32
OK, which of the two is original and which is the desired result ?
â Sergiy Kolodyazhnyy
Jun 26 at 2:01
OK, which of the two is original and which is the desired result ?
â Sergiy Kolodyazhnyy
Jun 26 at 2:01
@glennjackman Yes. The second is the desired state.
â fball4life36
Jun 26 at 13:26
@glennjackman Yes. The second is the desired state.
â fball4life36
Jun 26 at 13:26
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
6
down vote
accepted
sed '
# delete the 2nd line
2d
# remove any leading whitespace
s/^[[:blank:]]+//
# on line 1, replace "data" with other words
1s/data/appnbr request tasknbr appctx username/
# replace any sequences of whitespace with comma
s/[[:blank:]]+/,/g
# replace the 3rd and subsequent colons
s/:/,/3g
' file
Required GNU sed for the s///3g
action
GNU sed also needed for+
. Standard equivalent is1,
â Stéphane Chazelas
Jun 25 at 20:53
@glennjackman I'm confuse. How would the entire command look like?
â fball4life36
Jun 27 at 13:47
Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
â glenn jackman
Jun 27 at 13:51
Is it because it's a multiline script with comments? Newlines are valid within quotes
â glenn jackman
Jun 27 at 13:52
@glennjackman How would command look like without the comments?
â fball4life36
Jun 28 at 13:26
 |Â
show 1 more comment
up vote
2
down vote
Try this
grep -v "^-" test.txt | tr -s " " ',' | sed -e s/:/,/3g -e '0,/data/ s/data/appnbr,request,tasknbr,appctx,username/'
A tweak needed to cover the conversion of:
to,
in the final column ?
â steve
Jun 25 at 19:29
1
@steve thanks.. updated
â SivaPrasath
Jun 25 at 19:31
add a comment |Â
up vote
1
down vote
$ awk -f script.awk file.txt
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Where script.awk
is
BEGIN OFS = "," # set output delimiter
NR == 1
# modify some fields of the header
$6 = "appnbr"
$7 = "request"
$8 = "tasknbr"
$9 = "appctx"
$10 = "username"
NR == 2 next # skip line 2
NR > 2
# split the sixth field on ":" and extend the record with the bits
split($6, a, ":")
$6 = a[1]
$7 = a[2]
$8 = a[3]
1 # print
add a comment |Â
up vote
1
down vote
perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt
Output:
id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Explanation:
- Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array
@F
. Array is indexed from0
and goes all the way to$#F
. So$#F-1
is the second last element. - All lines except the 2nd are to be processed, in that the last element is the deciding factor.
- For the first line, the last element is replaced by multiple field names.
- For the remaining, (remember 2nd is already discarded) are split up on
:
and these split up fields come in place of the last element. - The joining of all the elements is done by the
,
and then printed tostdout
.
With POSIX sed
, we can do the following:
sed -e '
2d
s/^[[:blank:]]*//;s/[[:blank:]]*$//;s/[[:blank:]]1,/ /g ;# trim n squeeze blanks
1s/data$/appnbr request tasknbr appctx username/ ;# line 1 spl processing
y/:/n/ ;# change all colons to newlines
s/n/:/;s//:/ ;# undo the transformation for the first 2
y/n/ / ;# change the rest to blanks
y/ /,/ ;# now change these and the already existing blanks to commas
' input-file.txt
add a comment |Â
up vote
1
down vote
Here's my awk
stab at it.
awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt
- NR>2 so that record number 1 and 2 (headers) are skipped
- print fields $1 thru $5 with commas
- instead of printing field $6, print it with
:
replaced with ","
How would I do the new headers aspect?
â fball4life36
Jun 27 at 19:44
now added the headers part.
â steve
Jun 27 at 20:25
The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
â fball4life36
Jun 27 at 21:04
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
sed '
# delete the 2nd line
2d
# remove any leading whitespace
s/^[[:blank:]]+//
# on line 1, replace "data" with other words
1s/data/appnbr request tasknbr appctx username/
# replace any sequences of whitespace with comma
s/[[:blank:]]+/,/g
# replace the 3rd and subsequent colons
s/:/,/3g
' file
Required GNU sed for the s///3g
action
GNU sed also needed for+
. Standard equivalent is1,
â Stéphane Chazelas
Jun 25 at 20:53
@glennjackman I'm confuse. How would the entire command look like?
â fball4life36
Jun 27 at 13:47
Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
â glenn jackman
Jun 27 at 13:51
Is it because it's a multiline script with comments? Newlines are valid within quotes
â glenn jackman
Jun 27 at 13:52
@glennjackman How would command look like without the comments?
â fball4life36
Jun 28 at 13:26
 |Â
show 1 more comment
up vote
6
down vote
accepted
sed '
# delete the 2nd line
2d
# remove any leading whitespace
s/^[[:blank:]]+//
# on line 1, replace "data" with other words
1s/data/appnbr request tasknbr appctx username/
# replace any sequences of whitespace with comma
s/[[:blank:]]+/,/g
# replace the 3rd and subsequent colons
s/:/,/3g
' file
Required GNU sed for the s///3g
action
GNU sed also needed for+
. Standard equivalent is1,
â Stéphane Chazelas
Jun 25 at 20:53
@glennjackman I'm confuse. How would the entire command look like?
â fball4life36
Jun 27 at 13:47
Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
â glenn jackman
Jun 27 at 13:51
Is it because it's a multiline script with comments? Newlines are valid within quotes
â glenn jackman
Jun 27 at 13:52
@glennjackman How would command look like without the comments?
â fball4life36
Jun 28 at 13:26
 |Â
show 1 more comment
up vote
6
down vote
accepted
up vote
6
down vote
accepted
sed '
# delete the 2nd line
2d
# remove any leading whitespace
s/^[[:blank:]]+//
# on line 1, replace "data" with other words
1s/data/appnbr request tasknbr appctx username/
# replace any sequences of whitespace with comma
s/[[:blank:]]+/,/g
# replace the 3rd and subsequent colons
s/:/,/3g
' file
Required GNU sed for the s///3g
action
sed '
# delete the 2nd line
2d
# remove any leading whitespace
s/^[[:blank:]]+//
# on line 1, replace "data" with other words
1s/data/appnbr request tasknbr appctx username/
# replace any sequences of whitespace with comma
s/[[:blank:]]+/,/g
# replace the 3rd and subsequent colons
s/:/,/3g
' file
Required GNU sed for the s///3g
action
edited Jun 25 at 19:33
answered Jun 25 at 19:24
glenn jackman
45.6k264100
45.6k264100
GNU sed also needed for+
. Standard equivalent is1,
â Stéphane Chazelas
Jun 25 at 20:53
@glennjackman I'm confuse. How would the entire command look like?
â fball4life36
Jun 27 at 13:47
Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
â glenn jackman
Jun 27 at 13:51
Is it because it's a multiline script with comments? Newlines are valid within quotes
â glenn jackman
Jun 27 at 13:52
@glennjackman How would command look like without the comments?
â fball4life36
Jun 28 at 13:26
 |Â
show 1 more comment
GNU sed also needed for+
. Standard equivalent is1,
â Stéphane Chazelas
Jun 25 at 20:53
@glennjackman I'm confuse. How would the entire command look like?
â fball4life36
Jun 27 at 13:47
Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
â glenn jackman
Jun 27 at 13:51
Is it because it's a multiline script with comments? Newlines are valid within quotes
â glenn jackman
Jun 27 at 13:52
@glennjackman How would command look like without the comments?
â fball4life36
Jun 28 at 13:26
GNU sed also needed for
+
. Standard equivalent is 1,
â Stéphane Chazelas
Jun 25 at 20:53
GNU sed also needed for
+
. Standard equivalent is 1,
â Stéphane Chazelas
Jun 25 at 20:53
@glennjackman I'm confuse. How would the entire command look like?
â fball4life36
Jun 27 at 13:47
@glennjackman I'm confuse. How would the entire command look like?
â fball4life36
Jun 27 at 13:47
Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
â glenn jackman
Jun 27 at 13:51
Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
â glenn jackman
Jun 27 at 13:51
Is it because it's a multiline script with comments? Newlines are valid within quotes
â glenn jackman
Jun 27 at 13:52
Is it because it's a multiline script with comments? Newlines are valid within quotes
â glenn jackman
Jun 27 at 13:52
@glennjackman How would command look like without the comments?
â fball4life36
Jun 28 at 13:26
@glennjackman How would command look like without the comments?
â fball4life36
Jun 28 at 13:26
 |Â
show 1 more comment
up vote
2
down vote
Try this
grep -v "^-" test.txt | tr -s " " ',' | sed -e s/:/,/3g -e '0,/data/ s/data/appnbr,request,tasknbr,appctx,username/'
A tweak needed to cover the conversion of:
to,
in the final column ?
â steve
Jun 25 at 19:29
1
@steve thanks.. updated
â SivaPrasath
Jun 25 at 19:31
add a comment |Â
up vote
2
down vote
Try this
grep -v "^-" test.txt | tr -s " " ',' | sed -e s/:/,/3g -e '0,/data/ s/data/appnbr,request,tasknbr,appctx,username/'
A tweak needed to cover the conversion of:
to,
in the final column ?
â steve
Jun 25 at 19:29
1
@steve thanks.. updated
â SivaPrasath
Jun 25 at 19:31
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Try this
grep -v "^-" test.txt | tr -s " " ',' | sed -e s/:/,/3g -e '0,/data/ s/data/appnbr,request,tasknbr,appctx,username/'
Try this
grep -v "^-" test.txt | tr -s " " ',' | sed -e s/:/,/3g -e '0,/data/ s/data/appnbr,request,tasknbr,appctx,username/'
edited Jun 25 at 19:54
answered Jun 25 at 19:18
SivaPrasath
3,88611737
3,88611737
A tweak needed to cover the conversion of:
to,
in the final column ?
â steve
Jun 25 at 19:29
1
@steve thanks.. updated
â SivaPrasath
Jun 25 at 19:31
add a comment |Â
A tweak needed to cover the conversion of:
to,
in the final column ?
â steve
Jun 25 at 19:29
1
@steve thanks.. updated
â SivaPrasath
Jun 25 at 19:31
A tweak needed to cover the conversion of
:
to ,
in the final column ?â steve
Jun 25 at 19:29
A tweak needed to cover the conversion of
:
to ,
in the final column ?â steve
Jun 25 at 19:29
1
1
@steve thanks.. updated
â SivaPrasath
Jun 25 at 19:31
@steve thanks.. updated
â SivaPrasath
Jun 25 at 19:31
add a comment |Â
up vote
1
down vote
$ awk -f script.awk file.txt
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Where script.awk
is
BEGIN OFS = "," # set output delimiter
NR == 1
# modify some fields of the header
$6 = "appnbr"
$7 = "request"
$8 = "tasknbr"
$9 = "appctx"
$10 = "username"
NR == 2 next # skip line 2
NR > 2
# split the sixth field on ":" and extend the record with the bits
split($6, a, ":")
$6 = a[1]
$7 = a[2]
$8 = a[3]
1 # print
add a comment |Â
up vote
1
down vote
$ awk -f script.awk file.txt
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Where script.awk
is
BEGIN OFS = "," # set output delimiter
NR == 1
# modify some fields of the header
$6 = "appnbr"
$7 = "request"
$8 = "tasknbr"
$9 = "appctx"
$10 = "username"
NR == 2 next # skip line 2
NR > 2
# split the sixth field on ":" and extend the record with the bits
split($6, a, ":")
$6 = a[1]
$7 = a[2]
$8 = a[3]
1 # print
add a comment |Â
up vote
1
down vote
up vote
1
down vote
$ awk -f script.awk file.txt
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Where script.awk
is
BEGIN OFS = "," # set output delimiter
NR == 1
# modify some fields of the header
$6 = "appnbr"
$7 = "request"
$8 = "tasknbr"
$9 = "appctx"
$10 = "username"
NR == 2 next # skip line 2
NR > 2
# split the sixth field on ":" and extend the record with the bits
split($6, a, ":")
$6 = a[1]
$7 = a[2]
$8 = a[3]
1 # print
$ awk -f script.awk file.txt
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Where script.awk
is
BEGIN OFS = "," # set output delimiter
NR == 1
# modify some fields of the header
$6 = "appnbr"
$7 = "request"
$8 = "tasknbr"
$9 = "appctx"
$10 = "username"
NR == 2 next # skip line 2
NR > 2
# split the sixth field on ":" and extend the record with the bits
split($6, a, ":")
$6 = a[1]
$7 = a[2]
$8 = a[3]
1 # print
answered Jun 25 at 19:40
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â
up vote
1
down vote
perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt
Output:
id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Explanation:
- Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array
@F
. Array is indexed from0
and goes all the way to$#F
. So$#F-1
is the second last element. - All lines except the 2nd are to be processed, in that the last element is the deciding factor.
- For the first line, the last element is replaced by multiple field names.
- For the remaining, (remember 2nd is already discarded) are split up on
:
and these split up fields come in place of the last element. - The joining of all the elements is done by the
,
and then printed tostdout
.
With POSIX sed
, we can do the following:
sed -e '
2d
s/^[[:blank:]]*//;s/[[:blank:]]*$//;s/[[:blank:]]1,/ /g ;# trim n squeeze blanks
1s/data$/appnbr request tasknbr appctx username/ ;# line 1 spl processing
y/:/n/ ;# change all colons to newlines
s/n/:/;s//:/ ;# undo the transformation for the first 2
y/n/ / ;# change the rest to blanks
y/ /,/ ;# now change these and the already existing blanks to commas
' input-file.txt
add a comment |Â
up vote
1
down vote
perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt
Output:
id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Explanation:
- Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array
@F
. Array is indexed from0
and goes all the way to$#F
. So$#F-1
is the second last element. - All lines except the 2nd are to be processed, in that the last element is the deciding factor.
- For the first line, the last element is replaced by multiple field names.
- For the remaining, (remember 2nd is already discarded) are split up on
:
and these split up fields come in place of the last element. - The joining of all the elements is done by the
,
and then printed tostdout
.
With POSIX sed
, we can do the following:
sed -e '
2d
s/^[[:blank:]]*//;s/[[:blank:]]*$//;s/[[:blank:]]1,/ /g ;# trim n squeeze blanks
1s/data$/appnbr request tasknbr appctx username/ ;# line 1 spl processing
y/:/n/ ;# change all colons to newlines
s/n/:/;s//:/ ;# undo the transformation for the first 2
y/n/ / ;# change the rest to blanks
y/ /,/ ;# now change these and the already existing blanks to commas
' input-file.txt
add a comment |Â
up vote
1
down vote
up vote
1
down vote
perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt
Output:
id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Explanation:
- Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array
@F
. Array is indexed from0
and goes all the way to$#F
. So$#F-1
is the second last element. - All lines except the 2nd are to be processed, in that the last element is the deciding factor.
- For the first line, the last element is replaced by multiple field names.
- For the remaining, (remember 2nd is already discarded) are split up on
:
and these split up fields come in place of the last element. - The joining of all the elements is done by the
,
and then printed tostdout
.
With POSIX sed
, we can do the following:
sed -e '
2d
s/^[[:blank:]]*//;s/[[:blank:]]*$//;s/[[:blank:]]1,/ /g ;# trim n squeeze blanks
1s/data$/appnbr request tasknbr appctx username/ ;# line 1 spl processing
y/:/n/ ;# change all colons to newlines
s/n/:/;s//:/ ;# undo the transformation for the first 2
y/n/ / ;# change the rest to blanks
y/ /,/ ;# now change these and the already existing blanks to commas
' input-file.txt
perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt
Output:
id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-10:59:27,Working,600005,3070001,3050012
Explanation:
- Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array
@F
. Array is indexed from0
and goes all the way to$#F
. So$#F-1
is the second last element. - All lines except the 2nd are to be processed, in that the last element is the deciding factor.
- For the first line, the last element is replaced by multiple field names.
- For the remaining, (remember 2nd is already discarded) are split up on
:
and these split up fields come in place of the last element. - The joining of all the elements is done by the
,
and then printed tostdout
.
With POSIX sed
, we can do the following:
sed -e '
2d
s/^[[:blank:]]*//;s/[[:blank:]]*$//;s/[[:blank:]]1,/ /g ;# trim n squeeze blanks
1s/data$/appnbr request tasknbr appctx username/ ;# line 1 spl processing
y/:/n/ ;# change all colons to newlines
s/n/:/;s//:/ ;# undo the transformation for the first 2
y/n/ / ;# change the rest to blanks
y/ /,/ ;# now change these and the already existing blanks to commas
' input-file.txt
answered Jun 26 at 8:25
Rakesh Sharma
3433
3433
add a comment |Â
add a comment |Â
up vote
1
down vote
Here's my awk
stab at it.
awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt
- NR>2 so that record number 1 and 2 (headers) are skipped
- print fields $1 thru $5 with commas
- instead of printing field $6, print it with
:
replaced with ","
How would I do the new headers aspect?
â fball4life36
Jun 27 at 19:44
now added the headers part.
â steve
Jun 27 at 20:25
The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
â fball4life36
Jun 27 at 21:04
add a comment |Â
up vote
1
down vote
Here's my awk
stab at it.
awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt
- NR>2 so that record number 1 and 2 (headers) are skipped
- print fields $1 thru $5 with commas
- instead of printing field $6, print it with
:
replaced with ","
How would I do the new headers aspect?
â fball4life36
Jun 27 at 19:44
now added the headers part.
â steve
Jun 27 at 20:25
The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
â fball4life36
Jun 27 at 21:04
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Here's my awk
stab at it.
awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt
- NR>2 so that record number 1 and 2 (headers) are skipped
- print fields $1 thru $5 with commas
- instead of printing field $6, print it with
:
replaced with ","
Here's my awk
stab at it.
awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt
- NR>2 so that record number 1 and 2 (headers) are skipped
- print fields $1 thru $5 with commas
- instead of printing field $6, print it with
:
replaced with ","
edited Jun 27 at 20:24
answered Jun 25 at 19:41
steve
12.1k22047
12.1k22047
How would I do the new headers aspect?
â fball4life36
Jun 27 at 19:44
now added the headers part.
â steve
Jun 27 at 20:25
The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
â fball4life36
Jun 27 at 21:04
add a comment |Â
How would I do the new headers aspect?
â fball4life36
Jun 27 at 19:44
now added the headers part.
â steve
Jun 27 at 20:25
The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
â fball4life36
Jun 27 at 21:04
How would I do the new headers aspect?
â fball4life36
Jun 27 at 19:44
How would I do the new headers aspect?
â fball4life36
Jun 27 at 19:44
now added the headers part.
â steve
Jun 27 at 20:25
now added the headers part.
â steve
Jun 27 at 20:25
The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
â fball4life36
Jun 27 at 21:04
The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
â fball4life36
Jun 27 at 21:04
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451825%2fkorn-bash-shell-how-to-do-i-convert-the-contents-to-the-below-format%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
pray tell, what have you tried so far ?
â steve
Jun 25 at 19:13
1
@steve I'm very new to scripting so I'm not sure where to start.
â fball4life36
Jun 25 at 19:18
Your header has more fields than the data. Is this on purpose?
â glenn jackman
Jun 25 at 19:32
OK, which of the two is original and which is the desired result ?
â Sergiy Kolodyazhnyy
Jun 26 at 2:01
@glennjackman Yes. The second is the desired state.
â fball4life36
Jun 26 at 13:26