Use two field separator in the same line and print them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use two field separator in the same line and print them
# 1  
Old 08-10-2015
Hammer & Screwdriver Use two field separator in the same line and print them

Hi Guys,

I have the file

---
Code:
HOST_NAME,data_coleta,data_carga,CPU_util,CPU_idle,run_queue,memory,MEMORY_SYSTEM,MEMORY_TOTAL,MEMORY_SWAPIN,MEMORY_SWAPOUT,DISK_READ,DISK_WRITE,DISK_IO,NET_IN_PACKET,
NET_OUT_PACKET
bd-bkp-sjp,01/06/2015,03/06/2015,3.1908333333,99.8725,0.8333333333,20.649166667,0.55,21.199166667,0,0,192.98333333,1207.3083333,144.65,60.008333333,55.35
bdurasip,01/06/2015,03/06/2015,0.2608333333,99.755833333,0.5833333333,8.8458333333,0.28,9.1258333333,0,0,91.283333333,29.325,2.2,43.275,78.966666667
bd2-rj,01/06/2015,03/06/2015,12.168333333,87.866666667,1.4166666667,9.3791666667,0.27,9.6491666667,0,0,0.2166666667,49.775,2.6583333333,17.041666667,17.241666667

----

I can print it with
Code:
awk -F"," '{print " "$2";"8600";"$1";CPU_UTIL;SYSGLB;"$4";"$1" "}' $FILE

Output:
Code:
01/06/2015;8600;cl-wfl-01;CPU_UTIL;SYSGLB;49.974166667;cl-wfl-01
01/06/2015;8600;cl-wfl-02;CPU_UTIL;SYSGLB;1.7383333333;cl-wfl-02
01/06/2015;8600;cl-wrf-01;CPU_UTIL;SYSGLB;88.7825;cl-wrf-01


But I need to change the datetime format to YYYY-MM-DD

I can do this with
Code:
echo "19/05/2015" | awk -F/ '{printf "%s%02s%02s\n", $3"-", $2"-", $1}'




But I don't know how put it together in the same line.... Anyone can help me please?


Thanks in advanced
Best Regards
Antonio

Last edited by Don Cragun; 08-10-2015 at 09:20 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-10-2015
Antonio,

Your output does not reflect what your data says. I get:

Code:
awk -F"," '{print " "$2";"8600";"$1";CPU_UTIL;SYSGLB;"$4";"$1" "}' antonio.file
 data_coleta;8600;HOST_NAME;CPU_UTIL;SYSGLB;CPU_util;HOST_NAME
 01/06/2015;8600;bd-bkp-sjp;CPU_UTIL;SYSGLB;3.1908333333;bd-bkp-sjp
 01/06/2015;8600;bdurasip;CPU_UTIL;SYSGLB;0.2608333333;bdurasip
 01/06/2015;8600;bd2-rj;CPU_UTIL;SYSGLB;12.168333333;bd2-rj


The following gives me:
Code:
awk -F","  'FNR>1 {split($2, a, "/"); print a[3]"-"a[2]"-"a[1], "8600", $1, "CPU_UTIL", "SYSGLB", $4, $1}' OFS=";" antonio.file
2015-06-01;8600;bd-bkp-sjp;CPU_UTIL;SYSGLB;3.1908333333;bd-bkp-sjp
2015-06-01;8600;bdurasip;CPU_UTIL;SYSGLB;0.2608333333;bdurasip
2015-06-01;8600;bd2-rj;CPU_UTIL;SYSGLB;12.168333333;bd2-rj

Which I think is what you want.
This User Gave Thanks to Aia For This Post:
# 3  
Old 08-10-2015
Thanks

Aia,

You are correct I forgot remove the header file...

I'll study the split command I'v never seen it....

You save!! Thank you very much!!!


Thanks again
Best Regards
# 4  
Old 08-10-2015
Quote:
Originally Posted by antoniorajr
[...]
I'll study the split command I'v never seen it....
Muy facil.
Code:
split($2, a, "/")

$2 is the string you want to split
a is any user given variable name
"/" is the separator that you want to split from
After that, you can retrieve the values by using a[1], a[2], a[3]...
This User Gave Thanks to Aia For This Post:
# 5  
Old 08-11-2015
split will return the element count: n=split($2, a, "/"), so you will know where/when to stop looking for elements, e.g. for (i=1; i<=n; i++) print a[i]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. UNIX for Dummies Questions & Answers

Add a field separator (comma) inside a line of a CSV file

Hi... I can't find my little red AWK book and it's been a long while since I've awk'd. But I need to take a CSV file and convert the first word of the fifth field to its own field by replacing a space with a comma. This is for importing a spreadsheet of issues into JIRA... Example: a line... (9 Replies)
Discussion started by: Tawpie
9 Replies

4. Shell Programming and Scripting

Grep or print each section of a file on one line with a separator

I can obtain information from itdt inventory command however it display as below, I'd like to print each entity on one line but seperated by : the file is something like and each section ends with Volume Tag Drive Address 256 Drive State ................... Normal ASC/ASCQ... (3 Replies)
Discussion started by: gefa
3 Replies

5. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

6. Shell Programming and Scripting

Print a field from the previous line

plz help me!! I have this file , 3408 5600 3796 6035 4200 6285 4676 0 40 1554 200 1998 652 2451 864 2728 1200 0 I want it like if $2==0,replace it with field from the previous line+500 say here the o/p would be like 3408 5600 3796 6035 4200 6285... (16 Replies)
Discussion started by: Indra2011
16 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies

9. Shell Programming and Scripting

awk field separator or print command

Hello Experts, I am back, with another doubt. I am not sure what it relates to this time - awk or the print command actually. I'll explain the scenario: I have a huge file, and it has some traces(logs). In between those logs, there are statements with some SQL queries. All I want to do is... (4 Replies)
Discussion started by: hkansal
4 Replies

10. Shell Programming and Scripting

how to print field n of line m

Hi everyone, I have a basic csh/awk question. How do I print a given field from a given line in a given file? Thanks in advance! (11 Replies)
Discussion started by: Deanne
11 Replies
Login or Register to Ask a Question