LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

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


cat test.csv

t1,e1,l1,r1,t2,s1
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, 07:55 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Works for me.
Can you check if you've got any invisible ctrl chars in the real file eg use od cmd http://linux.die.net/man/1/od or in vi/vim use ':set list'.
Also show version of awk 'awk --version'
Code:
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,,
HelloWorld1,HelloWorld2,10,,,,,
Please use code tags for posting code/data https://www.linuxquestions.org/quest...do=bbcode#code
 
Old 06-15-2013, 02:29 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 805

Rep: Reputation: 186Reputation: 186
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

OK

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

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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¨
 
  


Reply


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 01:33 AM
creating columns with awk? verse123 Linux - Newbie 6 11-09-2011 02:50 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 07:14 AM
extracting columns with awk gav251 Programming 7 03-10-2011 09:40 AM
Using awk to switch columns bioinformatics_guy Linux - Newbie 3 10-30-2008 10:50 AM


All times are GMT -5. The time now is 12:16 PM.

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