Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-14-2013, 05:00 PM   #1
LQ Newbie
Registered: Jun 2013
Posts: 3

Rep: Reputation: Disabled
awk issue with adding formula columns?

cat test.csv

137,597,LG1,20000,Group 1-1,true
1370,8,JBC,2800000,Group 1-1,false

awk 'BEGIN {FS=OFS=","} {if (NR!=1) print $1="HelloWorld1",$2="HelloWorld2",$3=((($4/100)+10)),$4,$5,$6,$7,$8}' test.csv
,,lloWorld1,HelloWorld2,210,20000,Group 1-1,true
,,lloWorld1,HelloWorld2,28010,2800000,Group 1-1,false

I am missing the first two letters

I want the output as below:

HelloWorld1,HelloWorld2,210,20000,Group 1-1,true
HelloWorld1,HelloWorld2,28010,2800000,Group 1-1,false

Could you please help.

Thanks a lot
Old 06-14-2013, 06:55 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,372

Rep: Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382
Works for me.
Can you check if you've got any invisible ctrl chars in the real file eg use od cmd or in vi/vim use ':set list'.
Also show version of awk 'awk --version'
awk --version
GNU Awk 3.1.7

 awk 'BEGIN {FS=OFS=","} {if (NR!=1) print $1="HelloWorld1",$2="HelloWorld2",$3=((($4/100)+10)),$4,$5,$6,$7,$8}' t.t
HelloWorld1,HelloWorld2,210,20000,Group 1-1,true,,
HelloWorld1,HelloWorld2,28010,2800000,Group 1-1,false,,
Please use code tags for posting code/data
Old 06-15-2013, 01:29 AM   #3
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 837

Rep: Reputation: 201Reputation: 201Reputation: 201
Why didn't you just use:
awk 'BEGIN {FS=OFS=","} {if (NR!=1) print "HelloWorld1","HelloWorld2",(($4/100)+10),$4,$5,$6,$7,$8}' test.csv


Last edited by AnanthaP; 06-15-2013 at 01:32 AM.
Old 06-15-2013, 03:06 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
There are a few things to note here:

1. AnanthaP is quite correct that if you do not use the first 2 fields, then no need to set them.

2. chrism01ś point about the file probably being created in Windows would mean erroneous items at the end of the line causing issues. This one can be overcome by either, running dos2unix over the file
or simply setting the record separator (RS) to use the windows setting of ¨\n\r¨

3. You have happily used the process OFS=FS=¨,¨, so why not use $1=$2=¨HelloWorld¨


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] help on awk contain between two columns phpshell Programming 10 04-17-2013 12:33 AM
creating columns with awk? verse123 Linux - Newbie 6 11-09-2011 01:50 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM
extracting columns with awk gav251 Programming 7 03-10-2011 08:40 AM
Using awk to switch columns bioinformatics_guy Linux - Newbie 3 10-30-2008 09:50 AM

All times are GMT -5. The time now is 07:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration