Hello Team,
I want to know if there is any one liner command , using which I can edit multiple column of a file.
input file input.txt (comma separated),
taran, 12.45, uttam, 23.40, babay
karan, 12.45, raju, 11.40, rahulg
I want to update, 2nd and 4th column,
but want all those column in output.
output,
taran, 12:45, uttam, 23:40, babay
karan, 12:45, raju, 11:40, rahul
if we can update at least one column, the also it would be helpful.
But I want all those column in output.
Many Thanks
Uttam Maji
Are you familiar with either ed
or ex
...
As far as I can see you only want to substitute the period .
with colon :
in fields 2 and 4? If yes, try this
awk 'BEGIN { FS=OFS="," } { sub("\.",":",$2); sub("\.",":",$4); print }' file
But its giving Syntax error.
cat 2.txt
uttam,23.40,ravi,23.40,uttam
uttam,23.40,ravi,23.40,uttam
uttam,23.40,ravi,23.40,uttam
uttam,23.40,ravi,23.40,uttam
awk 'BEGIN { FS=OFS="," } { sub("\.",":",$2); sub("\.",":",$4); print }' 2.txt
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1
But its giving Syntax error.
cat 2.txt
uttam,23.40,ravi,23.40,uttam
uttam,23.40,ravi,23.40,uttam
uttam,23.40,ravi,23.40,uttam
uttam,23.40,ravi,23.40,uttam
awk 'BEGIN { FS=OFS="," } { sub("\.",":",$2); sub("\.",":",$4); print }' 2.txt
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1
What OS and shell are you using? I would't expect the above code to work correctly, but I wouldn't expect it to generate syntax errors either.
Try the following instead:
awk 'BEGIN { FS=OFS="," } { sub("\\.",":",$2); sub("\\.",":",$4); print }' 2.txt
or, more simply (since it doesn't matter how many times the string containing the ERE is evaluated):
awk 'BEGIN { FS=OFS="," } { sub("[.]",":",$2); sub("[.]",":",$4); print }' 2.txt
If you are running this on a Solaris/SunOS system, change awk
in either of these scripts to /usr/xpg4/bin/awk
, /usr/xpg6/bin/awk
, or nawk
.
What is the result of running:
awk -V
Also, the awk script has a small typo - the "\." is interpreted as a plain ".", a second "\" is needed:
awk 'BEGIN { FS=OFS="," } { sub("\\.",":",$2); sub("\\.",":",$4); print }
(at least, "\\." is needed for gawk)
awk 'NF{ sub(/\./,":",$2); sub(/\./,":",$4) }1' FS=, OFS=, infile
I tried all but tehy are giving syntax Error.
I am ysing korn shell. Solaries OS.
Actually, all are working, provided you take Don's advice into account.
If you are running this on a Solaris/SunOS system, change awk
in either of these scripts to /usr/xpg4/bin/awk
, /usr/xpg6/bin/awk
, or nawk
.