Use bash command on awk field and output the result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use bash command on awk field and output the result
# 1  
Old 01-07-2016
Use bash command on awk field and output the result

Hello, I want to run a field from an awk command through a command in bash.

For example my input file is

Code:
1,2,3
20,30,40
60,70,80

I want tot run $2 thought the command
Code:
date +%d/%m/%y -d"01/01/15 + $2 days -1 day"

and get the output

Code:
1,02/01/15,3
20,30/01/15,40
60,11/03/15,80

I'm stumped. Tried using
Code:
awk -v var=......

but can't seem to get it to work.
Any hints?

Thanks

Last edited by Scrutinizer; 01-07-2016 at 04:27 PM.. Reason: CODE tags
# 2  
Old 01-07-2016
Try using a while loop, for example something like (not tested):

Code:
while IFS=, read a b c
do
  date +%d/%m/%y -d"01/01/15 + $b days -1 day"
done < inputfile

# 3  
Old 01-07-2016
If it HAS to be awk, try
Code:
 awk '{system("date +\"" $1 ",%d/%m/%y," $3 "\" -d\"01/01/15 + " $2 "days -1 day\"")}' FS=, file
1,02/01/15,3
20,30/01/15,40
60,11/03/15,80

or, slightly shorter,
Code:
awk '{system("date +\"" $1 ",%d/%m/%y," $3 "\" -d\"01/01/15 + " $2 - 1 "days\"")}' FS=, file
1,02/01/15,3
20,30/01/15,40
60,11/03/15,80

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a multi-line shell command output and execute logic based on result

The following is a multi-line shell command example: $cargo build Compiling prawn v0.1.0 (/Users/ag/rust/prawn) error: failed to resolve: could not find `setup_panix` in `human_panic` --> src/main.rs:14:22 | 14 | human_panic::setup_panix!(); | ... (2 Replies)
Discussion started by: yogi
2 Replies

2. Shell Programming and Scripting

awk command gives incorrect result?

Hi All, I am looking to filter out filesystems which are greter than a specific value. I use the command df -h | awk '$4 >=70.00 {print $4,$5}' But this results out as below, which also gives for lower values. 9% /u01 86% /home 8% /u01/data 82% /install 70% /u01/app Looks... (3 Replies)
Discussion started by: jjoy
3 Replies

3. UNIX for Dummies Questions & Answers

awk truncating first field output?

Hello, I'm writing an Awk script to take a command line argument (student's name) and output their relevant student#, name, and marks. For some reason, awk arbitrarily removes the first digit from the student number and doesn't show me the proper output. Here is my code: #! /usr/bin/awk -f... (6 Replies)
Discussion started by: trashmouth12
6 Replies

4. UNIX for Dummies Questions & Answers

NEWB Question - BASH COMMAND RESULT for ${0##*/}

This should be extremely simple and someone will probably answer it in .5 seconds. I need to know what: VARIABLE=${0##*/} does? I do not have a shell handy to just try it in. I am reading through some scripts and need to understand this line. Any help is appreciated. Many thanks! -... (3 Replies)
Discussion started by: chrisgoetz
3 Replies

5. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

6. Shell Programming and Scripting

Selecting awk output depending on grep result

Hi, I don't script often enough to know how to do this, and I can't seem to find a right example online. I have a csv output from an old, old system (Win2K???), from which I want to extract only certain fields. Initially I came up with something like this: cat file1 | awk -F '"' '{print $8... (7 Replies)
Discussion started by: takada
7 Replies

7. Shell Programming and Scripting

How i can put the result of a command inside a bash variable?

#!/bin/bash #... for i in `ls -c1 /usr/share/applications` do name="cat $i | grep ^Name= | cut -d = -f2" echo $name #... done Now inside name as output is present: while i want only the result of the command. Ideally i would like obtain that information using only bash ... or... (8 Replies)
Discussion started by: alexscript
8 Replies

8. Shell Programming and Scripting

awk output field delimiter

Dear All, 1.txt (tab in between each value in a line) a b c a b c a c d you can see below, why with ~ i can output with tab, but = cannot? # awk -F'\t' '$2 ~ /b/' 1 a b c a b c # awk -F'\t' '$2 = "b"' 1 a b c a b c a b d ... (1 Reply)
Discussion started by: jimmy_y
1 Replies

9. Shell Programming and Scripting

Help! Paste Multiple SQL output result to exec command

Hi, I want to write the shell script to change multple file name (the file name is get from DB) e.g. cp db1.txt file1_new.txt cp db2.txt file2_new.txt cp db3.txt file3_new.txt I have write the script like this: VAR=`sqlplus -s $LOGON @<<ENDOFTEXT set termout off ... (0 Replies)
Discussion started by: jackyntk
0 Replies

10. HP-UX

awk to output cmd result

I was wondering if it was possible to tell awk to print the output of a command in the print. .... | awk '{print $0}' I would like it to print the date right before $0, so something like (this doesn't work though) .... | awk '{print date $0}' (4 Replies)
Discussion started by: IMTheNachoMan
4 Replies
Login or Register to Ask a Question