Insert field values in a record using awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert field values in a record using awk command
# 1  
Old 10-06-2014
Computer Insert field values in a record using awk command

Hi Friends,

Below is my input file with "|" (pipe) as filed delimiter:
My Input File:
Code:
HDR|F1|F2||||F6|F7

I want to inser values in the record for field 4 and field 5.
Expected output
Code:
HDR|F1|F2||F4|F5|F6|F7

I am able to append the string to the end of the record, but not in between the fileds. Below is my awk command:

Code:
awk '    
     BEGIN{FS = "|";RS = "\r\n"};
     FNR=NR{x = "F4";y="F5";z="|"
                     print $0 z x z y;
            } 
                    
             }' $Iputfile > $OutputFile


My Output:
Code:
HDR|F1|F2||||F6|F7|F4|F5

Please suggest me on how to insert the values in-between the fields.

Thanks in advance
# 2  
Old 10-06-2014
Code:
akshay@nio:/tmp$ cat file
HDR|F1|F2||||F6|F7

Code:
akshay@nio:/tmp$ awk '{ for(i=1;i<=NF;i++){ if(!length($i)){ $i="F"last+1} last=$i; gsub(/[^0-9]*/,"",last)  }}1' FS=\| OFS=\| file

Code:
HDR|F1|F2|F3|F4|F5|F6|F7

---------- Post updated at 01:33 PM ---------- Previous update was at 01:28 PM ----------

---

I see you edited expected output

add like this

Code:
awk '{$5 = "F4"; $6 = "F5"}1' FS=\| OFS=\| file
HDR|F1|F2||F4|F5|F6|F7

---------- Post updated at 01:54 PM ---------- Previous update was at 01:33 PM ----------

OR try something like this

Code:
awk -vfield='4=F4,5=F5'  'BEGIN{split(field,S,/,/); for(i in S){ split(S[i],D,/=/);F[D[1]+1]=D[2] }}{for(i in F)$i=F[i]}1' FS=\| OFS=\|  file

# 3  
Old 10-06-2014
Or much more simply:
Code:
awk -F'|' '{$4 = "F4"; $5 = "F5"}1' OFS='|' "$Iputfile" > "$OutputFile"

but you should verify that $Iputfile is correct. From your output file variable name I would have expected $Inputfile or $InputFile.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 10-06-2014
Thanks Don, it worked fine... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Require original field position after sorting the values in a record

Dear Team, Can any body help me out to get the filed position of the records post sorting using AWK programming. Thanks in advance Example Input: StudentID col-1 col-2 col-3 col-4 1234 14 10 12 13 1235 10 11 12 13 1236 13 12 11 10 ... (3 Replies)
Discussion started by: Srinivasa Reddy
3 Replies

2. Shell Programming and Scripting

Insert field between two fields using awk or sed

Hi All, I am trying to insert two columns in the following text. I tried awk but failed to achieve. Highly appreciate your help DATETIME="28-Sep-2013;20:09:08;" CONTROL="AB" echo "Myfile.txt;11671;7824.90;2822.48" The DATETIME will be inserted at the beginning and CONTROL will... (4 Replies)
Discussion started by: angshuman
4 Replies

3. UNIX for Dummies Questions & Answers

Values with common field in same line with awk

Hi all ! I almost did it but got a small problem. input: cars red cars blue cars green truck black Wanted: cars red-blue-green truck black Attempt: gawk 'BEGIN{FS="\t"}{a = a (a?"-":"")$2; $2=a; print $1 FS $2}' input But I also got the intermediate records... (2 Replies)
Discussion started by: beca123456
2 Replies

4. Shell Programming and Scripting

Save awk record field in bourne shell variable

Hello, I am trying to write a shell script that maintains the health of the passwd file. The goal is to check for duplicate usernames, UID's etc. I am able to find and sort out the UID and login names via awk (which I would like to use), but I can't figure out how to save the record field into a... (1 Reply)
Discussion started by: Learn4Life
1 Replies

5. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

6. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

7. Shell Programming and Scripting

Insert missing field using perl,sed,awk

sample file (comma as field separators) MessageFlow,1,BusIntBatchMgr,a OOBEvent,1,BusIntBatchMgr,a TaskEvents,1,,a MTTrace,1,,a MTWarning,,1,a MessageFlow,1,Batch,a OOBEvent,1,Batch,a TaskEvents,1,,a EAISAPIdocWizard,1,BusIntMgr,a EAISAPBAPIWizard,1,BusIntMgr,a... (3 Replies)
Discussion started by: vrclm
3 Replies

8. Shell Programming and Scripting

awk text record - prepend first field to all subsequent fields

Hello everyone, I've suddenly gotten very interested in sed and awk (and enjoying it quite a bit too) because of a large conversion project that we're working on. I'm currently stuck with a very inefficient process for processing text blocks. I'm sure someone here should be able to easily point out... (2 Replies)
Discussion started by: jameswatson3
2 Replies

9. UNIX for Advanced & Expert Users

command to insert a record at a particular loaction

Hi, Is there any command to insert a line in between two lines? My input data is as below: 1|ETG|63121387883|Alternate|Y 3|79.58|||GBP|| 4|001137001 4|0011372 5|1021701 5|1021901 1|ETG|63121387884|Alternate|Y 3|79.58|||GBP|| 4|001137001 5|1021702 5|1021802... (1 Reply)
Discussion started by: laxmi131
1 Replies

10. Shell Programming and Scripting

awk Problem while insert null field to file

Dear All, I have the following input file. I want to replace data with null values. I/P File: 9022334455|2008-12-06 06:10:21|2|Error@@@|esoo8erp| 9024334432|2008-12-06 08:40:59|6|Error@@@|6y2o8e6r| O/P File: 9022334455||2||esoo8erp| 9024334432||6||6y2o8e6r| ... (4 Replies)
Discussion started by: hanu_oracle
4 Replies
Login or Register to Ask a Question