UNIX Command to replace particular field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX Command to replace particular field
# 1  
Old 02-16-2012
UNIX Command to replace particular field

Hello All,
I need your help in getting a command. I have a input file as below:

"A0C0VN","XZ70720","02","MEMBER ELIGIBILITY_DC",2012021308,"",,,20120213

I need a command to remove the field in Bold. To be specific the 5th field along with the next comma (2012021308,) needs to be taken off from the line..

I there any specific command which can achieve this?

Thanks a lot for your support..
# 2  
Old 02-16-2012
Try this:
Code:
awk -F, '{for(i=5;i<NF;i++){$i=$(i+1)}NF-=1}1' OFS="," file


Last edited by Franklin52; 02-16-2012 at 04:28 AM..
# 3  
Old 02-16-2012
Code:
sed 's/[^,]*,//5' yourfile

These 2 Users Gave Thanks to ctsgnb For This Post:
# 4  
Old 02-16-2012
Dear Praveen,
Try the below mentioned command to solve the mentioned one,
CODE:
in sed ::
Code:
sed 's/,[0-9][0-9]*,/,/g' File1


in nawk ::
Code:
nawk '{gsub(/,[0-9]*,/,",");print ;}' File1

Your i/p is
Code:
"A0C0VN","XZ70720","02","MEMBER ELIGIBILITY_DC",2012021308,"",,,20120213

and u will get o/p as
Code:
"A0C0VN","XZ70720","02","MEMBER ELIGIBILITY_DC","",,,20120213


Moderator's Comments:
Mod Comment How to use code tags



Thanks & Regards.
Shanmu

Last edited by Franklin52; 02-16-2012 at 06:52 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 02-16-2012
--deleted--

Last edited by ctsgnb; 02-16-2012 at 03:09 PM.. Reason: I see i did not refresh quick enough ... fixed! :)
# 6  
Old 02-16-2012
I see.. I was unable to remove it quickly enough Smilie
# 7  
Old 02-16-2012
Lol Scruti Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command to extract empty field in a large UNIX file?

Hi All, I have records in unix file like below. In this file, we have empty fields from 4th Column to 22nd Column. I have some 200000 records in a file. I want to extract records only which have empty fields from 4th field to 22nd filed. This file is comma separated file. what is the unix... (2 Replies)
Discussion started by: rakeshp
2 Replies

2. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

4. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

5. Shell Programming and Scripting

Search and replace field?

I have 2 files A.txt and B.txt A.txt 3 fields and separate by a comma some,thing,florida any1,thing1,california some2,thing2,dallas just,fun,kansas B.txt has 8 fields and separate by a comma what,ever,florida-state,,,,,, some,one,dallas_state,,,,,, You will see 3rd fields are the... (5 Replies)
Discussion started by: sabercats
5 Replies

6. Shell Programming and Scripting

Using sed command replace date variable in unix

I need to use a shell script, using sed command how to replace date variable value in following format. 04/18/2012 11:38:55 Because the sed is treating the '/' as a parameter instead of the value of a variable, and hence there is the message as sed: command garbled: s/insert/04/18/2012... (9 Replies)
Discussion started by: jannusuresh
9 Replies

7. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

8. Shell Programming and Scripting

What is find and replace command in unix?

hello forum memvers, 1:I have to write a script for find a string and replace with another string. 2:In shell script how to replace one string with another string.:b: (4 Replies)
Discussion started by: rajkumar_g
4 Replies

9. Shell Programming and Scripting

Replace Date field in Unix File

I have a data file having first 19 characters having the date in the below format- 2010-04-29-00.00.00 I have to check this date field ( first 19 characters) against some specific dates and if date is not in 3 valid dates ( business date available to me , business date - 1 , businessdate... (10 Replies)
Discussion started by: varunrbs
10 Replies

10. Shell Programming and Scripting

Replace 10th Field by 2

Hello, I need some help ....i have a file with many number of fields i want to replace the 10th field of the file by number 2 .Also the delimeter of the file is | . Pls suggest and help.. Regards, (2 Replies)
Discussion started by: PradeepRed
2 Replies
Login or Register to Ask a Question