1

I am working on this awk tutorial here.

$ head file
Name,Number,Letter
Unix,10,A
Linux,30,B
Solaris,40,C
Fedora,20,D
Ubuntu,50,E


this command inserts a new column after the last column. But i do not want to add to the header, how do I do this? I will also, after this, want to add a header name after.

$ awk -F, '{$(NF+1)=++i;}1' OFS=, file
Name,Number,Letter,1
Unix,10,A ,2
Linux,30,B ,3
Solaris,40,C ,4
Fedora,20,D ,5
Ubuntu,50,E,6
5
  • Your output shows that your input is sloppy w.r.t trailing blanks. Commented Oct 1, 2014 at 23:33
  • tks,how do i fix that? Ah i see you mean the space after the Letter column. Commented Oct 2, 2014 at 0:32
  • It depends on your choice in editors. I have various implementations of a program stb (strip trailing blanks) which I use to ensure no trailing blanks. In vim, you could use :g/[ ^I][ ^I]*$/s/// (where the ^I is a tab) to eliminate trailing blanks and tabs. At the office, vim is set up to display ghastly yellow when there's a trailing blank or tab on a line in a source file. Etc. Basically, trailing blanks and tabs are untidy; avoid them when you can. Commented Oct 2, 2014 at 0:55
  • That i variable is redundant since NR provides a count of lines. Setting FS and OFS to the same value in 2 different places is a terrible idea and that's not a reason to set vars in the file list so I'd treat anything else in that tutorial with a lot of suspicion. Use awk 'BEGIN{FS=OFS=","} .. ' file instead of awk -F, '...' OFS="," file and get the book Effective Awk Programming, Third Edition, by Arnold Robbins. Commented Oct 2, 2014 at 2:02
  • tks ed, found that book here if only I had the time to read all that :) Commented Oct 2, 2014 at 21:07

2 Answers 2

4

You can skip the first line with:

awk -F, 'NR>1{$(NF+1)=++i;}1' OFS=, file

If there can be blanks in the file, then you can use regular expression to mask them out:

awk -F, '!/^$/ && NR>1{$(NF+1)=++i;}1' OFS=, file

To add a new header, you can do:

awk 'NR==1{$0=$0",New_header";} NR>1{$(NF+1)=++i;}1' OFS=, file

Like @jaypal said, it could be simply:

awk -F, 'NR==1{$0=$0",New_header";} NF && NR>1{$(NF+1)=++i;}1' OFS=, file
Sign up to request clarification or add additional context in comments.

5 Comments

+1: You can use NF to ignore blank lines. awk -F, 'NF && NR>1{$(NF+1)=++i;}1' OFS=, file.
How do I overwrite the existing file, or do i just do a > to the same filename?
@HattrickNZ No, that'll destroy the file! If you have latest awk (4.1 or later) then you can use the -i option to do in-pace modification. Otherwise, use a temp file: awk -F, '{...}' file > tmpfile && mv tmpfile file
with 4.1 how do i do it? is it awk -Fi, ... file? when in command line it works, but then when I cat the file it has not changed
@HattrickNZ If your awk has -i option then : awk -i -F, '{...}' file Read GNU awk manual here.
2
$ awk 'BEGIN{FS=OFS=","} {print $0, (NR>1?NR-1:"new header")}' file
Name,Number,Letter,new header
Unix,10,A,1
Linux,30,B,2
Solaris,40,C,3
Fedora,20,D,4
Ubuntu,50,E,5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.