Hello people, newbie question. I'm trying to parse these type of file
1 "CAR " " C1 " " " 6 0 C1
2 "CAR " " O1A" " " 8 0 O1A
3 "CAR " " O1B" " " 8 -1 O1B
4 "CAR " " C2 " " " 6 0 C2
5 "CAR " " C3 " " " 6 0 C3
6 "CAR " "H5A1" " " 8 0 H5A1
to the new one
1 "CAR " " C1 " " " 6 0 0 1 "C1" 0
2 "CAR " " O1A" " " 8 0 0 1 "O1A" 0
3 "CAR " " O1B" " " 8 -1 0 1 "O1B" 0
4 "CAR " " C2 " " " 6 0 0 1 "C2" 0
5 "CAR " " C3 " " 1f" 6 0 0 1 "C3" 0
6 "CAR " "H5A1" " " 6 0 0 1 "H5A1" 0
Every field are constant, but I have to insert the 1f value in a certain line (here the number 5) and i know previosly what is it's (i think i will use a counter to do it).
Are sed/awk the best approach to do it? How is the best way to start, extract the values with awk and then modify it with sed? is a mess that some fields could have white spaces ( " C1 ", " O1B", "H5A1").
I'm starting with this, could anybody help me with the code?
Thank you in advanced. Best regards.
nawk '{$NF = "0 1" "\"" $NF "\" 0"; print}' myFile
Great!!! thank you very much. And, could you help me about how is the way to insert the 1f value in a certain line, for example in the line number five? (whoa, this is really powerful).
Thank youn in advanced.
nawk '{$NF = "0 1" "\"" $NF "\" 0"; if (FNR==5) $8= "1f" $8; print}' myFile
I have a problem with white spaces, they are important for the specified file format, and using the awk command
awk '{$NF = "0 1" "\"" $NF "\" 0"; print}' myFile
i retrieve the correct output, but with the fields separated only for one white space.
Please, what could be the way to parse this lines...?
1 2 -5.288000 -3.217000 4.035000 1 " " X " " 2 0.17850 0.00000 "car " " C1 " " " 6 0 C1
2 15 -5.385000 -3.069000 5.276000 1 " " X " " 70 -0.64780 0.00000 "car " " O1a" " " 8 0 O1a
3 18 -6.014000 -4.000000 3.392000 1 " " X " " 70 -0.64780 0.00000 "car " " O1b" " " 8 -1 O1b
4 3 -4.190000 -2.467000 3.295000 1 " " X " " 2 0.06130 0.00000 "car " " C2 " " " 6 0 C2
5 3 -3.620000 -3.260000 2.103000 1 " " X " " 2 0.09070 0.00000 "car " " C3 " " " 6 0 C3
6 16 -2.802000 -2.377000 1.324000 1 " " X " " 70 -0.38950 0.00000 "car " " O3 " " " 8 0 O3
...to this new ones, respecting the number of white spaces?
1 2 -5.288000 -3.217000 4.035000 1 " " X " " 2 0.17850 0.00000 "CAR " " C1 " " " 6 0 0 1 "C1" 0
2 15 -5.385000 -3.069000 5.276000 1 " " X " " 70 -0.64780 0.00000 "CAR " " O1A" " " 8 0 0 1 "O1A" 0
3 18 -6.014000 -4.000000 3.392000 1 " " X " " 70 -0.64780 0.00000 "CAR " " O1B" " " 8 -1 0 1 "O1B" 0
4 3 -4.190000 -2.467000 3.295000 1 " " X " " 2 0.06130 0.00000 "CAR " " C2 " " " 6 0 0 1 "C2" 0
5 3 -3.620000 -3.260000 2.103000 1 " " X " " 2 0.09070 0.00000 "CAR " " C3 " " 1f" 6 0 0 1 "C3" 0
6 3 -2.750000 -4.417000 2.639000 1 " " X " " 2 0.14300 0.00000 "CAR " " C4 " " " 6 0 0 1 "C4" 0
I use html tag to reflect the correct white spaces. Thank you in advanced.
aristegui:
I have a problem with white spaces, they are important for the specified file format, and using the awk command
awk '{$NF = "0 1" "\"" $NF "\" 0"; print}' myFile
i retrieve the correct output, but with the fields separated only for one white space.
Please, what could be the way to parse this lines...?
awk -F'[ ]' '{$NF = "0 1" "\"" $NF "\"\t0"; print}' file
Try this one:
$ cat source
1 2 -5.288000 -3.217000 4.035000 1 " " X " " 2 0.17850 0.00000 "car " " C1 " " " 6 0 C1
2 15 -5.385000 -3.069000 5.276000 1 " " X " " 70 -0.64780 0.00000 "car " " O1a" " " 8 0 O1a
3 18 -6.014000 -4.000000 3.392000 1 " " X " " 70 -0.64780 0.00000 "car " " O1b" " " 8 -1 O1b
4 3 -4.190000 -2.467000 3.295000 1 " " X " " 2 0.06130 0.00000 "car " " C2 " " " 6 0 C2
5 3 -3.620000 -3.260000 2.103000 1 " " X " " 2 0.09070 0.00000 "car " " C3 " " " 6 0 C3
$ cat output
1 2 -5.288000 -3.217000 4.035000 1 " " X " " 2 0.17850 0.00000 "CAR " " C1 " " " 6 0 0 1 "C1" 0
2 15 -5.385000 -3.069000 5.276000 1 " " X " " 70 -0.64780 0.00000 "CAR " " O1A" " " 8 0 0 1 "O1A" 0
3 18 -6.014000 -4.000000 3.392000 1 " " X " " 70 -0.64780 0.00000 "CAR " " O1B" " " 8 -1 0 1 "O1B" 0
4 3 -4.190000 -2.467000 3.295000 1 " " X " " 2 0.06130 0.00000 "CAR " " C2 " " " 6 0 0 1 "C2" 0
5 3 -3.620000 -3.260000 2.103000 1 " " X " " 2 0.09070 0.00000 "CAR " " C3 " " 1f" 6 0 0 1 "C3" 0
$ awk '{
OFS=""
a=toupper($0)
b=substr(a,1,109)
d=substr(a,112,10)" 0 1 \""
e=substr(a,123)"\" 0"
}
{
if(NR==5)c="1f"
else c=" "
}
{
print b,c,d,e
}
' source > output1
$ diff -s output output1
Files output and output1 are identical
danmero:
Try this one:
$ cat source
1 2 -5.288000 -3.217000 4.035000 1 " " X " " 2 0.17850 0.00000 "car " " C1 " " " 6 0 C1
2 15 -5.385000 -3.069000 5.276000 1 " " X " " 70 -0.64780 0.00000 "car " " O1a" " " 8 0 O1a
3 18 -6.014000 -4.000000 3.392000 1 " " X " " 70 -0.64780 0.00000 "car " " O1b" " " 8 -1 O1b
4 3 -4.190000 -2.467000 3.295000 1 " " X " " 2 0.06130 0.00000 "car " " C2 " " " 6 0 C2
5 3 -3.620000 -3.260000 2.103000 1 " " X " " 2 0.09070 0.00000 "car " " C3 " " " 6 0 C3
$ cat output
1 2 -5.288000 -3.217000 4.035000 1 " " X " " 2 0.17850 0.00000 "CAR " " C1 " " " 6 0 0 1 "C1" 0
2 15 -5.385000 -3.069000 5.276000 1 " " X " " 70 -0.64780 0.00000 "CAR " " O1A" " " 8 0 0 1 "O1A" 0
3 18 -6.014000 -4.000000 3.392000 1 " " X " " 70 -0.64780 0.00000 "CAR " " O1B" " " 8 -1 0 1 "O1B" 0
4 3 -4.190000 -2.467000 3.295000 1 " " X " " 2 0.06130 0.00000 "CAR " " C2 " " " 6 0 0 1 "C2" 0
5 3 -3.620000 -3.260000 2.103000 1 " " X " " 2 0.09070 0.00000 "CAR " " C3 " " 1f" 6 0 0 1 "C3" 0
$ awk '{
OFS=""
a=toupper($0)
b=substr(a,1,109)
d=substr(a,112,10)" 0 1 \""
e=substr(a,123)"\" 0"
}
{
if(NR==5)c="1f"
else c=" "
}
{
print b,c,d,e
}
' source > output1
$ diff -s output output1
Files output and output1 are identical
If i want to put this code in a script
#! /bin/awk -f
(...)
what is the correct syntax? i'm unable to run this from a script. Thank you in advanced.
Here is the file:
$ cat awk.file
{
OFS=""
a=toupper($0)
b=substr(a,1,109)
d=substr(a,112,10)" 0 1 \""
e=substr(a,123)"\" 0"
}
{
if(NR==5)c="1f"
else c=" "
}
{
print b,c,d,e
}
Ok, thank you very much indeed, i see that is not mandatory to use
#! /bin/awk -f
blah blah blah
My last question (in this thread), please, how is the way to put in a script (script.awk file) this type of command-line awk's?
awk -F'[ ]' '{ /[0-9]+/ && $NR > 6 && $NF = "0 1"; print}'
I think is a problem with the quotes, and i don't know how to scecify the -F'[ ]'
parameter. Thanks!!!
$ cat awkprog
BEGIN{ FS="[ ]" }
/[0-9]+/ && $NR > 6 && $NF = "0 1"
$ awk -f awkprog file