Replace column help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace column help
# 1  
Old 08-30-2016
Replace column help

I have a pipe delimited file.
Below are the contents

Code:
order_id|line_id|retail|cost
2324|1|99.99|50
89793|23|100|23.99
90989|9|23|76.50|56.5

I need to zero out the 3rd column(retail)
Below is what i want

Code:
order_id|line_id|retail|cost
2324|1|0|50
89793|23|0|23.99
90989|9|23|0|56.5

Any help is appreciated
Thanks
Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules) when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 08-30-2016 at 08:30 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-30-2016
awk -F'|' 'BEGIN {OFS="|"} NR>1{{$3=0}{print}}' file_name

You will have to handle header separately.
# 3  
Old 08-30-2016
It is always a good idea to tell us what operating system and shell you're using when you start a thread like this.

The following is untested, but should do what you want (including copying the header unchanged):
Code:
awk '
BEGIN {	FS = OFS = "|" }
NR > 1 {$3 = 0 }
1' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-30-2016
Quote:
Originally Posted by Don Cragun
It is always a good idea to tell us what operating system and shell you're using when you start a thread like this.

The following is untested, but should do what you want (including copying the header unchanged):
Code:
awk '
BEGIN {	FS = OFS = "|" }
NR > 1 {$3 = 0 }
1' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Don,
Thanks for the quick response. The environment I am in is AIX and the shell is KSH

---------- Post updated at 08:04 PM ---------- Previous update was at 07:43 PM ----------

Thanks Don. This seems to work. If you don't mind can you explain what the command is doing? This is a really good forum and I have learnt a great deal from it
# 5  
Old 08-30-2016
Here is the script with comments:
Code:
awk '			# Start awk script...
BEGIN {	FS = OFS = "|" }# Before reading any lines from the input file, set the
			# input file field separator (FS) and the output file
			# field separator (OFS) to the pipe symbol.
NR > 1 {$3 = 0 }	# For every line where the input file line number (NR)
			# is greater than 1 (i.e., skip the header line) set
			# field #3 to zero.
1			# For every line in the input file, perform the default
			# action (print the current input line possibly modified
			# by the above actions).
' file			# End the awk script and name the input file to be
			# processed.

Let us know if you still have questions.

Note also that the last line of the sample input you provided:
Code:
order_id|line_id|retail|cost
2324|1|99.99|50
89793|23|100|23.99
90989|9|23|76.50|56.5

has five fields (not four) and the output this script produces:
Code:
order_id|line_id|retail|cost
2324|1|0|50
89793|23|0|23.99
90989|9|0|76.50|56.5

has the 3rd field in this line set to zero; not the 4th field you said you wanted:
Code:
order_id|line_id|retail|cost
2324|1|0|50
89793|23|0|23.99
90989|9|23|0|56.5

If what you wanted was to set the next to the last field to zero instead of the 3rd field to zero, that could be done with:
Code:
awk '			# Start awk script...
BEGIN {	FS = OFS = "|" }# Before reading any lines from the input file, set the
			# input file field separator (FS) and the output file
			# field separator (OFS) to the pipe symbol.
NR > 1 {$(NF-1) = 0 }	# For every line where the input file line number (NR)
			# is greater than 1 (i.e., skip the header line) set
			# field the next to the last field (field number NF - 1)
			# to zero.
1			# For every line in the input file, perform the default
			# action (print the current input line possibly modified
			# by the above actions).
' file			# End the awk script and name the input file to be
			# processed.

but that is not what your English description of the problem requested.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-30-2016
Quote:
Originally Posted by Don Cragun
Here is the script with comments:
Code:
awk '            # Start awk script...
BEGIN {    FS = OFS = "|" }# Before reading any lines from the input file, set the
            # input file field separator (FS) and the output file
            # field separator (OFS) to the pipe symbol.
NR > 1 {$3 = 0 }    # For every line where the input file line number (NR)
            # is greater than 1 (i.e., skip the header line) set
            # field #3 to zero.
1            # For every line in the input file, perform the default
            # action (print the current input line possibly modified
            # by the above actions).
' file            # End the awk script and name the input file to be
            # processed.

Let us know if you still have questions.

Note also that the last line of the sample input you provided:
Code:
order_id|line_id|retail|cost
2324|1|99.99|50
89793|23|100|23.99
90989|9|23|76.50|56.5

has five fields (not four) and the output this script produces:
Code:
order_id|line_id|retail|cost
2324|1|0|50
89793|23|0|23.99
90989|9|0|76.50|56.5

has the 3rd field in this line set to zero; not the 4th field you said you wanted:
Code:
order_id|line_id|retail|cost
2324|1|0|50
89793|23|0|23.99
90989|9|23|0|56.5

If what you wanted was to set the next to the last field to zero instead of the 3rd field to zero, that could be done with:
Code:
awk '            # Start awk script...
BEGIN {    FS = OFS = "|" }# Before reading any lines from the input file, set the
            # input file field separator (FS) and the output file
            # field separator (OFS) to the pipe symbol.
NR > 1 {$(NF-1) = 0 }    # For every line where the input file line number (NR)
            # is greater than 1 (i.e., skip the header line) set
            # field the next to the last field (field number NF - 1)
            # to zero.
1            # For every line in the input file, perform the default
            # action (print the current input line possibly modified
            # by the above actions).
' file            # End the awk script and name the input file to be
            # processed.

but that is not what your English description of the problem requested.

Thanks for the detailed response Don. It was a typo on the input file. There should have been only four columns. Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Get extract and replace column with link in a column where it exists

hi i have sample data a,b,c,d,e,g h http://mysite.xyx z,b,d,f,e,s t http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing output expected is a,b,c,d,e,http://mysite.xyx z,b,d,f,e,http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing i can get only links using grep -o 'http.*' i... (8 Replies)
Discussion started by: zozoo
8 Replies

3. Shell Programming and Scripting

Replace column value

Hi I need to replace particular column value with blank.For eg 2016-03-21,ABC,"3,447,001" 2016-03-21,DFH,"1,469,922" 2016-03-21,DGH,"1,469" I have data like above.I need to replace the comma in 3rd column with blank in the original file itself .My expected result is given below ... (2 Replies)
Discussion started by: ginrkf
2 Replies

4. UNIX for Dummies Questions & Answers

column replace

Hello. I'm trying to replace the 1st column of text with the word Blue. The string is text and numbers. Trying to learn awk and have been searching forums but can't come up with anything. Any help would be appreciated. (4 Replies)
Discussion started by: jimmyf
4 Replies

5. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

6. Shell Programming and Scripting

Replace column with column from another file

Hello, I am trying to replace the column in file1 with the column from file2. The two files will have the same amount of rows. Each row will correspond with the same row in the other file. File1 "Replace this column" 500 13-APR-2011... (11 Replies)
Discussion started by: doobe01
11 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

9. UNIX for Advanced & Expert Users

replace a column values with the first value in column

Hi All, I have a file which has data in following format: "Body_Model","2/1/2007","2/1/2007" "CSCH74","0","61" "CSCS74","0","647" "CSCX74","0","3" "CSYH74","0","299" "CSYS74","0","2514" "CSYX74","0","3" "Body_Model","3/1/2007","3/1/2007" "CSCH74","0","88" "CSCS74","0","489"... (3 Replies)
Discussion started by: sumeet
3 Replies

10. Shell Programming and Scripting

Replace 10th column with a new column--- Terriblly hurry

Hi Can any one tell me how to replace the 10th column in a file(comma delimted) with a new file with a single column. Can any one Help me out of the please as soon as possible as i am in a terribley hurry!!!!!! Many THanks, (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies
Login or Register to Ask a Question