How to add missing value?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add missing value?
# 1  
Old 07-17-2015
How to add missing value?

Hi Gurus,

I have a txt file. some record missed 2nd colum value, I need put default value into the file.
ex: below file, first 4 records, missed one column (only 4 columns. it should be 5)

Code:
 
$cat missedfield
aaa,bbb,ccc,fff
aaa,bbb,ccc,ddd
111,222,333,444
111,222,333,444
111,222,333,444,555

I tried awk command as below:
the problem is I want to if NF<5 and $NF>2, $2='xxx'(default value), then assign $2 to $3 and so on. but below code the column value is minused.
Code:
 
awk -F"," '{if (NF< 5 && $NF>2) {$2="XXX"}{ $NF=$NF-1} {print $0}}' missedfield

Can anybody help me this?

Thanks in advance.
# 2  
Old 07-17-2015
That may be the most correct way but a quick way to get the same output is to just append the new value and the field separator inside of $1.

Code:
mute@tiny:~$ awk 'NF<5{$1=$1 FS "XXX"}1' FS=, OFS=, missedfield
aaa,XXX,bbb,ccc,fff
aaa,XXX,bbb,ccc,ddd
111,XXX,222,333,444
111,XXX,222,333,444
111,222,333,444,555

or iterating over the fields you'd have to first increase the field count and starting from the end work your way backwards moving them until $2 is now free to assign:

Code:
mute@tiny:~$ awk 'NF<5{NF++;for (i=NF;i>1;i--)$i=$(i-1);$2="XXX"}1' FS=, OFS=, missedfield
aaa,XXX,bbb,ccc,fff
aaa,XXX,bbb,ccc,ddd
111,XXX,222,333,444
111,XXX,222,333,444
111,222,333,444,555

the 1 at the end is shorthand for { print $0 } since the default action in awk is to print and 1 is always true.

Last edited by neutronscott; 07-17-2015 at 09:48 AM..
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 07-17-2015
Hello Ken6503,

You haven't shown us the expected output, so I am not sure either we need to put both conditions of having number of fields lesser than 5 + $NF(last field's) value should be grater than 2. But by seeing your expression I have made following.
Code:
 awk -F, '{if(NF<5 && $NF+0 > 2){$2="XXX" OFS $2;print;next}} 1' OFS=,  Input_file

Output will be as follows.
Code:
aaa,bbb,ccc,fff
aaa,bbb,ccc,ddd
111,XXX,222,333,444
111,XXX,222,333,444
111,222,333,444,555

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-17-2015 at 10:07 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 07-17-2015
Thanks R.Singh and neutronscott.

As always. the code you provided works perfect.

Thank you very much.

---------- Post updated at 01:16 PM ---------- Previous update was at 09:16 AM ----------

Below is file and result with R.Singh and neutronscott's code.

Please note: for R.Singh code, I don't fully understand
Code:
$NF+0 > 2

why here need $NF+0, I removed +0. the result are exactly I need.

Code:

Code:
$cat missedfield 
aaa,bbb,ccc
aaa,bbb,ccc,ddd
111,222,333,444
111,222,333,444
111,222,333,444,555
$nawk -F, '{if(NF<5 && $NF > 2){$2="XXX" OFS $2;print;next}} 1' OFS=,   missedfield
aaa,XXX,bbb,ccc
aaa,XXX,bbb,ccc,ddd
111,XXX,222,333,444
111,XXX,222,333,444
111,222,333,444,555
$nawk 'NF<5{$1=$1 FS "XXX"}1' FS=, OFS=, missedfield
aaa,XXX,bbb,ccc
aaa,XXX,bbb,ccc,ddd
111,XXX,222,333,444
111,XXX,222,333,444
111,222,333,444,555

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add missing date and time in a bash script?

Hi Again, I have a file that contains date and time for the past 2 hours. What i need is add missing date and time in a file. INPUT 2016-01-13 01:33 10 2016-01-13 01:31 10 2016-01-13 01:30 10 2016-01-13 01:29 10 2016-01-13 01:28 10 2016-01-13 01:27 10 2016-01-13 01:26 10 2016-01-13... (14 Replies)
Discussion started by: ernesto
14 Replies

2. Shell Programming and Scripting

Add word for a missing column

Hi team... I need some help/advise on adding a word for a missing column. I have a file as below. Rep N1 Forever Limited 2015 Rep N2 Limited 2016 since 2nd content is 3 lines missing one line so i need to make this like Rep N1 Forever (3 Replies)
Discussion started by: newbee5
3 Replies

3. Red Hat

Yum - resolving missing dependencies that are not missing

I am trying to install VirtualBox on RHEL 5 but I need the 32 bit version for 32 bit Windows. When I run yum I get the following: sudo yum localinstall /auto/spvtg-it/spvss-migration/Software/VirtualBox-4.3-4.3.2_90405_el6-1.i686.rpm Loaded plugins: fastestmirror Setting up Local Package... (13 Replies)
Discussion started by: gw1500se
13 Replies

4. SuSE

How to resolve missing missing dependencies with opensuse 11.3 and 12.3?

Hello, This is a programming question as well as a suse question, so let me know if you think I should post this in programming. I have an application that I compiled under opensuse 12.2 using g77-3.3/g++3.3. The program compiles and runs just fine. I gave the application to a colleague who... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

5. Solaris

[solved]Config/enable_mapping missing, how to add?

Issue resolved by upgrading from solaris 11 to solaris 11.1 I would like to enable network mapping. While using instructions from: https://blogs.oracle.com/VDIpier/entry/solaris_11_changing_the_hostname To change my hostname I noticed I am missing the enable mapping bool. What it should... (0 Replies)
Discussion started by: taltamir
0 Replies

6. UNIX Desktop Questions & Answers

merging files and add missing rows

hello all, I have files that have a specific way for naming the first column they are make of five names in Pattern of 3 Y = (no case sensitive) so the files are names $Y-$Y-$Y or $X-$Y-$Z depending how we look they only exist of the pattern exist now I want to create a file from them that... (9 Replies)
Discussion started by: A-V
9 Replies

7. Shell Programming and Scripting

Add missing linefeeds between formfeeds in reports

I need to take a report text file that is output from vendor software and there are some pages which have less then the normal amount of lines. I need to add these missing line feeds as there is a merge program that reads this file in fixed character and line mode template to output the final PDF... (6 Replies)
Discussion started by: rnygren
6 Replies

8. Shell Programming and Scripting

Add missing times

Not sure about the title if someone has a better name for it please lemme know and I will edit the title. I have several (10+ files) which look something like: File 1: 12/28/2009 04:0 8 12/28/2009 04:4 4 12/28/2009 05:0 4 . . . File 2: 12/28/2009 04:1 7 12/28/2009 04:2 3... (2 Replies)
Discussion started by: jstrangfeld
2 Replies

9. Shell Programming and Scripting

i need to add missing delimiters...

ladies, gents.. say i have a file that should have 10 fields... (9 delimiters) some records have 10 fields, some have 5 some have 8, etc.. nothing consistent, but i need my file to have 9 delimiters on each line, even if its null fields.. how can i go line by line and add the correct... (2 Replies)
Discussion started by: obarrett
2 Replies
Login or Register to Ask a Question