LinuxQuestions.org
Visit Jeremy's Blog.
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, 05: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, 06:55 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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, 01:29 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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 01:32 AM.
 
Old 06-15-2013, 03:06 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:22 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
Open Source Consulting | Domain Registration