LinuxQuestions.org
Register a domain and help support LQ
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 03-30-2014, 03:58 AM   #1
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Rep: Reputation: 1
A question on scripting on columns


Hi,

I have columns as shown below:

Code:
   
   -1.67575986   -0.93463490   -0.04002288   1       1  A
   -1.67054931    0.95747993    2.49038750   1       2  A
    0.02656904    1.89430566   -0.04319414   1       3  A
    1.67890904    0.93354409    2.49318191   1       4  A
    1.67546785   -0.94638837   -0.04121474   1       5  A
   -0.02566387   -1.89149303    2.48861212   2       6  B
    0.03003399   -3.72674086   -0.04296870   2       7  B
   -1.53161408   -4.96442186    2.65528205   2       8  B
   -3.50360605   -3.83134179    0.12373351   2       9  B
   -3.25051789   -1.88832668    2.48613150   3      10  C
   -5.07993818   -1.10253852    0.12410003   3      11  C
   -5.07980173    1.10605240    2.65639801   3      12  C
I want to add a constant to column three and put the result to the bottom of these columns so that I will have 24 rows at the end.

Could anyone let me know how to do it with some shell scripting?
 
Old 03-30-2014, 12:32 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by sryzdn View Post
Hi,
I have columns as shown below:
Code:
   
   -1.67575986   -0.93463490   -0.04002288   1       1  A
   -1.67054931    0.95747993    2.49038750   1       2  A
    0.02656904    1.89430566   -0.04319414   1       3  A
    1.67890904    0.93354409    2.49318191   1       4  A
    1.67546785   -0.94638837   -0.04121474   1       5  A
   -0.02566387   -1.89149303    2.48861212   2       6  B
    0.03003399   -3.72674086   -0.04296870   2       7  B
   -1.53161408   -4.96442186    2.65528205   2       8  B
   -3.50360605   -3.83134179    0.12373351   2       9  B
   -3.25051789   -1.88832668    2.48613150   3      10  C
   -5.07993818   -1.10253852    0.12410003   3      11  C
   -5.07980173    1.10605240    2.65639801   3      12  C
I want to add a constant to column three and put the result to the bottom of these columns so that I will have 24 rows at the end. Could anyone let me know how to do it with some shell scripting?
You first need to show us an example of what your desired output would be, and you also need to post what YOU have written and tried so far, and tell us where you're stuck. We will be glad to HELP you, but we're not going to write your scripts for you. There are MANY easily-found scripting tutorials (even one in my posting signature), that can help you get started.
 
Old 03-31-2014, 01:54 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Ditto ... we are here to help, not to do the work for you.
 
Old 03-31-2014, 02:53 AM   #4
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Original Poster
Rep: Reputation: 1
I apologize if my words were like I was asking you to work for me. I give up this question. I just did some manipulations with "cut" and "awk" and could get close to what I wanted.
Sorry again guys.
 
Old 03-31-2014, 04:24 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well if you show what you did then people may be able to help improve or offer alternatives
 
Old 03-31-2014, 10:03 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by sryzdn View Post
I apologize if my words were like I was asking you to work for me. I give up this question. I just did some manipulations with "cut" and "awk" and could get close to what I wanted.
Sorry again guys.
Well, all you posted what a few lines of input, and told us what you wanted, and asked for a script. You didn't post what output you wanted, or show us what you did/tried. Certainly SEEMS like you wanted us to write a script for you.

And since you did this, and you came to this site for help, why don't you share your answer, so that others can benefit? And as grail said, we might also be able to improve upon things, too.
 
Old 03-31-2014, 01:30 PM   #7
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by TB0ne View Post
Well, all you posted what a few lines of input, and told us what you wanted, and asked for a script. You didn't post what output you wanted, or show us what you did/tried. Certainly SEEMS like you wanted us to write a script for you.

And since you did this, and you came to this site for help, why don't you share your answer, so that others can benefit? And as grail said, we might also be able to improve upon things, too.
well, I really didn't know I am making you guys angry with me. Again I am sorry.Ok, here's what I did:

1. Here's what I wanted as a result:

Code:
   -1.67575986   -0.93463490   -0.04002288   1       1  A
   -1.67054931    0.95747993    2.49038750   1       2  A
    0.02656904    1.89430566   -0.04319414   1       3  A
    1.67890904    0.93354409    2.49318191   1       4  A
    1.67546785   -0.94638837   -0.04121474   1       5  A
   -0.02566387   -1.89149303    2.48861212   2       6  B
    0.03003399   -3.72674086   -0.04296870   2       7  B
   -1.53161408   -4.96442186    2.65528205   2       8  B
   -3.50360605   -3.83134179    0.12373351   2       9  B
   -3.25051789   -1.88832668    2.48613150   3      10  C
   -5.07993818   -1.10253852    0.12410003   3      11  C
   -5.07980173    1.10605240    2.65639801   3      12  C
   -1.67575986   -0.93463490    0.95997712   1      13  A
   -1.67054931    0.95747993    3.49038750   1      14  A
    0.02656904    1.89430566    0.95997712   1      15  A
    1.67890904    0.93354409    3.49318191   1      16  A
    1.67546785   -0.94638837    0.95997712   1      17  A
   -0.02566387   -1.89149303    3.49318191   2      18  B
    0.03003399   -3.72674086    0.95997712   2      19  B
   -1.53161408   -4.96442186    3.49318191   2      20  B
   -3.50360605   -3.83134179    1.12373351   2      21  B
   -3.25051789   -1.88832668    3.48613150   3      22  C
   -5.07993818   -1.10253852    1.12410003   3      23  C
   -5.07980173    1.10605240    3.65639801   3      24  C
2. First I took columns #1,2,3,4,5 and #6 out with awk to separate files:

Quote:
awk '{print $1}' input > output1
3. I added 1 to the numbers of the column #3 in file3:

Quote:
awk '{print ($1 +1)}' output3 > output33
This way I had the digits for column three ready.

4. I merged the columns #1,2,3,4 with paste command:

Quote:
paste output1 output2 output3 output4 > output-1
5. I appended the merged file to the first file using cat (I had removed columns 5 and 6 with cut):

Quote:
cat output-1 >> input
(columns #1 to #4 were ready for the result I wanted)

also appended column #6 using cat again in a separate file:

Quote:
cat output6 >> output6
6. for column #5, I just put the line numbers as below:

Quote:
awk '{ print NR" "$0 }' input
Then "cut" this column in a seperate file and merged everything again using "paste"


I should say that before I join this forum, I read the terms of asking questions and I did not mean to insult anyone. I'll go back and read the terms of asking questions again because I like this forum really very much and I want to keep asking questions.
 
Old 03-31-2014, 02:30 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Assuming I understood correctly, you could try:
Code:
cat input <(awk 'BEGIN{CONVFMT="%.8f";OFS="\t"}$3 += 1 && $5 += 12' input) > output
 
2 members found this post helpful.
  


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] Converting a file with Rows and Columns to just Columns mphillips67 Linux - Newbie 14 03-05-2014 11:31 AM
[SOLVED] bash suggestions to convert horizontal columns to vertical columns Guyverix Programming 14 01-24-2013 12:03 PM
SQL statements howto -- 3 columns input but 2 columns output fhleung Programming 3 11-29-2012 11:45 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 07:14 AM
Completely unrelated question: HTML->Tables->Columns Ctawp Linux - Software 6 08-07-2003 01:31 PM


All times are GMT -5. The time now is 07:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration