awk-adding a column to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk-adding a column to a file
# 1  
Old 08-27-2009
awk-adding a column to a file

Hello Friends,

i used awk to sum up total size of files under a directory (with the help of examples, threads here).

Code:
ls -l | awk '/^-/ {total += $5} END {printf "%15.0f\n",total}' >> total.txt

After each execution of the script total result is appended into a text file:
Code:
7010
7794
8890

i would like to add the execution date of the script into first column and then the results to the second column, but i couldnt do it after some tries. Do you know how to modify the script to add the date (date format is not important)?

expected:
Code:
Aug 27 7010
Sep 27 7794
Oct 27 8890



thanks by advance
# 2  
Old 08-27-2009
not sure what you meant by 'execution date of the script', but...
Code:
ls -l | awk '/^-/ {a[$6 OFS $7]+=$5} END {for(i in a) printf "%s %15.0f\n",i,a[i]}'

OR
Code:
ls -l | awk '/^-/ {a[$6 OFS sprintf("%2d",$7)]+=$5} END {for(i in a) printf "%s %15d\n",i,a[i]}'


Last edited by vgersh99; 08-27-2009 at 03:32 PM..
# 3  
Old 08-27-2009
Code:
ls -l | awk '/^-/ {total +=$5} END {"date \"+%b %_d\""|getline d;printf "%s %15.0f\n",d,total}' >> total.txt

# 4  
Old 08-27-2009
Hi Vgersh,

with "execution date of script" i meant the date when crontab run the script (monthly). Sorry for my bad explanation.. Let me try the scripts, thnx all
# 5  
Old 08-27-2009
Code:
#!/bin/ksh
ls -l | awk '/^-/ {total+=$5} END {printf "%s %15d\n",d, total}' d=$(date '+%b %d')

# 6  
Old 08-27-2009
Vgersh i tried it and AWK tries to open 27 of "Aug 27" and give error like below:

Code:
#!/bin/ksh -x
ls -l | awk '/^-/ {total+=$5} END {printf "%s %15d\n",d, total}' d=$(date '+%b %d')

Code:
server1{root}>./vgersh.sh
+ ls -l
+ date +%b %d
+ awk /^-/ {total+=$5} END {printf "%s %15d\n",d,total} d=Aug 27
awk: can't open 27

and besides i tried Danmero's script, it resulted with this (all script is 1 line so how can i trace it? ):

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

# 7  
Old 08-27-2009
sorry....
Code:
#!/bin/ksh
ls -l | awk '/^-/ {total+=$5} END {printf "%s %15d\n",d, total}' d="$(date '+%b %d')"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Des/awk for change format and adding integers in a column of data?

Greetings! I need a quick way to change the format in a table of data Here is an example of the input: 10 72 Value=177 VDB=0.0245 Value4=0,0,171,0 10 274 Value=238 VDB=0.0433 Value4=29,0,205,0 10 312 Value=222 VDB=0.0384 Value4=8,0,190,19 10 540 Value=405 VDB=0.0391 Value4=13,30,153,195... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

2. UNIX for Dummies Questions & Answers

awk adding counts together from column

Hello Im new treat me nicely, I have a headache :) I have a script that seemed to work now it doesnt anyway, the last part is adding counts of unique items in a csv file eg 05492U34 38 05492U34 47 two columns, (many different values like this in file) i want... (7 Replies)
Discussion started by: aniquebmx
7 Replies

3. Shell Programming and Scripting

Adding a specified value to a specified column - awk?

Hi everyone! I sometimes need to do some simple arithmetics, like adding a number to a certain column of a file. So I wrote a small function in the .bashrc file, which looks like this shifter() { COL=$1 VAL=$2 FILE=$3 cp $FILE $FILE.shifted awk 'NF==4 {$(( $COL )) = $(( $COL ))... (6 Replies)
Discussion started by: radudownload
6 Replies

4. Shell Programming and Scripting

Adding a column to a file

Hello all, I need to add a coloumn at the 5th Position of a file, Can this be done using awk or sed. Sample Input 1008,300186,R,2009,0,2,2,3,2,0,1,1,1,1,2,1,1,0,2,1,1,0,2,2,2,2,0,0,0,0,1,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,3,2,3,1,1,1, Ouput: ... (4 Replies)
Discussion started by: Sri3001
4 Replies

5. Linux

Adding a prefix to a column using awk/sed commands

Hello, I am a newbie to linux and struggling to find a better way to append a column in a text file. Here is the file i want to modify: It has 8 columns (and thousands of rows). I want to append the first column by adding "chr" infront of the numbers. Some rows have a string in the first... (4 Replies)
Discussion started by: bjorngill
4 Replies

6. Shell Programming and Scripting

Adding column using awk

Hello everyone, I have a file with the following structure: abc xyz 111 222 agf hjhf 787 799 tht yah 878 898 ... ... ... ... ... ... ... ... ... ... ... ... I want to add a column (with a fixed value of 1000) at the end such that it becomes: abc xyz 111 222 1000 agf hjhf 787... (5 Replies)
Discussion started by: ad23
5 Replies

7. UNIX for Dummies Questions & Answers

Adding a column with the row number using awk

Is there anyway to use awk to add a first column to my data that automatically goes from 1 to n , where n is the numbers of my rows?:confused: (4 Replies)
Discussion started by: cosmologist
4 Replies

8. Shell Programming and Scripting

sed/awk-adding numeric to a column

I have a txt file as follows Code: Oct 1 file1 4144 Oct 1 file23 5170 Oct 2 file5 3434 Oct 21 file56 2343 I need to add a new column by marking the right log file from current directory. For example populate like this. Please not in the second columt for "1" it has... (2 Replies)
Discussion started by: gubbu
2 Replies

9. Shell Programming and Scripting

problem while adding column values in awk

Hi, I have a file "input.txt" with the following content : 5312,0,,,1,8,141.2,20090727 3714,0,,,1,8,285.87,20090727 5426,0,,,1,8,3.9,20090727 3871,0,,,1,8,30.4,20090727 9071,0,,,1,8,146.2,20090727 5141,0,,,1,8,2.8,20090727 0460,0,,,1,8,-0.1,20090727 7918,0,,,1,8,-0.1,20090727... (3 Replies)
Discussion started by: valokv
3 Replies

10. Shell Programming and Scripting

FILE:Adding new column

Is it possible to add a new column in a file by matching a key element in second file ? File 1 2 a 3 b 4 d 5 g 6 j 7 m File 2 4 hjjjj 5 aaa 6 sasa 7 dsds 2 dsdf 3 fdsfg (2 Replies)
Discussion started by: sandeep_hi
2 Replies
Login or Register to Ask a Question