Remove new line and convert Month to Decimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove new line and convert Month to Decimal
# 1  
Old 01-19-2017
Remove new line and convert Month to Decimal

# Sample input
Code:
common-name www.test.com.au
expiration Dec 21 01:00:31 2017 GMT
common-name www.test1.com.au
expiration Jan 19 04:41:03 2018 GMT

# Desired Output
# Field 1: Domain name
# Field 2: Date/time converted to Austraian format DD/MM/YYYY and on the same line as Domain Name.
# These will be import to excel and sort by date later.
# Repeat
Code:
www.test.com.au		21/12/2017
www.test1.com.au	19/01/2018

# My attempt below
Code:
awk '/common-name/ {print $2}/expiration/{print $3,$2,$5}'

# Current output
Code:
www.test.com.au
21 Dec 2017
www.test1.com.au
19 Jan 2018

1) How to remove the new line?
2) If Month can not be converted, i will look at excel function.
# 2  
Old 01-19-2017
Hello thangbom,

Could you please try following and let me know if this helps(haven't tested it though).
Code:
awk '/common-name/{VAL=$2} /expiration/{num=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", A,",");for(i=1;i<=num;i++){B[A[i]]=i};print VAL FS $3"/"B[$2]"/"$(NF-1)}'  Input_file

Output will be as follows.
Code:
www.test.com.au 21/12/2017
www.test1.com.au 19/1/2018

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-19-2017
Try this:

Code:
awk '
/common-name/ {DN=$2}
/expiration/{
   print DN,$3 "/" 1 + int(\
       index("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC",\
       toupper($2))/3) "/" $5
}' infile

Edit: RaviderSingh13 just beat me on this, but still worth keeping for alternate month conversion logic
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 01-19-2017
Thank you. Works fine.

---------- Post updated at 01:15 PM ---------- Previous update was at 01:14 PM ----------

Quote:
Originally Posted by Chubler_XL
Try this:

Code:
awk '
/common-name/ {DN=$2}
/expiration/{
   print DN,$3 "/" 1 + int(\
       index("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC",\
       toupper($2))/3) "/" $5
}' infile

Edit: RaviderSingh13 just beat me on this, but still worth keeping for alternate month conversion logic
Thank you. Your code works as well.
# 5  
Old 02-08-2017
Hi R Singh,
I 'd like to make change to the script for some new requirements. Hope you can give me a quick explanation how it works so i can go away and study it. Thanks

---------- Post updated at 03:39 PM ---------- Previous update was at 03:35 PM ----------

Quote:
Originally Posted by Chubler_XL
Try this:

Code:
awk '
/common-name/ {DN=$2}
/expiration/{
   print DN,$3 "/" 1 + int(\
       index("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC",\
       toupper($2))/3) "/" $5
}' infile

Edit: RaviderSingh13 just beat me on this, but still worth keeping for alternate month conversion logic
Hi Chubber_XL, sorry for a simple question but is there a way to run this code on the 1 single line instead of multiple lines.
# 6  
Old 02-08-2017
How exactly would your input and the desired output look like?
# 7  
Old 02-08-2017
Both above solutions works fine. I am fairly new to this so just need a short explanation how the codes work. That's all. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Convert hexadecimal value to decimal value

Hi All, cat filename | awk '{print $1, $2, $4, $5, $6, $7, $8, $9, $10;}' | awk 'NF > 0' OUTPUT: 2015-01-19 00:12:32 00000000fbfa0000 000000009ae5cf80 014d 015d 0017 003c 0362de20 2015-01-19 00:13:52 00000000fc820000 00000000994c6758 014c 015d 000b 003c 08670250 2015-01-19 00:14:25... (12 Replies)
Discussion started by: sam@sam
12 Replies

2. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

3. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

4. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

5. Shell Programming and Scripting

Convert From Month Number to Month Name

Hi, I have a script that accepts an input date from the user in yyyy-mm-dd format. I need to get the mm-dd part and convert it to month name. example: 2011-11-15 I want that to become "Nov 15" I don't have the GNU date, I am using an AIX os. Thanks. (1 Reply)
Discussion started by: erin00
1 Replies

6. Shell Programming and Scripting

Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file. If the length is less than the max length, the spaces should be appended... (4 Replies)
Discussion started by: Amrutha24
4 Replies

7. Shell Programming and Scripting

Convert hexadecimal value in decimal value

hi all, this is my script: #! /bin/sh minutes=$( { i2cget -f -y 0 0x51 3; } 2>&1 ) minutes=${minutes:2} hour=$( { i2cget -f -y 0 0x51 4; } 2>&1 ) hour=${hour:2} day=$( { i2cget -f -y 0 0x51 5; } 2>&1 ) day=${day:2} month=$( { i2cget -f -y 0 0x51 7; } 2>&1 ) month=${month:2} ... (6 Replies)
Discussion started by: enaud
6 Replies

8. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

9. Shell Programming and Scripting

Convert into month name

Hi, If I am having month like 2/3/4 then how can I convert it into month name like Feb/Mar/Apr.... Is there any defined function in Perl ?? (2 Replies)
Discussion started by: darshakraut
2 Replies

10. Shell Programming and Scripting

Convert exponential value to decimal

Hi, Here is my script to read a file into array: awk -F '+' ' # load first file into array indexed by fields 1 and 2 NR == FNR { file1nr = FNR for (i=3; i<NF; i++) { file1 = $i } I have this... (5 Replies)
Discussion started by: Sangtha
5 Replies
Login or Register to Ask a Question