LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2013, 01:41 PM   #1
calebante
LQ Newbie
 
Registered: May 2013
Posts: 3

Rep: Reputation: Disabled
Smile bash - Copying two columns and doing an operation to the third


I have an executable "a.out" that does an operation with a number, for example duplicates it.

$./a.out 12
24

and I have a test file "file.dat" like this:

11 12 13
21 22 23
31 32 33
41 42 43

I'd like to obtain a file identical to the original, except by the 3rd column, in which every value has been replaced by its double, found by a.out:

11 12 26
21 22 46
31 32 66
41 42 86

I'm new in shell scripting and I'm totally confused. I would appreciate a lot any help!
 
Old 05-26-2013, 12:09 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well as you have mentioned scripting I will go on a limb (as you don't say) and go with bash. To this end you will need the following:

1. A loop to read in the original values

2. Call your a.out on the last value

3. Redirect the output to a new file

If you do not have a reference, try http://tldp.org/LDP/abs/html/

Let us know how you get on
 
Old 05-26-2013, 07:46 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
This kind of operation is trivial to do in awk, by the way, without even the need for any external program. I just accomplished it myself with an expression that's a whole 8 characters long.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/series/awk-one-liners-explained
 
Old 05-26-2013, 09:18 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... I can go 5 if not using a.out and assuming we do not include the quotes
 
Old 05-26-2013, 10:46 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
I can go 5 if not using a.out and assuming we do not include the quotes
Please post this code.

Daniel B. Martin
 
Old 05-26-2013, 10:50 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk '$3*=2' file
 
Old 05-26-2013, 10:57 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
What if you have a 0 in column 3? I think 8 characters is the minimum for a correct solution.
 
Old 05-26-2013, 11:08 AM   #8
calebante
LQ Newbie
 
Registered: May 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Maybe I was not clear enough - awk can't help

I know that

awk '{print $1,$2,2*$3}' test.dat

would work if I just wanted to double the 3rd column, but this is not what my a.out does. I just gave this duplication thing as an example...

For each line, I have to write the 1st value, write the 2nd value, and write the value returned by a.out on the 3rd value!
 
Old 05-26-2013, 11:25 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Have a look at this:
Code:
awk '{ printf("%s %s ",$1,$2) ; system("./a.out "$3) }' test.dat
If a.out is not in the directory you execute this command from, prepend the full path to it.

Last edited by druuna; 05-26-2013 at 11:50 AM.
 
1 members found this post helpful.
Old 05-26-2013, 12:10 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I concede to the zero
 
Old 05-26-2013, 01:24 PM   #11
calebante
LQ Newbie
 
Registered: May 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yes, it works!

Great, druuna! Thank you all of you.

Last edited by calebante; 05-26-2013 at 03:29 PM.
 
Old 05-28-2013, 10:30 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
To do it in the shell, BTW, you just have to split the data properly as you read it.

Code:
while read -r -a fields ; do

    fields[2]=$( mycommand "${fields[2]}" )
    echo "${fields[*]}"

done <infile.txt
This example assumes you're using a shell like bash with array support, but it could easily be converted into posix-compliant code as well.
 
  


Reply



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] bash suggestions to convert horizontal columns to vertical columns Guyverix Programming 14 01-24-2013 11:03 AM
[SOLVED] transform rows into columns in bash xeon123 Linux - Newbie 4 07-30-2011 07:19 AM
Parsing columns in bash seb357 Linux - Newbie 9 09-28-2009 02:40 PM
sorting columns in bash twistadias Linux - Newbie 8 08-25-2008 12:22 AM
bash thinks term is 80 columns? JurgyMan Linux - General 4 06-13-2006 09:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 AM.

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