Visit Our UNIX and Linux User Community


Adding Fields to the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding Fields to the file
# 22  
Old 12-20-2012
Quote:
Originally Posted by pamu
Hi Don,
... ... ...
Code:
awk -F '"' '
NR > 1{    for(i = 1; i <= NF; i += 2) gsub(/,/, "|", $i)
    print
}' OFS="" file | awk 'BEGIN{FS = OFS = "|"}
{    for(i = 1; i <= 20; i++)
        $i = $i != "" ? $i : $i = (i == 2 || i == 3) ? "ND" : " "
    print
}'

Thanks for above code.Smilie
That's brilliant piece of coding..(Specially highlighted part in red)

Thanks...Smilie
Thanks for the compliment, paum. But, after I looked at it again in red, I see that it is more complex than it needs to be. That red line should be replaced with:
Code:
        $i = $i != "" ? $i : (i == 2 || i == 3) ? "ND" : " "

(which drops an unneeded $i = ).

It will give the same results either way, but those 5 characters aren't needed the second time. Smilie
This User Gave Thanks to Don Cragun For This Post:
# 23  
Old 12-20-2012
Thanks Don, Pamu for all your efforts to make out the best effective pracrtise of coding. So i can use the final version by Don.. looks awesome and simple.Smilie... really brilliant piece of coding .Smilie thanks a lot!!!Smilie

---------- Post updated at 04:40 PM ---------- Previous update was at 03:37 PM ----------

Quote:
Originally Posted by Don Cragun
Thanks for the compliment, paum. But, after I looked at it again in red, I see that it is more complex than it needs to be. That red line should be replaced with:
Code:
        $i = $i != "" ? $i : (i == 2 || i == 3) ? "ND" : " "

(which drops an unneeded $i = ).

It will give the same results either way, but those 5 characters aren't needed the second time. Smilie
can anybody explain what the below line does?
Code:
$i = $i != "" ? $i : (i == 2 || i == 3) ? "ND" : " "

can i modify it to remove the condition for 2nd and 3rd field and default all additional fields to space apart from the existing one's and if an existing contains null it will be defaulted to null.

Last edited by Scrutinizer; 12-20-2012 at 08:19 AM.. Reason: code tags
# 24  
Old 12-20-2012
Quote:
Originally Posted by Arun Mishra
can anybody explain what the below line does?
Code:
$i = $i != "" ? $i : (i == 2 || i == 3) ? "ND" : " "

can i modify it to remove the condition for 2nd and 3rd field and default all additional fields to space apart from the existing one's and if an existing contains null it will be defaulted to null.
Code:
$i = $i != "" ? $i : (i == 2 || i == 3) ? "ND" : " "

Here it checks if $i not equals to null (""). If True then $i keep it as it is. If false then check i == 2 OR 3 if true then set $i="ND" and if false then set $i=" ".

If i got it your requirement right. You want all the values which is not present to null. and keep null value as it is.
try
Code:
$i = $i != "" ? $i:""

OR

Code:
awk -F '"' '
NR > 1{    for(i = 1; i <= NF; i += 2) gsub(/,/, "|", $i)
    print
}' OFS="" file | awk 'BEGIN{FS = OFS = "|"}
{    for(i = NF+1; i <= 20; i++)
        $i="";
    print
}'

# 25  
Old 12-20-2012
Actually i wanted all existing values plus the remaining fields to make it 20. and in all if any value is null it should be defaulted to space. i think the below should do it :

Code:
 $i = $i != "" ? $i : " "

Let me know if i make sense..
# 26  
Old 12-20-2012
Quote:
Originally Posted by Arun Mishra
Code:
 $i = $i != "" ? $i : " "

Let me know if i make sense..
Yes this will solve your purpose.. Smilie

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

Adding ' in particular fields

I have a file with 4 columns a|b|c|d I need to add single quotes around field 2 and 3 I need it to be like a|'b'|'c'|d (5 Replies)
Discussion started by: dsravanam
5 Replies

3. Shell Programming and Scripting

Awk: adding fields after matching $1

Dear AWK-experts! I did get stuck in the task of combining files after matching fields, so I'm still awkward with learning AWK. There are 2 files: one containing 3 columns with ID, coding status, and score for long noncoding RNAs: file1 (1.txt) (>5000 lines) ... (12 Replies)
Discussion started by: kben
12 Replies

4. Shell Programming and Scripting

Adding fields to a file

Hi All, I have a file(Pipe Delimited) where i need to add a blank field before the last field and a blank field after the last field. Please help. I have provided below the sample input records and desired output. Sample Input: A0010000|Abilene TX A 1|A0010957|Dallas TX|A0010998|West|US... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

5. Shell Programming and Scripting

Adding fields to file

Hi All, I have a file(Pipe Delimited) where i need to add a blank field before the last field and a blank field after the last field. Please help. I have provided below the sample input records and desired output. Code: Sample Input: A0010000|Abilene TX A 1|A0010957|Dallas... (0 Replies)
Discussion started by: Arun Mishra
0 Replies

6. Homework & Coursework Questions

regarding adding fields to DSR protocol in ns2.34

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: want to add field to route cache and packet of DSR routing protocol in ns2.34, add field, DSR package in ns2.34... (2 Replies)
Discussion started by: khubalkar
2 Replies

7. Programming

regarding adding fields to DSR protocol in ns2.34

hi i am student doing project in ns2.34. i hav to add field in route cache and packet of DSR routing protocol. which files hv to be changed...pl help me (1 Reply)
Discussion started by: khubalkar
1 Replies

8. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

9. Shell Programming and Scripting

Adding field to file and moving the last 2 fields

I have a file with 32 fields each separated by |. I need to add a file date exactly in the format "20100120" as the 32nd field moving the existing 32nd field to 33. so the field I added should be 32nd and the 33rd field is the last field before I added the file date. I know we can... (8 Replies)
Discussion started by: dsravan
8 Replies

10. Shell Programming and Scripting

Adding new fields to an existing layout

Hi Everybody, I have an layout file like below f1 1 char 10, f2 11 char 2, f3 13 char 1, lineend 14 char 1 Their I need to add a new field which would be like f5 char 3, f6 char 2 The o/p should be f1 1 char 10, f2 11 char 2, f3 13 char 1, f5 14 char 3, f6 17 char 2 (3 Replies)
Discussion started by: mr_manii
3 Replies

Featured Tech Videos