Using awk, how to 'properly' print the rest of the string?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using awk, how to 'properly' print the rest of the string?
# 1  
Old 03-18-2020
Using awk, how to 'properly' print the rest of the string?

Hi,


Script below is working. But I want to put a "=" after the new date format BUT excluding the extra space Smilie

Code:
$ cat x.bash
#!/bin/bash
#

cat x.txt
echo

awk '
   BEGIN {
      split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month, " ")
      for (i=1; i<=12; i++) mdigit[month[i]]=i
   }
   { m=substr($1,4,3)
     $1 = sprintf("%04d-%02d-%02d=",substr($1,8,4),mdigit[m],substr($1,1,2))
     print
   }' x.txt | sort | uniq > x.txt.00

cat x.txt.00
echo

Example run below:


Code:
$ ./x.bash
18-MAR-2020 04:28:13 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=

2020-03-18= 04:28:13 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=

Note the space after the day, trying to work out how to get rid of it Smilie The preferred output is


Code:
2020-03-18=04:28:13 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=

Tried this one below and still the same Smilie, still have the space



Code:
$1 = sprintf("%04d-%02d-%02d=",substr($1,8,4),mdigit[m],substr($1,1,2),substr($0,13))


Last edited by newbie_01; 03-18-2020 at 03:21 AM.. Reason: update code
# 2  
Old 03-18-2020
That space is the OFS between $1 and $2. Try like


Code:
awk '
BEGIN   {for (i=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month); i; i--) mdigit[month[i]] = i
        }
        {$1 = sprintf("%04d-%02d-%02d=",substr($1,8,4),mdigit[substr($1,4,3)],substr($1,1,2)) $2
         $2 = ""         
         print
        }
  ' file
2020-03-18=04:28:13  * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=

Note the two spaces after the date/time string, the two OFS around the empty $2. If that hurts, put in an e.g. sub (FS FS, FS)


EDIT: or, try


Code:
    {sub ($1 FS $2, sprintf("%04d-%02d-%02d=",substr($1,8,4),mdigit[substr($1,4,3)],substr($1,1,2)) $2)
     print
    }

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-18-2020
Thanks as usual RudiC. I used the sub option instead



Code:
   BEGIN {
         for ( i = split( "JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month ) ; i ; i-- )
         mdigit[month[i]] = i
      }
      {
         sub ( $1 FS $2, sprintf( "%04d-%02d-%02d=",substr($1,8,4),mdigit[substr($1,4,3)],substr($1,1,2) ) $2 )
         print
      }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print not working properly

Hello friends, There is one requirment where I need to login into database environment and pull all schema names into a text file ... as of now below are the schemas available... $> describe keyspaces; system_schema system_auth system abc system_distributed system_traces Now from... (4 Replies)
Discussion started by: onenessboy
4 Replies

2. Shell Programming and Scripting

awk to print string if tag is specific value

In the below awk I am trying to print expName only if another tag planExecuted is true. In addition to the expName I am also printing planShortID. For some reason the word experiment gets printed so I remove it with sed. I have attached the complete index.html as well as included a sample of it... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. UNIX for Dummies Questions & Answers

Tree command does not print properly

Hi, when I run tree command in linux box getting below image, not the line test âââ lost+found âââ test1 â  âââ aaa.txt â  âââ bbb.txt âââ test2 3 directories, 2 files installed tree-1.5.3-2.el6.x86_64 package (8 Replies)
Discussion started by: stew
8 Replies

4. Shell Programming and Scripting

awk print string with removing all spaces

Hi all, I want to set 10 set of strings into a variable where: removing all spaces within each string change the delimiter from "|" to "," Currently, I've the below script like this:Table=`ten character strings with spaces in-between and each string with delimiter "|" | tr -d ' ' |... (7 Replies)
Discussion started by: o1283c
7 Replies

5. Shell Programming and Scripting

awk find and print next string

Can I do this in one awk session. Solution I have is poor. I want to return the number after PID. echo "Start: 12345 is used by PID:11111 username" | awk -F: '{print $3}' | awk '{print $1}' (6 Replies)
Discussion started by: u20sr
6 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

8. Shell Programming and Scripting

Search a string and print the rest of line

Hi Guys, I need to search a string and print the rest of the lines... input: 8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n output: 00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n Actually i don even need the first "00".. any suggestions is appreciated..... (13 Replies)
Discussion started by: mac4rfree
13 Replies

9. UNIX for Dummies Questions & Answers

AWK...find a string ,if so print it

Hi, I need to find a string, if it finds then I need to print it , otherwise it has to goto next line.... input is====> uid = shashi, india uid ,uid= asia uid= none, uid=india. none ========== output shold be uid = shashi, india uid , uid= asia uid= none, uid=india. none ... (1 Reply)
Discussion started by: hegdeshashi
1 Replies

10. Shell Programming and Scripting

SED Exclude Matches Between Pattern1 And Pattern2 Containing Pattern3 Print The Rest

Hi, From the sample file below Conditions 1) Pattern Range must start with "ALTER TABLE" 2) Pattern Range ends when it finds ";" 3) Between this range i want to select all the patterns that contain pattern " MOVE " Note : I would like to exclude the above pattern matches and print... (1 Reply)
Discussion started by: rajan_san
1 Replies
Login or Register to Ask a Question