Insert field between two fields using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert field between two fields using awk or sed
# 1  
Old 09-28-2013
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

Code:
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 be inserted between field 1 and 2.

The output will be as below:

Code:
28-Sep-2013;20:09:08;Myfile.txt;AB;11671;7824.90;2822.48

I will be using variable to get date and time and the value AB.

Thanks
Angshuman
# 2  
Old 09-28-2013
Hi,
In shell:
Code:
DATETIME="28-Sep-2013;20:09:08;"
CONTROL="AB"
echo "Myfile.txt;11671;7824.90;2822.48" | ( IFS=\; read a b c d ; echo "$DATETIME$a;$CONTROL;$b;$c;$d")

Regards.
# 3  
Old 09-28-2013
Hi disedorgue,

Thank you for your reply. This is working fine in shell. However it is my mistake that I did not mention the expected end result completely. I have a file which contains some data with pipe as separator. I need to extract the data from the file and then insert the fields as mentioned above. I am using the following:

Code:
awk  -F'|' 'BEGIN{OFS=";";} {print $1,$11,$12,$16;}' $SUCCESSFILE

This is why I wanted to use awk or sed. May be something like following:

Code:
awk  -F'|' 'BEGIN{OFS=";";} {print $1,$11,$12,$16;}' $SUCCESSFILE  | awk/sed command to insert field

Thanks
Angshuman
# 4  
Old 09-28-2013
Ok, you can do it with awk as this example:
Code:
$ DATETIME="28-Sep-2013;20:09:08;"
$ CONTROL="AB"
$ echo "Myfile.txt|11671|7824.90|tttt|2822.48|xxx|yyy" | awk -F'|' -v d=$DATETIME -v c=$CONTROL 'BEGIN{OFS=";";} {print d$1,c,$2,$3,$5;}'
28-Sep-2013;20:09:08;Myfile.txt;AB;11671;7824.90;2822.48

Beware: you must concat variable d and $1 because end d is already ";"

Otherwise, technically, it's possible with shell solution but most slow:
Example:
Code:
$ echo "Myfile.txt|11671|7824.90|tttt|2822.48|xxx|yyy" | ( IFS=\| read a b c d e f g; echo "$DATETIME$a;$CONTROL;$b;$c;$e")
28-Sep-2013;20:09:08;Myfile.txt;AB;11671;7824.90;2822.48

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 09-29-2013
You don't need two awk cmds connected through a pipe; do it in one go:
Code:
awk -F'|' -v DT=$DATETIME -v CTL=$CONTROL 'BEGIN{OFS=";"} {print DT,$1,CTL,$11,$12,$16;}' $SUCCESSFILE

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to add field heards to specific fields

I have tab delimited input that prints out in the format below: I am trying to add field headers to $5 and $6. Not sure if sed is the best tool but my attempt is below. Thank you :). $5 = REF $6 = ALT file ID CHR START STOP 123 1 100 200 A ... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Insert field values in a record using awk command

Hi Friends, Below is my input file with "|" (pipe) as filed delimiter: My Input File: HDR|F1|F2||||F6|F7 I want to inser values in the record for field 4 and field 5. Expected output 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... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

3. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

4. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies

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

6. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

7. Shell Programming and Scripting

how to parse with awk (using different fields), then group by a field?

When parsing multiple fields in a file using AWK, how do you group by one of the fields and parse by delimiters? to clarify If a file had tom | 223-2222-4444 , randofield ivan | 123-2422-4444 , random filed ... | and , are the delimiters ... How would you group by the social security... (4 Replies)
Discussion started by: Josef_Stalin
4 Replies

8. Shell Programming and Scripting

AWK : Add Fields of lines with matching field

Dear All, I would like to add values of a field, if the lines match in a certain field. Then I would like to divide the sum though the number of lines that have a matched field. This is the Input: Input: Test1 5 Test1 10 Test2 2 Test2 5 Test2 13 Test3 4 Output: Test1 7.5 Test1 7.5... (6 Replies)
Discussion started by: DerSeb
6 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 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