AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK
# 8  
Old 08-16-2010
Quote:
Originally Posted by Ernst
Thank you for the reply. What if I had only the first four columns and wanted to add a fifth column (i.e. ens in output file). Note that the 5th column is the sum of column 3 and column 4. How would I write a program that inserts a fifth column which is the sum of the columns 3 and 4?
input_file
Code:
Caul	Preface	1er	2eme	
8	1	32.6	5.7	
8	2	17.7	1.3	
8	3	34.8	8.2	
9	1	65.8	0	
9	2	46.1	10.7	
9	3	12	2.9	
10	1	17	5	
10	2	36.9	0	
10	3	4.6	1.1

output
Code:
Caul	Preface	1er	2eme	ens
8	1	32.6	5.7	38.3
8	2	17.7	1.3	19
8	3	34.8	8.2	43
9	1	65.8	0	65.8
9	2	46.1	10.7	56.8
9	3	12	2.9	14.9
10	1	17	5	22
10	2	36.9	0	36.9
10	3	4.6	1.1	5.7

If you don't care of the format, using this code:

Code:
awk '{$5=(NR>1)?$3+$4:"ens"}1' urfile

If you care:

Code:
awk '{print $0,"\t", (NR>1)?$3+$4:"ens"}' urfile



---------- Post updated at 02:12 PM ---------- Previous update was at 02:05 PM ----------

Quote:
Originally Posted by Ernst
Thank you guys, I am half way through what I want to accomplish. My script so far is working fine. The only issue I have is that the output file for this script is:
Caul Preface
9 1
10 2
Instead, I want the output file to be:
Caul Preface
9 1
9 2
9 3
10 1
10 2
10 3

I want all references to either Caul 9 or Caul 10 to be displayed. That would be fantastic if I could get it to work this way.
Your new request are totally different.

If you just need find out that column 1 equal 9 or 10, you can use below code:

Code:
awk '$1==9||$1==10 {print $1,$2}' urfile

# 9  
Old 08-17-2010
You are right. I was not clear enough. I asked the wrong question from the beginning. What I am trying to do is as follows:
Here is my input file
Code:
Caul	Preface	1er	2eme
1	1	31.8	0
1	2	69.1	23.8
1	3	80.7	0
2	1	18.5	4.7
2	2	7.1	2
2	3	5.3	1.6
3	1	1.7	0.8
4	1	127.9	42.5
4	2	107	37.7
4	3	45.5	0
5	1	44.1	5
5	2	42.4	11.5
5	3	22.7	0
6	1	25.1	0
6	2	33.9	8
6	3	4.7	0.6
7	2	33.9	8
8	3	2.5	0.7
9	1	40.3	16.4
9	2	51.4	15.6
9	3	76.8	11
9	4	81.7	15
9	5	38	0
9	6	16.2	0
10	1	101.6	28.6
10	2	86.3	26
10	3	60.6	7.7
11	1	14.7	3.1
11	2	46.6	18.2
11	3	33.4	0
17	1	31.8	10.8
17	2	61.2	6
17	3	57.2	4.5
17	4	30.1	6.7
17	5	13.2	0
17	6	29	6.8
19	1	39.8	7.5
19	2	46.9	12.4
19	3	30.5	6.5
19	4	21.1	9
19	5	21.1	19
19	6	22.5	5.1
20	2	25	5
20	2	41.5	8
20	3	19.8	5.2
21	1	11.1	4.7
21	2	15	2.5
21	3	27.1	4.9
21	4	42.1	5.8
21	5	43.6	8.9
21	6	11.1	0

Output
Code:
Caul	Preface	1er	2eme	ens
1	1	31.8	0	31.8
1	2	69.1	23.8	45.3
1	3	80.7	0	80.7
4	1	127.9	42.5	85.4
4	2	107	37.7	69.3
4	3	45.5	0	45.5
5	1	44.1	5	39.1
5	2	42.4	11.5	30.9
5	3	22.7	0	22.7
6	1	25.1	0	25.1
6	2	33.9	8	25.9
6	3	4.7	0.6	4.1
9	1	40.3	16.4	23.9
9	2	51.4	15.6	35.8
9	3	76.8	11	65.8
9	4	81.7	15	66.7
9	5	38	0	38
9	6	16.2	0	16.2
11	1	14.7	3.1	11.6
11	2	46.6	18.2	28.4
11	3	33.4	0	33.4
17	1	31.8	10.8	21
17	2	61.2	6	55.2
17	3	57.2	4.5	52.7
17	4	30.1	6.7	23.4
17	5	13.2	0	13.2
17	6	29	6.8	22.2
21	1	11.1	4.7	6.4
21	2	15	2.5	12.5
21	3	27.1	4.9	22.2
21	4	42.1	5.8	36.3
21	5	43.6	8.9	34.7
21	6	11.1	0	11.1

First, I inserted a column called “ens” which is the diff between 1er et 2eme. Next, what I really want to do is ask the program to look at the column called “2eme” in my input file. Every time, there is a 0 in that column, the output should be all the references in the “Caul” and “Preface” columns. For example, there is a a “0” corresponding to Caul 1 / Preface 1 and Caul 1 / Preface 2, however, the output should be all three references for the Caul 1 column. The same goes for Caul 67. Although there are only three “0” under the column “ens”, the output should be all six references for Caul and Preface.

This is the script that I have been using:
Code:
awk '
        NR == 1 {
                printf( "Caul Preface 1er 2eme ens\n" ); # head
er
                next;
        }
        { printf( "%s %.1f\n", $t, $3 - $4 ); }
' testfile >> test.txt
awk '$3==$5{print $1,$2}' test.txt >> Final.txt

But I keep getting this result the following result:
Code:
Caul	Preface	1er	2eme	ens
1	1	31.8	0	31.8
1	3	80.7	0	80.7
4	3	45.5	0	45.5
5	3	22.7	0	22.7
6	1	25.1	0	25.1
9	5	38	0	38
9	6	16.2	0	16.2
11	3	33.4	0	33.4
17	5	13.2	0	13.2
21	6	11.1	0	11.1

Please let me know how can I get this result instead
Code:
Caul	Preface	1er	2eme	ens
1	1	31.8	0	31.8
1	2	69.1	23.8	45.3
1	3	80.7	0	80.7
4	1	127.9	42.5	85.4
4	2	107	37.7	69.3
4	3	45.5	0	45.5
5	1	44.1	5	39.1
5	2	42.4	11.5	30.9
5	3	22.7	0	22.7
6	1	25.1	0	25.1
6	2	33.9	8	25.9
6	3	4.7	0.6	4.1
9	1	40.3	16.4	23.9
9	2	51.4	15.6	35.8
9	3	76.8	11	65.8
9	4	81.7	15	66.7
9	5	38	0	38
9	6	16.2	0	16.2
11	1	14.7	3.1	11.6
11	2	46.6	18.2	28.4
11	3	33.4	0	33.4
17	1	31.8	10.8	21
17	2	61.2	6	55.2
17	3	57.2	4.5	52.7
17	4	30.1	6.7	23.4
17	5	13.2	0	13.2
17	6	29	6.8	22.2
21	1	11.1	4.7	6.4
21	2	15	2.5	12.5
21	3	27.1	4.9	22.2
21	4	42.1	5.8	36.3
21	5	43.6	8.9	34.7
21	6	11.1	0	11.1

Thanks!

Last edited by Scott; 08-17-2010 at 03:36 PM.. Reason: Code tags again
# 10  
Old 08-17-2010
Code:
awk '{print $0,"\t", (NR>1)?$3-$4:"ens"}' urfile

# 11  
Old 08-18-2010
I feel awful right now. I just spent one hour trying this thing and it is still not working.
Where exactly do I need to insert your code?
Code:
awk '{print $0,"\t", (NR>1)?$3-$4:"ens"}' urfile"

into my code:
Code:
awk '
        NR == 1 {
                printf( "Caul Preface 1er 2eme ens\n" ); # header
                next;
        }
        { printf( "%s %.1f\n", $t, $3 - $4 ); }
' testfile >> test.txt
awk '$3==$5{print $1,$2}' test.txt >> Final.txt


in order for me to get the desired output from the previously listed input.
Code:
Caul	Preface	1er	2eme	ens
1	1	31.8	0	31.8
1	2	69.1	23.8	45.3
1	3	80.7	0	80.7
4	1	127.9	42.5	85.4
4	2	107	37.7	69.3
4	3	45.5	0	45.5
5	1	44.1	5	39.1
5	2	42.4	11.5	30.9
5	3	22.7	0	22.7
6	1	25.1	0	25.1
6	2	33.9	8	25.9
6	3	4.7	0.6	4.1
9	1	40.3	16.4	23.9
9	2	51.4	15.6	35.8
9	3	76.8	11	65.8
9	4	81.7	15	66.7
9	5	38	0	38
9	6	16.2	0	16.2
11	1	14.7	3.1	11.6
11	2	46.6	18.2	28.4
11	3	33.4	0	33.4
17	1	31.8	10.8	21
17	2	61.2	6	55.2
17	3	57.2	4.5	52.7
17	4	30.1	6.7	23.4
17	5	13.2	0	13.2
17	6	29	6.8	22.2
21	1	11.1	4.7	6.4
21	2	15	2.5	12.5
21	3	27.1	4.9	22.2
21	4	42.1	5.8	36.3
21	5	43.6	8.9	34.7
21	6	11.1	0	11.1

I appreciate your patience.

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by Franklin52; 08-19-2010 at 06:52 AM..
# 12  
Old 08-18-2010
You can use my code directly.

Code:
$ cat input
Caul    Preface 1er     2eme
1       1       31.8    0
1       2       69.1    23.8
1       3       80.7    0
2       1       18.5    4.7
2       2       7.1     2
2       3       5.3     1.6
3       1       1.7     0.8
4       1       127.9   42.5
4       2       107     37.7
4       3       45.5    0

$ awk '{print $0,"\t", (NR>1)?$3-$4:"ens"}' input

Caul    Preface 1er     2eme     ens
1       1       31.8    0        31.8
1       2       69.1    23.8     45.3
1       3       80.7    0        80.7
2       1       18.5    4.7      13.8
2       2       7.1     2        5.1
2       3       5.3     1.6      3.7
3       1       1.7     0.8      0.9
4       1       127.9   42.5     85.4
4       2       107     37.7     69.3
4       3       45.5    0        45.5

# 13  
Old 08-19-2010
For some reason, I keep getting this message when I run it the way you said
Code:
awk '{print $0,"\t", (NR>1)?$3-$4:"ens"}' input

Error message
Code:
awk: syntax error near line 1
awk: illegal statement near line 1

Anyway, thanks for the help!

Last edited by Scott; 08-19-2010 at 11:00 AM..
# 14  
Old 08-19-2010
Quote:
Originally Posted by Ernst
For some reason, I keep getting this message when I run it the way you said
Code:
awk '{print $0,"\t", (NR>1)?$3-$4:"ens"}' input

Error message
Code:
awk: syntax error near line 1
awk: illegal statement near line 1

Anyway, thanks for the help!
Solaris?

Use nawk (or /usr/xpg4/bin/awk)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

2. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

3. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

6. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

7. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

8. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

9. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

10. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies
Login or Register to Ask a Question