Make a new field with a word on it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Make a new field with a word on it
# 1  
Old 05-07-2012
Make a new field with a word on it

i have a file contains as below:
[file]:
Code:
12 32 qw
21 44 qq
###
11 90 er
88 23 sa
###

i want to add new field with constant string "madd" between 2nd field and 3rd field of each record, so the desired output will be:
[out]:
Code:
12 32 madda qw
21 44 madda qq
###
11 90 madda er
88 23 madda sa
###

# 2  
Old 05-07-2012
Code:
awk 'NF-1{$3="madda " $3}1' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 05-07-2012
Try This,
Code:
nawk '{if($0!~/###/){$3="madd " $3}}1' txt

Regards,
Indu.
This User Gave Thanks to Indumathy For This Post:
# 4  
Old 05-07-2012
Code:
 
$ nawk '!/^\#/{$2=$2" abcd"}1' test.txt
12 32 abcd qw
21 44 abcd qq
###
11 90 abcd er
88 23 abcd sa
###

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 05-07-2012
@all: what about empty lines?
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-07-2012
@Scrutinizer

The OP didn't provide empty lines in his/her example file Smilie
This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 05-07-2012
Now, that is technically true Smilie
Code:
awk 'NF>2{$3="madda " $3}1' infile


Last edited by Scrutinizer; 05-07-2012 at 05:48 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Update a specific field in file with Variable value based on other Key Word

I have an input file with A=xyz B=pqr I would want the value in Second Field (xyz or pqr) updated with a value present in Shell Variable based on the value passed in the first field. (A or B ) while read line do NEW_VALUE = `some functionality done on $line` If $line=First Field-... (1 Reply)
Discussion started by: infernalhell
1 Replies

2. Shell Programming and Scripting

Find repeated word and take sum of the second field to it ,for all the repeated words in awk

Hi below is the input file, i need to find repeated words and sum up the values of it which is second field from the repeated work.Im trying but getting no where close to it.Kindly give me a hint on how to go about it Input fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30... (11 Replies)
Discussion started by: 100bees
11 Replies

3. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

4. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

5. Shell Programming and Scripting

expr Field 1 + Field 2 Since when does 2 positives make a negative

Please help. I am adding 2 fields which are both positive and the result is negative. What am I missing? FileEHash=`expr ${Field1} + ${Field2}` >/dev/null Debug Results + expr 02100002 + 2009701914 EntryHash=2011801916 + expr 02100002 + 2146202044 FileEHash=-2146665250 How do... (2 Replies)
Discussion started by: ski
2 Replies

6. UNIX for Dummies Questions & Answers

Paste a word in the third field position of a file

Hi All, I have a file like this, 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 The above line is a comma separted data file. I want to modify the third field to The final data file should be like 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 ... (1 Reply)
Discussion started by: girish.raos
1 Replies

7. Shell Programming and Scripting

Trim leading zeros to make field 6 characters long

Hi all- I've got a file that will have multiple columns. In one column there will be a string that is 10 digits in length, but I need to trim the first four zeros to make it 6 characters? example: 0000001234 0000123456 0000234566 0000000321 output: 001234 123456 234566 000321 (5 Replies)
Discussion started by: Cailet
5 Replies

8. Shell Programming and Scripting

How to select or make reference to, part of a field

For a field format such as AAL1001_MD82, how do I select(and use in if statement) only the last four elements( in this case MD82) or the first three elements (in this case AAL)? For instance, how do I do the following - if first three elements of $x == yyy, then ... (5 Replies)
Discussion started by: akshaykr2
5 Replies

9. Shell Programming and Scripting

using awk make an field as 5 digit and display

using awk convert 3 rd fileld of file as 5 digit and then display changed file. like 1 2 23445 3452 3343 3 5 6 6 ================ o/p:- 1 2 23445 3452 03343 3 5 00006 6 (1 Reply)
Discussion started by: RahulJoshi
1 Replies
Login or Register to Ask a Question