AWK issue using unix scripts
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have this file
AK3*BPS*2
AK4*8*0510*1
AK3*RMT*12
AK4*1*0128*7*CR
and I want this output
BPS 2 1
RMT 12 7 CR
I am using this command
awk -F* '$1=="AK3" print $2, $3 "c" ($1=="AK4" print $4, $5 ' $FileHome/badfile_$session_num.txt >> $FileHome/input_output_record.txt
but i am getting this result
BPS 2
1
RMT 12
7 CR
Any hint will be appreciated
awk text-formatting
add a comment |Â
up vote
0
down vote
favorite
I have this file
AK3*BPS*2
AK4*8*0510*1
AK3*RMT*12
AK4*1*0128*7*CR
and I want this output
BPS 2 1
RMT 12 7 CR
I am using this command
awk -F* '$1=="AK3" print $2, $3 "c" ($1=="AK4" print $4, $5 ' $FileHome/badfile_$session_num.txt >> $FileHome/input_output_record.txt
but i am getting this result
BPS 2
1
RMT 12
7 CR
Any hint will be appreciated
awk text-formatting
1
What's thatc
about? You seem to be missing a)
somewhere...
â Kusalananda
Dec 14 '17 at 15:03
What happens if you have a line starting withAK3
following another one withAK3
?
â ilkkachu
Dec 14 '17 at 15:04
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this file
AK3*BPS*2
AK4*8*0510*1
AK3*RMT*12
AK4*1*0128*7*CR
and I want this output
BPS 2 1
RMT 12 7 CR
I am using this command
awk -F* '$1=="AK3" print $2, $3 "c" ($1=="AK4" print $4, $5 ' $FileHome/badfile_$session_num.txt >> $FileHome/input_output_record.txt
but i am getting this result
BPS 2
1
RMT 12
7 CR
Any hint will be appreciated
awk text-formatting
I have this file
AK3*BPS*2
AK4*8*0510*1
AK3*RMT*12
AK4*1*0128*7*CR
and I want this output
BPS 2 1
RMT 12 7 CR
I am using this command
awk -F* '$1=="AK3" print $2, $3 "c" ($1=="AK4" print $4, $5 ' $FileHome/badfile_$session_num.txt >> $FileHome/input_output_record.txt
but i am getting this result
BPS 2
1
RMT 12
7 CR
Any hint will be appreciated
awk text-formatting
edited Dec 14 '17 at 14:58
Jesse_b
10.5k22659
10.5k22659
asked Dec 14 '17 at 14:55
Antonio Pizarro
61
61
1
What's thatc
about? You seem to be missing a)
somewhere...
â Kusalananda
Dec 14 '17 at 15:03
What happens if you have a line starting withAK3
following another one withAK3
?
â ilkkachu
Dec 14 '17 at 15:04
add a comment |Â
1
What's thatc
about? You seem to be missing a)
somewhere...
â Kusalananda
Dec 14 '17 at 15:03
What happens if you have a line starting withAK3
following another one withAK3
?
â ilkkachu
Dec 14 '17 at 15:04
1
1
What's that
c
about? You seem to be missing a )
somewhere...â Kusalananda
Dec 14 '17 at 15:03
What's that
c
about? You seem to be missing a )
somewhere...â Kusalananda
Dec 14 '17 at 15:03
What happens if you have a line starting with
AK3
following another one with AK3
?â ilkkachu
Dec 14 '17 at 15:04
What happens if you have a line starting with
AK3
following another one with AK3
?â ilkkachu
Dec 14 '17 at 15:04
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
0
down vote
You seem to want:
- The last two fields of each
AK3
line, followed by - Fields four and five from the
AK4
line (field five may be missing).
$ awk -F '*' '/^AK3/ printf("%st%s", $2, $3) /^AK4/ printf("t%st%sn", $4, $5) ' data.in
BPS 2 1
RMT 12 7 CR
add a comment |Â
up vote
0
down vote
Another way is to directly control the output record separator(ORS)
$ awk -F'*' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS 2 1
RMT 12 7 CR
- by default, output field separator(OFS) is single space
- and input record separator(RS) is newline
This is easy to modify for different OFS and also shows empty field
$ awk -F'*' -v OFS=',' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS,2,1,
RMT,12,7,CR
add a comment |Â
up vote
0
down vote
Plain print
adds a newline (the output record separator), but printf
doesn't, so we can use that on the AK3
case with an explicit separator between the fields. In the AK4
case, we want a field separator before the first field, and the simplest way to get that is to print an empty field first.
$ awk -F* '$1=="AK3" printf "%s", $2 OFS $3 $1=="AK4" print "", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
This is easily converted to giving tab-spaced output, just add -vOFS='t'
in the beginning of the command line.
Though if you want fixed-width columns, it's probably easiest to use printf
and width settings on the %s
format:
$ awk -F* '$1=="AK3" printf "%-3s %-2s", $2, $3 $1=="AK4" printf " %-1s %sn", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
Over-long fields will still extend, unless you give the maximum field width too, with e.g. %-3.3s
, and this will produce broken output if the input lines are not in correct order.
add a comment |Â
up vote
0
down vote
How about:
awk -F '*' '
printf "%s %s", $2, $3
getline
for (i=4; i<=NF; i++) printf " %s", $i
print ""
' file | column -t
BPS 2 1
RMT 12 7 CR
With plain bash:
while IFS='*' read -r a1 b1 c1; IFS='*' read -r a2 b2 c3 rest
do
echo "$b1 $c1 $rest//*/ "
done < file
I'm assuming there are always pairs of lines.
â glenn jackman
Dec 14 '17 at 15:59
Wow! Thank you very much for all the answers I tried all of them and I always got the result that I was expecting. Amazing ! help
â Antonio Pizarro
Dec 14 '17 at 19:46
lol. Give a fun puzzle to a bunch of nerds, you'll get a lot of responses.
â glenn jackman
Dec 14 '17 at 20:24
add a comment |Â
up vote
0
down vote
I achieved the result by below commands
sed -n '1~2p' testfile.txt| awk -F "*" 'print $2,$3' >> op.txt
sed -n '2~2p' testfile.txt | awk -F "*" 'print $4,$5' >> op.txt
sed -n '1~2p' op.txt |sed "N;s/n/ /g" |awk 'print $0"n"'; sed -n '2~2p' op.txt| sed "N;s/n/ /g"
Output
BPS 2 1
RMT 12 7 CR
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You seem to want:
- The last two fields of each
AK3
line, followed by - Fields four and five from the
AK4
line (field five may be missing).
$ awk -F '*' '/^AK3/ printf("%st%s", $2, $3) /^AK4/ printf("t%st%sn", $4, $5) ' data.in
BPS 2 1
RMT 12 7 CR
add a comment |Â
up vote
0
down vote
You seem to want:
- The last two fields of each
AK3
line, followed by - Fields four and five from the
AK4
line (field five may be missing).
$ awk -F '*' '/^AK3/ printf("%st%s", $2, $3) /^AK4/ printf("t%st%sn", $4, $5) ' data.in
BPS 2 1
RMT 12 7 CR
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You seem to want:
- The last two fields of each
AK3
line, followed by - Fields four and five from the
AK4
line (field five may be missing).
$ awk -F '*' '/^AK3/ printf("%st%s", $2, $3) /^AK4/ printf("t%st%sn", $4, $5) ' data.in
BPS 2 1
RMT 12 7 CR
You seem to want:
- The last two fields of each
AK3
line, followed by - Fields four and five from the
AK4
line (field five may be missing).
$ awk -F '*' '/^AK3/ printf("%st%s", $2, $3) /^AK4/ printf("t%st%sn", $4, $5) ' data.in
BPS 2 1
RMT 12 7 CR
answered Dec 14 '17 at 15:13
Kusalananda
104k14206324
104k14206324
add a comment |Â
add a comment |Â
up vote
0
down vote
Another way is to directly control the output record separator(ORS)
$ awk -F'*' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS 2 1
RMT 12 7 CR
- by default, output field separator(OFS) is single space
- and input record separator(RS) is newline
This is easy to modify for different OFS and also shows empty field
$ awk -F'*' -v OFS=',' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS,2,1,
RMT,12,7,CR
add a comment |Â
up vote
0
down vote
Another way is to directly control the output record separator(ORS)
$ awk -F'*' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS 2 1
RMT 12 7 CR
- by default, output field separator(OFS) is single space
- and input record separator(RS) is newline
This is easy to modify for different OFS and also shows empty field
$ awk -F'*' -v OFS=',' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS,2,1,
RMT,12,7,CR
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Another way is to directly control the output record separator(ORS)
$ awk -F'*' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS 2 1
RMT 12 7 CR
- by default, output field separator(OFS) is single space
- and input record separator(RS) is newline
This is easy to modify for different OFS and also shows empty field
$ awk -F'*' -v OFS=',' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS,2,1,
RMT,12,7,CR
Another way is to directly control the output record separator(ORS)
$ awk -F'*' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS 2 1
RMT 12 7 CR
- by default, output field separator(OFS) is single space
- and input record separator(RS) is newline
This is easy to modify for different OFS and also shows empty field
$ awk -F'*' -v OFS=',' '$1=="AK3"ORS=OFS; print $2, $3 $1=="AK4"ORS=RS; print $4, $5' ip.txt
BPS,2,1,
RMT,12,7,CR
answered Dec 14 '17 at 15:21
Sundeep
6,9511826
6,9511826
add a comment |Â
add a comment |Â
up vote
0
down vote
Plain print
adds a newline (the output record separator), but printf
doesn't, so we can use that on the AK3
case with an explicit separator between the fields. In the AK4
case, we want a field separator before the first field, and the simplest way to get that is to print an empty field first.
$ awk -F* '$1=="AK3" printf "%s", $2 OFS $3 $1=="AK4" print "", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
This is easily converted to giving tab-spaced output, just add -vOFS='t'
in the beginning of the command line.
Though if you want fixed-width columns, it's probably easiest to use printf
and width settings on the %s
format:
$ awk -F* '$1=="AK3" printf "%-3s %-2s", $2, $3 $1=="AK4" printf " %-1s %sn", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
Over-long fields will still extend, unless you give the maximum field width too, with e.g. %-3.3s
, and this will produce broken output if the input lines are not in correct order.
add a comment |Â
up vote
0
down vote
Plain print
adds a newline (the output record separator), but printf
doesn't, so we can use that on the AK3
case with an explicit separator between the fields. In the AK4
case, we want a field separator before the first field, and the simplest way to get that is to print an empty field first.
$ awk -F* '$1=="AK3" printf "%s", $2 OFS $3 $1=="AK4" print "", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
This is easily converted to giving tab-spaced output, just add -vOFS='t'
in the beginning of the command line.
Though if you want fixed-width columns, it's probably easiest to use printf
and width settings on the %s
format:
$ awk -F* '$1=="AK3" printf "%-3s %-2s", $2, $3 $1=="AK4" printf " %-1s %sn", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
Over-long fields will still extend, unless you give the maximum field width too, with e.g. %-3.3s
, and this will produce broken output if the input lines are not in correct order.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Plain print
adds a newline (the output record separator), but printf
doesn't, so we can use that on the AK3
case with an explicit separator between the fields. In the AK4
case, we want a field separator before the first field, and the simplest way to get that is to print an empty field first.
$ awk -F* '$1=="AK3" printf "%s", $2 OFS $3 $1=="AK4" print "", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
This is easily converted to giving tab-spaced output, just add -vOFS='t'
in the beginning of the command line.
Though if you want fixed-width columns, it's probably easiest to use printf
and width settings on the %s
format:
$ awk -F* '$1=="AK3" printf "%-3s %-2s", $2, $3 $1=="AK4" printf " %-1s %sn", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
Over-long fields will still extend, unless you give the maximum field width too, with e.g. %-3.3s
, and this will produce broken output if the input lines are not in correct order.
Plain print
adds a newline (the output record separator), but printf
doesn't, so we can use that on the AK3
case with an explicit separator between the fields. In the AK4
case, we want a field separator before the first field, and the simplest way to get that is to print an empty field first.
$ awk -F* '$1=="AK3" printf "%s", $2 OFS $3 $1=="AK4" print "", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
This is easily converted to giving tab-spaced output, just add -vOFS='t'
in the beginning of the command line.
Though if you want fixed-width columns, it's probably easiest to use printf
and width settings on the %s
format:
$ awk -F* '$1=="AK3" printf "%-3s %-2s", $2, $3 $1=="AK4" printf " %-1s %sn", $4, $5 ' < input
BPS 2 1
RMT 12 7 CR
Over-long fields will still extend, unless you give the maximum field width too, with e.g. %-3.3s
, and this will produce broken output if the input lines are not in correct order.
edited Dec 14 '17 at 15:23
answered Dec 14 '17 at 15:11
ilkkachu
49.9k674137
49.9k674137
add a comment |Â
add a comment |Â
up vote
0
down vote
How about:
awk -F '*' '
printf "%s %s", $2, $3
getline
for (i=4; i<=NF; i++) printf " %s", $i
print ""
' file | column -t
BPS 2 1
RMT 12 7 CR
With plain bash:
while IFS='*' read -r a1 b1 c1; IFS='*' read -r a2 b2 c3 rest
do
echo "$b1 $c1 $rest//*/ "
done < file
I'm assuming there are always pairs of lines.
â glenn jackman
Dec 14 '17 at 15:59
Wow! Thank you very much for all the answers I tried all of them and I always got the result that I was expecting. Amazing ! help
â Antonio Pizarro
Dec 14 '17 at 19:46
lol. Give a fun puzzle to a bunch of nerds, you'll get a lot of responses.
â glenn jackman
Dec 14 '17 at 20:24
add a comment |Â
up vote
0
down vote
How about:
awk -F '*' '
printf "%s %s", $2, $3
getline
for (i=4; i<=NF; i++) printf " %s", $i
print ""
' file | column -t
BPS 2 1
RMT 12 7 CR
With plain bash:
while IFS='*' read -r a1 b1 c1; IFS='*' read -r a2 b2 c3 rest
do
echo "$b1 $c1 $rest//*/ "
done < file
I'm assuming there are always pairs of lines.
â glenn jackman
Dec 14 '17 at 15:59
Wow! Thank you very much for all the answers I tried all of them and I always got the result that I was expecting. Amazing ! help
â Antonio Pizarro
Dec 14 '17 at 19:46
lol. Give a fun puzzle to a bunch of nerds, you'll get a lot of responses.
â glenn jackman
Dec 14 '17 at 20:24
add a comment |Â
up vote
0
down vote
up vote
0
down vote
How about:
awk -F '*' '
printf "%s %s", $2, $3
getline
for (i=4; i<=NF; i++) printf " %s", $i
print ""
' file | column -t
BPS 2 1
RMT 12 7 CR
With plain bash:
while IFS='*' read -r a1 b1 c1; IFS='*' read -r a2 b2 c3 rest
do
echo "$b1 $c1 $rest//*/ "
done < file
How about:
awk -F '*' '
printf "%s %s", $2, $3
getline
for (i=4; i<=NF; i++) printf " %s", $i
print ""
' file | column -t
BPS 2 1
RMT 12 7 CR
With plain bash:
while IFS='*' read -r a1 b1 c1; IFS='*' read -r a2 b2 c3 rest
do
echo "$b1 $c1 $rest//*/ "
done < file
answered Dec 14 '17 at 15:54
glenn jackman
46.7k265103
46.7k265103
I'm assuming there are always pairs of lines.
â glenn jackman
Dec 14 '17 at 15:59
Wow! Thank you very much for all the answers I tried all of them and I always got the result that I was expecting. Amazing ! help
â Antonio Pizarro
Dec 14 '17 at 19:46
lol. Give a fun puzzle to a bunch of nerds, you'll get a lot of responses.
â glenn jackman
Dec 14 '17 at 20:24
add a comment |Â
I'm assuming there are always pairs of lines.
â glenn jackman
Dec 14 '17 at 15:59
Wow! Thank you very much for all the answers I tried all of them and I always got the result that I was expecting. Amazing ! help
â Antonio Pizarro
Dec 14 '17 at 19:46
lol. Give a fun puzzle to a bunch of nerds, you'll get a lot of responses.
â glenn jackman
Dec 14 '17 at 20:24
I'm assuming there are always pairs of lines.
â glenn jackman
Dec 14 '17 at 15:59
I'm assuming there are always pairs of lines.
â glenn jackman
Dec 14 '17 at 15:59
Wow! Thank you very much for all the answers I tried all of them and I always got the result that I was expecting. Amazing ! help
â Antonio Pizarro
Dec 14 '17 at 19:46
Wow! Thank you very much for all the answers I tried all of them and I always got the result that I was expecting. Amazing ! help
â Antonio Pizarro
Dec 14 '17 at 19:46
lol. Give a fun puzzle to a bunch of nerds, you'll get a lot of responses.
â glenn jackman
Dec 14 '17 at 20:24
lol. Give a fun puzzle to a bunch of nerds, you'll get a lot of responses.
â glenn jackman
Dec 14 '17 at 20:24
add a comment |Â
up vote
0
down vote
I achieved the result by below commands
sed -n '1~2p' testfile.txt| awk -F "*" 'print $2,$3' >> op.txt
sed -n '2~2p' testfile.txt | awk -F "*" 'print $4,$5' >> op.txt
sed -n '1~2p' op.txt |sed "N;s/n/ /g" |awk 'print $0"n"'; sed -n '2~2p' op.txt| sed "N;s/n/ /g"
Output
BPS 2 1
RMT 12 7 CR
add a comment |Â
up vote
0
down vote
I achieved the result by below commands
sed -n '1~2p' testfile.txt| awk -F "*" 'print $2,$3' >> op.txt
sed -n '2~2p' testfile.txt | awk -F "*" 'print $4,$5' >> op.txt
sed -n '1~2p' op.txt |sed "N;s/n/ /g" |awk 'print $0"n"'; sed -n '2~2p' op.txt| sed "N;s/n/ /g"
Output
BPS 2 1
RMT 12 7 CR
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I achieved the result by below commands
sed -n '1~2p' testfile.txt| awk -F "*" 'print $2,$3' >> op.txt
sed -n '2~2p' testfile.txt | awk -F "*" 'print $4,$5' >> op.txt
sed -n '1~2p' op.txt |sed "N;s/n/ /g" |awk 'print $0"n"'; sed -n '2~2p' op.txt| sed "N;s/n/ /g"
Output
BPS 2 1
RMT 12 7 CR
I achieved the result by below commands
sed -n '1~2p' testfile.txt| awk -F "*" 'print $2,$3' >> op.txt
sed -n '2~2p' testfile.txt | awk -F "*" 'print $4,$5' >> op.txt
sed -n '1~2p' op.txt |sed "N;s/n/ /g" |awk 'print $0"n"'; sed -n '2~2p' op.txt| sed "N;s/n/ /g"
Output
BPS 2 1
RMT 12 7 CR
answered Dec 14 '17 at 19:02
Praveen Kumar BS
1,010128
1,010128
add a comment |Â
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%2f410888%2fawk-issue-using-unix-scripts%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
What's that
c
about? You seem to be missing a)
somewhere...â Kusalananda
Dec 14 '17 at 15:03
What happens if you have a line starting with
AK3
following another one withAK3
?â ilkkachu
Dec 14 '17 at 15:04