awk help - delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk help - delimited file
# 1  
Old 10-29-2010
awk help - delimited file

Hi,

I have a delimited file with 10 fields. I have to replace a character in field 1 and field 5. It is possible to do using awk where i can read a row at a time field by field and if the field value is 1 or 5 then i will replace the character.

But this operation is cumbersome since the file size is huge and if the columns are more than 100.

Is there an option where i can do the operation on the field without looping through every field ie Directly act on the field 1 and field 5 without reading all the other field values as in my previous case.
# 2  
Old 10-29-2010
please give a sample of input and output expected

what is your delimiter ?
which character do you want to replace, and by which one ?
# 3  
Old 10-29-2010
Sample data :

Code:
Record 1 : aca|bbb|ccc|ddd|eee|aaa|bb7
Record 2 : aca|xyz|xyz|ddd|eee|aaa|bb7

I want to replace character a in field 1 with & and character b in field 7 with #

Output:
Code:
Record 1 : &c&|bbb|ccc|ddd|eee|aaa|##7
Record 2 : &c&|xyz|xyz|ddd|eee|aaa|##7


Last edited by vgersh99; 10-29-2010 at 09:44 AM.. Reason: Code tags, please!
# 4  
Old 10-29-2010
Code:
awk -F\| '{gsub("a","&",$1);gsub("b","#",$7)}1' file > out.file

# 5  
Old 10-29-2010
Hi,

It is replacing the 7th field but not the first field..
This is the output i am getting

Code:
$ cat test1 
aca bbb ccc ddd eee aaa bb7
aca xyz xyz ddd eee aaa bb7

Code:
$ cat test1 | nawk -F\| '{gsub("a","&",$1);gsub("b","#",$7)}1'
aca bbb ccc ddd eee aaa ##7
aca xyz xyz ddd eee aaa ##7

# 6  
Old 10-29-2010
Code:
$ awk -F\| '{gsub("a","\\&",$1);gsub("b","#",$7)}1' file
Record 1 : &c& bbb ccc ddd eee aaa ##7
Record 2 : &c& xyz xyz ddd eee aaa ##7

# 7  
Old 10-30-2010
Thanks.

Is there anyway I can achieve it without using the "\\". The reason is, I am going to do it for a set of characters in a set of fields. So i am going to generate the command based on the inputs, if I were to add the "\\" it becomes difficult to identify for which characters i need to prefix with \\.
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 command to split pipe delimited file

Hello, I need to split a pipe de-limited file based on the COLUMN 7 value . If the column value changes I need to split the file Source File Payment|ID|DATE|TIME|CONTROLNUMBER|NUMBER|NAME|INDICATOR 42156974|1137937|10/1/2018|104440|4232|2054391|CARE|1... (9 Replies)
Discussion started by: rosebud123
9 Replies

2. UNIX for Beginners Questions & Answers

awk to parse current and next row in tab-delimited file

Hi there, I would like to use awk to reformat a tab-delimited file containing three columns as follows: Data file: sample 1 173 sample 269 530 sample 687 733 sample 1699 1779 Desired output file: sample 174..265, 531..686, 734..1698 I need the value... (5 Replies)
Discussion started by: emiley
5 Replies

3. UNIX for Dummies Questions & Answers

What awk 1-liner will replace value in 1stField of a delimited file with the value of '5' ?

Hi, I am a newbie to awk. Here is my problem. Looking for an awk 1-liner to solve it: My Computing Environment: - Solaris10 - I prefer to use csh or sh shells 1. Lets say my input file is File1.dat (delimter = | ) and looks as follows: (File1.dat) ... (1 Reply)
Discussion started by: andy b
1 Replies

4. Shell Programming and Scripting

awk read one delimited file, search another delimited file

Hello folks, I have another doozy. I have two files. The first file has four fields in it. These four fields map to different locations in my second file. What I want to do is read the master file (file 2 - 23 fields) and compare each line against each record in file 1. If I get a match in all four... (4 Replies)
Discussion started by: dagamier
4 Replies

5. Shell Programming and Scripting

Awk print all columns in delimited file

text file example 1,222222222222,333,444444444444444 111111111,22,33333333,444 desired output 1 222222222222 333 444444444444444 111111111 22 33333333 444I have a delimeted file which I want to print in a table like format. The... (10 Replies)
Discussion started by: Calypso
10 Replies

6. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

7. Shell Programming and Scripting

using awk to substitute data in a column delimited text file

using awk to substitute data in a column delimited text file hello i would like to use awk to do the following calculation from the following snippet. input file C;2390 ;CV BOUILLOTTE 2L 2FACES NERVUREES ;1.00 ;3552612239004;13417 ;25 ;50 ; 12;50000 ; ; ... (3 Replies)
Discussion started by: iindie
3 Replies

8. Shell Programming and Scripting

Awk search for string pattern in delimited file

I've got a semicolon delimited file. I would like to search for fields that match a pattern, and not hardcoded eg "mth". *th=something If the delimited field fulfills this condition, eg. mth=value I would like to print out both key and value for some number comparison. eg. if value > "12"... (5 Replies)
Discussion started by: alienated
5 Replies

9. Shell Programming and Scripting

splitting tab-delimited file with awk

Hi all, I need help to split a tab-delimited list into separate files by the filename-field. The list is already sorted ascendingly by filename, an example list would look like this; filename001 word1 word2 filename001 word3 word4 filename002 word1 word2 filename002 word3 word4... (4 Replies)
Discussion started by: perkele
4 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question