Need an awk for a global find/replace in a file, specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need an awk for a global find/replace in a file, specific column
# 1  
Old 03-22-2010
Need an awk for a global find/replace in a file, specific column

I am new to unix and awk/sed etc... using C-Shell.

Basically, I have a fixed length file that has 4 different record types on it, H, D, V, W all in column 1. I need to change all the W's in column 1 to D's. in the entire file. The W's can be anywhere in the file and must remain in the same location as before.

Input File
Code:
H1111111111111
D1111111111111
D1111111111111
H2222222222222
D2222222222222
V3333333333333
W3333333333333
W3333333333333
V4444444444444
W4444444444444

The output file should look like the following:

Output File
Code:
H1111111111111
D1111111111111
D1111111111111
H2222222222222
D2222222222222
V3333333333333
D3333333333333
D3333333333333
V4444444444444
D4444444444444

Any help would be greatly appreciated.
Thanks.

Last edited by pludi; 03-22-2010 at 10:22 AM.. Reason: code tags, please...
# 2  
Old 03-22-2010
Code:
 sed 's/^W/D/' file

# 3  
Old 03-22-2010
Using awk:

awk '{if (substr($0,1,1) == "W") {print "D"substr($0,2)} else {print substr($0,1)}}' file
# 4  
Old 03-22-2010
Code:
awk ' { sub("^W","D") } 1 ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a Linux command for find/replace column based on specific criteria.

I'm new to shell programming, I have a huge text file in the following format, where columns are separated by single space: ACA MEX 4O_ $98.00 $127.40 $166.60 0:00 0:00 0 ; ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ACA YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ADZ YUL TS_ $300.00... (3 Replies)
Discussion started by: transat
3 Replies

2. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

3. Shell Programming and Scripting

How to replace a character in a specific column in a file?

This is a file that I have test line 1 (55) ) test line 2 (45) ) I would like to change all the parens in position 1 of this file to a ); i only want to check position 1 in every line of the file. I have tried different varations of sed, but cannot seem to be able to limit it to... (1 Reply)
Discussion started by: JoeG
1 Replies

4. Shell Programming and Scripting

awk or sed to find specific column from different files

Hi everybody, I have a folder with many files: Files with 8 columns: X 123 A B C D E F And files with 7 columns: X1234 A B C D E F I am trying to find a way to extract the 5th column when the files have eight columns, or the 4th column when the files have... (3 Replies)
Discussion started by: Tzole
3 Replies

5. Shell Programming and Scripting

Scripting a global find and replace in an VME output print file

Hi Folks, Below is an extract from a VME Print file which gets handed over to a print house. The problem I have is not that tricky rther looking for a way to handle it in a simple and clean way. Is to first select all lines with "0058" which have four spaces so "0058 " as the selcetion... (3 Replies)
Discussion started by: Gary Hay
3 Replies

6. 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

7. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Find in first column and replace the line with Awk, and output new file

Find in first column and replace the line with Awk, and output new file File1.txt"2011-11-02","Georgia","Atlanta","x","","" "2011-11-03","California","Los Angeles","x","","" "2011-11-04","Georgia","Atlanta","x","x","x" "2011-11-05","Georgia","Atlanta","x","x","" ... (4 Replies)
Discussion started by: charles33
4 Replies

9. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

10. UNIX for Dummies Questions & Answers

AWK Command to find text in specific column

I'm new to scripting and would appreciate any help. I have a list of over 20 words in File1 that I need to find in columns 10-15 of File2. I need the entire row of File2 that the File1 list matches. I originally used a grep command which works, but provides File1 results that can be found... (3 Replies)
Discussion started by: Chillin
3 Replies
Login or Register to Ask a Question