awk :help to parse a file to change to separated by colon ":"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk :help to parse a file to change to separated by colon ":"
# 1  
Old 02-03-2013
awk :help to parse a file to change to separated by colon ":"

Hi experts ,


I am trying to get the below output:

file :
Code:
0/6/4/1  0x0019503C2E26 5   UP    lan5 snap5     1   ETHER     Yes     224
0/6/4/0  0x0019503C2E25 6   UP    lan6 snap6     2   ETHER     Yes     224
0/2/1/0  0x0019503E6900 0   UP    lan0 snap0     3   ETHER     Yes     224
0/2/1/1  0x0019502E52AA 1   UP    lan1 snap1     4   ETHER     Yes     224




The output to be look like this: in $2 , to remove 0x00 & to separate each 2 digit with :
Code:
0/6/4/1  19:50:3C:2E:26 5   UP    lan5 snap5     1   ETHER     Yes     224
0/6/4/0  19:50:3C:2E:25 6   UP    lan6 snap6     2   ETHER     Yes     224
0/2/1/0  19:50:3E:69:00 0   UP    lan0 snap0     3   ETHER     Yes     224
0/2/1/1  19:50:2E:52:AA 1   UP    lan1 snap1     4   ETHER     Yes     224

Thanks ,
# 2  
Old 02-03-2013
How about:

Code:
awk -F'\t' '{gsub(/^0x00/,"",$2); gsub(/../,"&:",$2);gsub(/:$/,"",$2)} 1' OFS="\t" infile

# 3  
Old 02-03-2013
And if the input is not tab-separated but of fixed width and you need to preserve the number of spaces/tabs between the columns, then try (with some assumptions):
Code:
awk '{t2=$2;s2=index($0,t2);l2=length(t2)
sub(/^0x00/,"",t2);gsub(/../,"&:",t2);sub(/:$/,"",t2)
print substr($0,1,s2-1) t2 substr($0,s2+l2)}' file

@Chubler_XL, the last gsub could be replaced with a sub.

@Scrutinizer: Yes, it's about using the right tool for the right job!

Last edited by elixir_sinari; 02-04-2013 at 01:51 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 02-04-2013
Code:
sed 's/0x00\(..\)\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:\5/' file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-04-2013
First one didn't return any output.
elixir_sinari's code & Scrutinizer's code worked very fine. The sed one being small code is nice .


Thanks a lot,
# 6  
Old 02-04-2013
Just realized the sed could be shortened a little:
Code:
sed 's/0x00\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:/' file

# 7  
Old 02-04-2013
Scrutinizer,

This works nicely: can you please explain how the regular expression working in this case,
Code:
sed 's/0x00\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:/' file

Thanks a lot..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

3. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

4. Shell Programming and Scripting

Regexp to separated rows by "asterisks-new line" in awk

Hello to all, I have the text file below, how would be the REGEXP to set the RS to separate registers by asterisks-newline-asterisks (highlighted in red) and FS as the default, in order that the fourth field ($4) always be the number after REG (in blue)? I'm trying with code below, but is... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

5. Shell Programming and Scripting

How to sort a column in UNIX that is colon separated ":" ?

Experts, how to sort this fields with numerical order : -How to use the sort command in this case, I was thinking with -k but it is not working, lan5000 lan5000:1 lan5000:10 lan5000:11 lan5000:12 lan5000:13 lan5000:14 lan5000:15 lan5000:16 lan5000:17 ... (6 Replies)
Discussion started by: rveri
6 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Help to change the file with "sed" and "awk"

Hi experts I want your help to change the file format to my wanted version, please give me a hand thanks $cat file install pass make os pass make build kernel failed usb storage pass chane to | *install* | *make os* | *make build kernel* | *usb storage* | | pass | pass... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

8. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

9. Shell Programming and Scripting

How to cut a file using " ", but fields can be separated with more than one " "

Hello, let's say I have a text file: word11 word12 word13 word21 word22 word23 word31 word32 word33 and I want to put the second field of each line into a list: set list = `cut -d" " -f2 ${1}` and I use space (" ") as a delimiter, only that there's a catch: there can be more than... (12 Replies)
Discussion started by: shira
12 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question