Need Help to Edit multiple column of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help to Edit multiple column of a file
# 1  
Old 11-25-2014
Question Need Help to Edit multiple column of a file

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),

Code:
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,

Code:
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

Last edited by Don Cragun; 11-26-2014 at 05:21 AM.. Reason: Add CODE tags.
# 2  
Old 11-25-2014
Are you familiar with either ed or ex...
# 3  
Old 11-25-2014
As far as I can see you only want to substitute the period . with colon : in fields 2 and 4? If yes, try this
Code:
awk 'BEGIN { FS=OFS="," } { sub("\.",":",$2); sub("\.",":",$4); print }' file

# 4  
Old 11-26-2014
But its giving Syntax error.

cat 2.txt
Code:
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


Code:
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


Last edited by Don Cragun; 11-26-2014 at 05:22 AM.. Reason: Add CODE tags.
# 5  
Old 11-26-2014
Quote:
Originally Posted by Uttam Maji
But its giving Syntax error.

cat 2.txt
Code:
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


Code:
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:
Code:
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):
Code:
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.
# 6  
Old 11-26-2014
What is the result of running:
Code:
awk -V

Also, the awk script has a small typo - the "\." is interpreted as a plain ".", a second "\" is needed:
Code:
awk 'BEGIN { FS=OFS="," } { sub("\\.",":",$2); sub("\\.",":",$4); print }

(at least, "\\." is needed for gawk)

Last edited by Don Cragun; 11-26-2014 at 06:32 AM.. Reason: Fix CODE tags.
# 7  
Old 11-26-2014
Code:
awk  'NF{ sub(/\./,":",$2); sub(/\./,":",$4) }1' FS=, OFS=, infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Edit and replace the multiple values in a file in one iteration

Hi All, I am preserving OLD and NEW values and want to replace the values in one go instead of using multiple sed and mv commands. Please help. echo "\nEnter the new qStart time '${CODE}' - (Hit Enter for No Change): \c" read NEW echo "\nEnter the new qStop time '${CODE}' - (Hit Enter for... (2 Replies)
Discussion started by: sdosanjh
2 Replies

3. Shell Programming and Scripting

Edit column using sort

Hi Expert, Please kindly need your help, I dont have any idea how to make this input to be as output. Thanks before $ more input.dat @zmap_fault HEADER , FALT, 80, 1 X (EASTING) , 1, 1, 1, 1, 15, 7, 0.1000000E+31, , 15, 7, 0 Y... (5 Replies)
Discussion started by: ipatah
5 Replies

4. Programming

Grepping a column from multiple file

I have 20 files that look pretty much like this: 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0 0.01 4 33 0.00189645523539 189.645523539 0 0.01 33 4... (5 Replies)
Discussion started by: kayak
5 Replies

5. Shell Programming and Scripting

Cut column and edit data

Mar 26 12:32:53 name sshd: 192.168.1.14 Mar 27 12:42:53 name sshd: 192.168.1.14 how to make this data in output as: "Mar 26 12:32:53","name","sshd","192.168.1.14" "Mar 27 12:42:53","name","sshd","192.168.1.14" anyone plzz help me out!!!!!!!!!!!!!! (4 Replies)
Discussion started by: jacky29
4 Replies

6. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

7. Shell Programming and Scripting

How to edit file sections that cross multiple lines?

Hello, I'm wondering where I could go to learn how to edit file sections that cross multiple lines. I'm wanting to write scripts that will add Gnome menu entries for all users on a system for scripts I write, etc. I can search an replace simple examples with sed, but this seems more complex. ... (8 Replies)
Discussion started by: Narnie
8 Replies

8. Shell Programming and Scripting

How to select and edit on a particular column?

How can I use awk to pick a particular column and work on it? For example, I want to count the number of characters in column10 that are separated by |? Thank you. (2 Replies)
Discussion started by: ivpz
2 Replies

9. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

10. AIX

Locking a file when using VI to prevent multiple-edit sessions by diff users

At the office, we often have to edit one file with VI. We are 4-6 workers doing it and sometimes can be done at the same time. We have found a problem and want to prevent it with a file lock. Is it possible and how ? problem : Worker-a starts edit VI session on File-A at 1PM Worker-b... (14 Replies)
Discussion started by: Browser_ice
14 Replies
Login or Register to Ask a Question